slide07-Repetitions - ICS 103 Computer Programming in C Handout-7 Topic Control Structures(Repetition Loop Statements Objective To know use of increment

slide07-Repetitions - ICS 103 Computer Programming in C...

This preview shows page 1 - 4 out of 11 pages.

ICS 103: Computer Programming in C Handout-7 Topic: Control Structures ( Repetition & Loop Statements ) Objective: To know use of increment operator ++, and decrement operator -- To know syntax of while , do-while , for statements (loops). To know working of above loops with examples. Use of increment operator ++, and decrement operator - - : i++ means i=i+1; i -- means i=i-1; (For above statements in first step initial value of i will be assigned and after that value of i will be incremented or decremented). ++i means i=i+1 ; -- i means i=i-1 ; (But in these, in first step initial value of i will be incremented or decremented and then value of i will be assigned ). Examples: #include<stdio.h> main() { int i=2, r1 ; r1=i++; printf("Value of r1 is : %d", r1); } Sample Output: Page 1 of 11
Image of page 1
#include<stdio.h> void main() { int i=2, r1 ; r1 = ++i; printf("Value of r1 is : %d", r1); } Looping Structures : While statement : Syntax : while (expression) statement A conditional loop is one whose continued execution depends on the value of a logical  expression. In the above syntax as long as the expression is  true the statement (or statements if enclosed within braces) is executed. When the expression evaluates to false the execution of the statement(s) stops and program continues with the other following statements. Example: Let number be an integer variable. while (number > 100) { printf (“Enter a number\n”) ; scanf (“%d”, &number) ; } Page 2 of 11
Image of page 2
In  the  abo ve  e xam ple , as  lo ng  as  the  e nte re d  value  o f  number is greater than 100 the loop continues to ask to enter a value for number . Once a number less than or equal to 100 is entered it stops asking. If in the beginning itself the value of number is less than or equal to 100, then the loop is not at all executed.
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 11 pages?

  • Spring '10
  • zaman
  • loop, Do while loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes