Comparison of the loop
   control structures
            Prakash Khaire
B V Patel Inst. of BMC & IT, GopalVidyanagar
for                        while                      do…while
A for loop is used to      A while loop is used to    A do while loop is used to
execute and repeat a       execute and repeat a       execute and repeat a
statement block depending statement block depending statement block depending
on a condition which is    on a condition which is    on a condition which is
evaluated at the beginning evaluated at the beginning evaluated at the end of the
of the loop.               of the loop.               loop.

Example :                  Example                    Example
for(i=1; i<=5; i++)        i=1;                       i=1;
{                          while(i <=5 )              do
    s = s + i;             {                           {
    p = p * i;               s = s + i;                    s = s + i;
}                            p = p * i;                    p = p * i;
                             i++;                          i++;
                           }                          }while(i<=5);
for                         while                       do…while
A      variable   value    isA     variable   value    isA     variable   value    is
initialized at the beginninginitialized at the beginninginitialized before the loop
of the loop and is used inor before the loop and isor assigned inside the loop
the condition.               used in the condition.      is used in the condition.

A statement to change theA statement to change theA statement to change the
value of the condition or tovalue of the condition or tovalue of the condition or to
increment the value of theincrement the value of theincrement the value of the
variable is given at thevariable is given inside thevariable is given inside the
beginning of the loop.      loop.                       loop.
The statement block will The statement block willThe statement block will
not be executed when thenot be executed when thenot be executed when the
value of the condition isvalue of the condition isvalue of the condition is
false.                   false.                   false, but the block is
                                                  executed at least once
                                                  irrespective of the value of
                                                  the condition
for                      while                    do…while
A for loop is commonlyA while loop is also widelyA do-while loop is used in
used      by     manyused         by       manysome cases where the
programmers.          programmers.               condition need to be
                                                 checked at the end of the
                                                 loop.
break Statement
* The break statement, when executed in a
  while, for, do/while, or switch structure,
  causes immediate exit for that structure.
* Program execution continues with the first
  statement after the structure.
* Common uses of the break statement are
  to escape early from a loop, or to skip the
  remainder of a switch structure
Continue Statement
* The continue statement, when executed
   in a while, for, or do/while structure, skips
   the remaining statements in the body of
   that structure, and proceeds with the next
   iteration of the loop.
* In while and do/while structures, the loop-
   continuation test is evaluated
   immediately after the continue statement
   is executed.
* In the for structure, the increment
   expression is executed, then the loop-
   continuation test is evaluated.
#include <stdio.h>
 #include <stdlib.h>
int main ()
 { unsigned x;
 x = 0;
while (x < 10)
 {
++x;
     if (x % 2 == 0)
      {
            continue;
      }
     printf ("%i is an odd number.n", x);
 }
 return 0;
}
Lecture15 comparisonoftheloopcontrolstructures.ppt

Lecture15 comparisonoftheloopcontrolstructures.ppt

  • 1.
    Comparison of theloop control structures Prakash Khaire B V Patel Inst. of BMC & IT, GopalVidyanagar
  • 2.
    for while do…while A for loop is used to A while loop is used to A do while loop is used to execute and repeat a execute and repeat a execute and repeat a statement block depending statement block depending statement block depending on a condition which is on a condition which is on a condition which is evaluated at the beginning evaluated at the beginning evaluated at the end of the of the loop. of the loop. loop. Example : Example Example for(i=1; i<=5; i++) i=1; i=1; { while(i <=5 ) do s = s + i; { { p = p * i; s = s + i; s = s + i; } p = p * i; p = p * i; i++; i++; } }while(i<=5);
  • 3.
    for while do…while A variable value isA variable value isA variable value is initialized at the beginninginitialized at the beginninginitialized before the loop of the loop and is used inor before the loop and isor assigned inside the loop the condition. used in the condition. is used in the condition. A statement to change theA statement to change theA statement to change the value of the condition or tovalue of the condition or tovalue of the condition or to increment the value of theincrement the value of theincrement the value of the variable is given at thevariable is given inside thevariable is given inside the beginning of the loop. loop. loop. The statement block will The statement block willThe statement block will not be executed when thenot be executed when thenot be executed when the value of the condition isvalue of the condition isvalue of the condition is false. false. false, but the block is executed at least once irrespective of the value of the condition
  • 4.
    for while do…while A for loop is commonlyA while loop is also widelyA do-while loop is used in used by manyused by manysome cases where the programmers. programmers. condition need to be checked at the end of the loop.
  • 5.
    break Statement * Thebreak statement, when executed in a while, for, do/while, or switch structure, causes immediate exit for that structure. * Program execution continues with the first statement after the structure. * Common uses of the break statement are to escape early from a loop, or to skip the remainder of a switch structure
  • 6.
    Continue Statement * Thecontinue statement, when executed in a while, for, or do/while structure, skips the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. * In while and do/while structures, the loop- continuation test is evaluated immediately after the continue statement is executed. * In the for structure, the increment expression is executed, then the loop- continuation test is evaluated.
  • 7.
    #include <stdio.h> #include<stdlib.h> int main () { unsigned x; x = 0; while (x < 10) { ++x; if (x % 2 == 0) { continue; } printf ("%i is an odd number.n", x); } return 0; }