1: //looping.cpp
2: //C++ code examples illustrating looping constructs
4: //How to format a while-loop with a one-statement body:
5: while (condition)
6: statement;
8: //If "statement" is very short, the following is also permissible:
9: while (condition) statement;
11: //How to format a while-loop with a multi-statement body:
12: while (condition)
13: {
14: statement1;
15: statement2;
16: }
18: //How to format a do...while-loop with a one-statement body:
19: do
20: statement;
21: while (condition);
23: //How to format a do...while-loop with a multi-statement body:
25: do
26: {
27: statement1;
28: statement2;
29: }
30: while (condition);
32: //How to format a for-loop with a one-statement body:
33: for (int i = start; i <= finish; i++)
34: statement;
36: //The following reduced-horizontal-spacing format may also be
37: //used, and though it is inconsistent with the usual recommendation
38: //to have a blank space on either side of an operator, in the
39: //for-loop situation it actually seems to enhance readability by
40: //emphasizing the groupings containing the loop control variable:
41: for (int i=start; i<=finish; i++)
42: statement;
44: //If "statement" is very short, the following is also permissible:
45: for (int i=start; i<=finish; i++) statement;
47: //How to format a for-loop with a multi-statement body:
48: for (int i=start; i<=finish; i++)
49: {
50: statement1;
51: statement2;
52: }