Displaying the numbers 1 to 100 with one hundred separate output statements is possible but absurd. A loop lets a single statement be carried out again and again, and it is the structure that gives a computer its power.

The Idea of Repetition

Definition: Iteration. The repeated execution of a group of statements; one execution of the group is called one iteration or one pass of the loop.

Every loop has four parts: initialization, which sets the starting value of the control variable; the condition, which decides whether another pass is made; the body, which is the group of statements repeated; and the update, which changes the control variable so that the condition eventually becomes false.

The Three Loops of C

C provides three loop statements. Table 2.3 compares them.

Table 2.3: Loop statements in C

Loop Condition checked Typical use
for Before each pass The number of passes is known in advance
while Before each pass Repeat as long as a condition holds
do-while After each pass The body must run at least once

The following three fragments all display the numbers 1 to 5.

for (i = 1; i <= 5; i++)  i = 1;                i = 1;
{                         while (i <= 5)        do
    printf("%d ", i);     {                     {
}                             printf("%d ", i);     printf("%d ", i);
                              i++;                  i++;
                          }                     } while (i <= 5);

The for loop gathers initialization, condition and update on one line, which makes the control of the loop easy to see.

Stacking and Nesting

Control structures can be combined in two ways. Stacking places one structure after another, so that the second begins when the first has finished. Nesting places one structure inside another.

Definition: Nested structure. A control structure placed inside the body of another control structure.

When one loop is nested in another, the inner loop runs completely for every single pass of the outer loop. If the outer loop makes 3 passes and the inner loop makes 4, the innermost statement is carried out 3 × 4 = 12 times. A selection statement may also be nested inside a loop, for example to count only the even numbers among the values processed.

Example 2.3: A rectangle of stars

Problem: Display a rectangle of stars with 3 rows and 4 columns.

Solution:

  1. The outer loop counts the rows, from 1 to 3.
  2. For each row, the inner loop displays 4 stars.
  3. After the inner loop finishes, a newline ends the row.
#include <stdio.h>

int main(void)
{
    int row, col;

    for (row = 1; row <= 3; row++)
    {
        for (col = 1; col <= 4; col++)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Output:

* * * *
* * * *
* * * *

Result: The star is displayed 3 × 4 = 12 times. The newline belongs to the outer loop, so it is displayed 3 times, once at the end of each row.

Common mistakes

  • Forgetting the update, so that the condition never becomes false and the loop never ends.
  • Using the same control variable for the outer and the inner loop.
  • Starting or stopping one pass too early or too late, known as an off-by-one error.

Key takeaways

  • A loop has initialization, a condition, a body and an update.
  • for and while test the condition before the body; do-while tests it after.
  • Stacking places structures one after another; nesting places one inside another.
  • An inner loop completes all its passes for each pass of the outer loop.

Practice questions

  1. Which C loop always carries out its body at least once?
  2. An outer loop runs 5 times and its inner loop runs 6 times. How many times does the innermost statement run?
  3. What happens if the update of a loop is left out?

Answers to the practice questions

  1. The do-while loop, because its condition is checked after the body.
  2. 5 × 6 = 30 times.
  3. The control variable never changes, the condition stays true, and the loop does not end.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II