Skip to content

Loops

Sometimes, we need to perform a task repeatedly. To avoid writing excessive duplicate code, we need loops.

Sometimes, the number of repetitions is not a constant, so we cannot simply duplicate the code multiple times; we must use loops.

for Statement

The structure of a for statement is as follows:

1
2
3
for (initialization; condition; update) {
  loop body;
}

Execution order:

e.g., reading n numbers:

1
2
3
for (int i = 1; i <= n; ++i) {
  cin >> a[i];
}

Any of the three parts of a for statement can be omitted. If the condition is omitted, it is equivalent to the condition always being true.

while Statement

The structure of a while statement is as follows:

1
2
3
while (condition) {
  loop body;
}

Execution order:

e.g., verifying the 3x+1 conjecture:

1
2
3
4
5
6
7
while (x > 1) {
  if (x % 2 == 1) {
    x = 3 * x + 1;
  } else {
    x = x / 2;
  }
}

do...while Statement

The structure of a do...while statement is as follows:

1
2
3
do {
  loop body;
} while (condition);

Execution order:

The difference from the while statement is that the do...while statement executes the loop body first before checking the condition.

e.g., enumerating permutations:

1
2
3
do {
  // do someting...
} while (next_permutation(a + 1, a + n + 1));

Relationship Between the Three Statements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// for statement

for (statement1; statement2; statement3) {
  statement4;
}

// while statement

statement1;
while (statement2) {
  statement4;
  statement3;
}

These are equivalent when there is no continue statement in statement4 (see below), but the latter method is rarely used.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// while statement

statement1;
while (statement2) {
  statement1;
}

// do...while statement

do {
  statement1;
} while (statement2);

These two methods are also equivalent when there is no continue statement in statement1.

1
2
3
4
5
6
7
while (1) {
  // do something...
}

for (;;) {
  // do something...
}

Both of these methods loop indefinitely. (You can use break (see below) to exit.)

It can be seen that the three statements can replace each other, but generally, the choice follows these principles:

  1. When there is a fixed increment step during the loop (most commonly enumeration), use the for statement;
  2. When only the termination condition of the loop is known, use the while statement;
  3. When using a while statement, if the loop body must be executed before checking the condition, use the do...while statement. This is rarely used; a common scenario is user input.

break and continue Statements

The break statement is used to exit a loop.

The continue statement is used to skip the rest of the loop body. The following example shows the use of continue in a do...while statement:

1
2
3
4
5
6
do {
  // do something...
  continue;  // equivalent to goto END;
// do something...
END:;
} while (statement);

Both break and continue statements can be used in the loop bodies of all three loop statements.

Generally, break and continue statements are used to make the logic of the code clearer, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// Logic is less clear, with complex brace nesting

for (int i = 1; i <= n; ++i) {
  if (i != x) {
    for (int j = 1; j <= n; ++j) {
      if (j != x) {
        // do something...
      }
    }
  }
}

// Logic is clearer, with simple and straightforward brace nesting

for (int i = 1; i <= n; ++i) {
  if (i == x) continue;
  for (int j = 1; j <= n; ++j) {
    if (j == x) continue;
    // do something...
  }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// The for statement condition is complex and does not reflect the essence of "enumeration"

for (int i = l; i <= r && i % 10 != 0; ++i) {
  // do something...
}

// The for statement is used for enumeration, and break is used for "until when"

for (int i = l; i <= r; ++i) {
  if (i % 10 == 0) break;
  // do something...
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Statements are repeated, and the order is unnatural

statement1;
while (statement3) {
  statement2;
  statement1;
}

// No repeated statements, and the order is natural

while (1) {
  statement1;
  if (!statement3) break;
  statement2;
}