Repetition Structure (Looping)

Instead of a sequence of C/C++’s code execution in programs, in the real C/C++ programming there is a lot of conditional and branching. Other than a sequence code execution, a common loops (repetition) in C/C++ programming is the for, while and do-while loops. For conditional execution (selection) we can use if and its variations such as if-else, if-else-if and switch-case-break.

The while loop in C/C++ is very similar to the for loop. The for statement contains two semicolons, which allows placement of the initialization statement, the negation of the termination condition and the iterative statement in it. However, the while loop allows placement of only the condition so the other two statements must be placed outside the while statement.

In more complete comparison, we may have:

while loop

initial condition
while ( terminal condition )

      iteration

for loop

for(initial condition ; terminal condition ; iteration)

or

initial condition
for( ; terminal condition; )
    iteration



while

Executes a block of statements as long as a specified condition is true.

The general form:

while(condition)
     statement(s);
     next_statement;


  
      The (condition) may be any C/C++ valid expression.
      The statement(s) may be either a single or a compound (a block) C/C++ statement.
      When program execution reaches a while statement, the following events occur:

       1.The (condition) is evaluated.
       2.If (condition) evaluates as false (that is zero), the while statement terminates and
          execution passes to the first statement following statement(s) that is the next_statement.
       3.If (condition) evaluates as true (that is non zero), the C/C++ statement(s) are executed.
       4.Then, the execution returns to step number 1.

do..while


Executes a block of statements as long as a specified condition is true.
Test the condition at the end of the loop rather than at the beginning, as is done by the for loop and the while loop.
 
The do-while loop construct is:

do{

   // statement(s);
} while(condition);
(condition) may be any C/C++ valid expression.
statement(s) may be either a single or compound (a block) C/C++ statement.
When the program execution reaches the do-while statement, the following events occur:
       1. The statement(s) are executed.
       2. The condition is evaluated.  If it is true, execution returns to step number 1.
           If it is false, the loop terminates and the next_statement is executed.
 This means the statement in the do-while will be executed at least once.

for


Actually, the same task that can be performed by for statement that we have discussed.But, while statement does not contain an initialization section, the program must explicitly initialize any variables before executing the while expression.

The for loop construct is:

for (initial; condition; update)

{
   // statement(s);
}
As conclusion, while statement is essentially a for statement without the initialization and increment components.
  
The comparison between for and while:

for( ; condition; )  vS   while(condition)

The tasks that can be accomplished with a for statement can also be done with a while statement.
If for statement is used, the initialization, test and increment expressions are located together and are easy to find and modify. Just like for and if statements, while statements can also be nested.

 
 
Copyright © C++ Lab