Essentialssec7.htm

Essentials of C++: Section VII


Iterative Statements

In this section we discuss the statements that cause the computer to execute some set of instructions over and over. There are three of these: the while, the do while, and the for statements.

A. The while Statement

The general form of the while statement is:

     while (some test condition )
      {

     some set of instructions
      }

It, thus, takes much the same form as the 'if' statement but its semantics are different. A 'while' statement can cause the same set of instructions to be executed many times. An 'if' statement is used to determine if a given set of instructions is executed once or not at all - there is no repetition.

When executed, a 'while' statement first examines the test condition. If TRUE is returned, the system executes the lines inside the brackets. It then re-evaluates the test condition and repeats the instructions inside the brackets if the result is still TRUE. This continues until the test condition returns FALSE. Note that if the test condition returns FALSE when first evaluated, the instructions inside the brackets are never executed. As with the 'if' statement, the brackets are technically not needed if there is only one instruction to be executed but you are advised to always use them. Also, as with the if statement, the code inside the while statement (whether in brackets or not) represents a block of code.

The form of the test condition is the same as with the 'if' statement and can involve logical operators. Note that if the code is written so that the test condition always returns TRUE, the program will be stuck in an infinite loop. In other words, it will never stop.

As with the 'if' statement, one can use any valid C++ instructions inside a 'while' statement. Thus, one can have (or nest) 'while' statements inside 'while' statements.
 

B. The do while Statement
The "do while" statement has the form:

     do
      // some set of instructions
      while (some test condition );

The key difference between this and the 'while' statement is that since the "do while" has its test condition at the end of the statement, the instructions are executed at least once. Note that there are no brackets because the keywords 'do' and 'while' enclose the statements and form a block of code. Brackets can, however, be inserted to make the code more readable. Note also that there is a semi-colon at the end of the statement - unlike the 'while' statement.
 

C. The for Statement
The 'for' is the most complex of the three iterators. It has the form:

     for (initialization expressions; test condition; increment expressions)
      {

      some set of instructions
     }

The parentheses and the semi-colons separating the three elements inside the parentheses are required although all the elements themselves are optional. The same comments as above apply to the brackets.

When executed, a 'for' statement first executes all its initialization expressions (if there are any). If there is more that one initialization expression, they are separated by commas. It then examines the test condition. It is possible to have more than one test in the test condition. If so, the tests are separated by commas. They are combined together as if '&&' (the logical AND operator) separated them. If logical 'OR's are required, they must be explicitly coded.

If the test condition returns TRUE, the code inside the brackets is executed. Once the instructions inside the brackets are completed, all the increment expressions (if any) are executed. Again, if there is more than one increment expression, they are separated by commas. After executing all the increment expressions, the system again evaluates the test condition. If the result is still TRUE, the code followed by the increment expressions is again evaluated. This continues until the test condition returns FALSE.

One way to code an infinite loop is by writing:

     for (initialization expressions; ;increment expressions)
     {

     > some set of instrutions
     }

This is an infinite loop because there is no test condition to ever return FALSE.

The initialization expressions can include variable declarations. The scope of such variables depends on which version of C++ you are using. In earlier versions, the scope is from the point of declaration until the end of the block including the 'for' statement. In other words, such variables are not just known inside the 'for' statement. With more recent versions of C++, including Borland C++ 5.0, the scope of variables declared in the initialization expression covers only the 'for' loop itself. In other words, such variables are known only inside the 'for' loop.

D. Using the return, break and continue Statements Within Iteration Statements
We have already introduced the 'return' and 'break' statements. The 'return' statement can actually be used to exit from anywhere inside a function, including the middle of an iteration.

The 'break' statement terminates a specific iteration (or 'switch') but does not exit a function. If a 'break' is part of a nested iteration or 'switch' statement it terminates or exits only those iteration or 'switch' statements at its level of nesting or deeper. For example, in the following code:


   for (int x = 0; x < 5; x++)
   {   while (y < 5)
       {  if (z == 3)
          {  break;
          }
          else
          {  y++
          }
          cout << y;
       }
       cout << x;
   }
the 'break' statement will cause the 'while' loop to terminate but it will have no effect on the 'for' loop. This code is also, by the way, a good example of the way iterator and selection statements can be nested - although the code itself makes little sense.

The 'continue' statement allows one to stop a particular iteration of a loop while allowing the loop itself to continue. When encountered, the 'continue' statement causes the system to transfer control or 'jump' to the test condition for 'while' and 'do' loops and to the increment expressions (to be followed by the test condition) in 'for' loops. The loop itself stops then only if the test condition is FALSE. If we substitute "continue" for "break" in the code above, the result will be that the value of 'y' will not print if z equals 3 but the 'while' loop will continue as long as 'y' is less than 5.

 

Links to 'The Story of C++" and other documents

The 'while' Statement
The Basic 'for' Statement
The 'for' Statement with Arrays
Multiple Test Conditions in a 'for' Statement
Advanced Discussion of the 'for' Statement
The Basic Return Statement
Another Use of the Return Statement
The Break Statement Used With the Switch Statement

Top of Section Main Menu of Essentials of C++ Next Section