Note In the while loop the expression is tested before the loop body statements

Note in the while loop the expression is tested

This preview shows page 3 - 5 out of 11 pages.

Note : In the  while  loop the expression is tested before the loop body statements are entered. So, if  the expression is  false  in the beginning the loop body is never entered. do…while statement : Syntax: do statement while (expression) ; Here, the expression is tested after the statement(s) are executed. So, even if the   expression is  false in the beginning the loop body is entered at least once. Here also like the while statement, 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: The example of while loop can be written with do…while loop as : do { printf (“Enter a number\n”) ; scanf (“%d”, &number) ; } while (number > 100) ; In the above example, as long as the entered value of 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. Note: In the conditional looping using  while  and  do…while  the number of times the loop will  be executed is not known  at the start of the program.  Page 3 of 11
Image of page 3
for statement : Syntax: for (expression 1; expression 2; expression 3) Statement In the iterative looping using for loop the number of times the loop will be executed is known at the start of the program. In the above syntax, the expression 1 is the initialization statement which assigns an initial value to the loop variable. The expression 2 is the testing expression where the loop variable value is checked. If it is true the loop body statement(s) are executed and if it is false the statements(s) are not executed. The expression 3 is usually an increment or decrement expression where the value of the loop variable is incremented or decremented.
Image of page 4
Image of page 5

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