do
<loop_body>;
while (<condition>);
If the loop body contains more than one statement, it should be enclosed by a pair of braces.
In a do-while loop, the condition is tested after execution of the loop body. If the condition is non-zero (i.e. true), the control goes back and the loop body is executed again. If the condition is zero (i.e. false), the loop is exited.
The C++ do-while structure is equivalent to the following:
Equivalent structure using while statement in C++ | <loop_body>; while (<condition>) <loop_body>; |
Equivalent structure using repeat-until statement in Pascal | repeat <loop_body> until not <condition> |
do-while loop | while loop |
The loop body is executed at least once. | It is possible that the loop body is never executed (when the condition is zero as the loop is entered). |
The condition is tested after the loop body is executed. | The condition is tested before the loop body is executed. |
do-while loop in C++ | repeat-until loop in Pascal |
The loop is exited when the condition is false (i.e. zero). | The loop is exited when the condition (after until) is true. |
The result of this program is identical to Program 18 except that there is only one cin statement in the loop in this program. This is because a do-while loop is used.
C++ version (using do-while loop) | Pascal version (using repeat-until loop) |
#include <iostream.h> main() { int score, sum = 0; do { cout << "Enter score (-1 to quit): "; cin >> score; if (score != -1) sum += score; } while (score != -1); cout << "Sum = " << sum << endl; return 0; } |
program prog_19(input, output); var score, sum: integer; begin sum := 0; repeat write('Enter score (-1 to quit): '); readln(score); if score <> -1 then sum := sum + score until score = -1; writeln('Sum = ', sum); end. |
for (<expression_1>;
<expression_2>; <expression_3>)
<loop_body>;
If the loop body contains more than one statement, it should be enclosed by a pair of braces.
The above C++ for structure is equivalent to the following:
<expression_1>;
while (<expression_2>) {
<loop_body>;
<expression_3>;
}
Therefore Program 16 can be re-written as follows:
#include <iostream.h> main() { int n, product; cout << "n? "; cin >> n; for (product = n; product <= 1000; product *= n) cout << product << endl; return 0; } |
Usually, for statement is used as a counter-controlled loop. In such case, <expression_1> is an intialisation statement, <expression_2> is an expression testing whether the final value is reached, and <expression_3> indicates how the counter variable is incremented (or decremented) in the loop.
Counter-controlled loop in C++ (using for) | for (<counter> = <initial>; <counter>
<= <final>; <counter>++) <loop_body>; |
Equivalent counter-controlled loop in C++ (using while) | <counter> =
<initial>; while (<counter> <= <final>) { <loop_body>; <counter>++; } |
Equivalent Pascal control structure | for <counter> := <initial> to
<final> do <loop_body> |
The following shows how Program 17 (containing a counter-controlled loop) can be re-written using for statement:
C++ version | Pascal version |
#include <iostream.h> main() { int score, sum, i; sum = 0; for (i = 1; i <=5; i++) { cout << "Score No. " << i << ": "; cin >> score; sum += score; } cout << "Sum = " << sum << endl; return 0; } |
program prog_21(input, output); var score, sum, i: integer; begin sum := 0; for i := 1 to 5 do begin write('Score No. ', i, ': '); readln(score); sum := sum + score; end; writeln('Sum = ', sum) end. |