This blog is under construction

Saturday, 21 April 2012

Loop control structure - do while statement

do while is a loop construct which executes the statements inside the loop at least once, regardless of whether the expression is true or false.  If the expression evaluates to be false, then the loop terminates.  Otherwise, the statements inside the loop will get executed.  This continues until the expression evaluates to be false.  Below is the syntax for do while loop construct.

Syntax for do while loop construct:
do {
statements;
} while(expression);


Example C program using do-while:
 
  #include <stdio.h>
  int main() {
        int i = 1;
        do {
                printf("%d\n", i);  // print the value of i
                i++; // incrementing the value of i
        } while(i < 10);  // validating the expression
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  1
  2
  3
  4
  5
  6
  7
  8
  9



No comments:

Post a Comment