ProgramGuru

C for, while and Loop Control

C for, while and Loop Control

A school needs room labels from one to three. Repeating the same print statement by hand makes longer sequences hard to maintain.

A loop repeats a body while its continuation rule holds. for places initialization, a condition and an update together; while checks before the body; do-while checks after at least one iteration.

  1. Explain

    Trace the initialization, test, body and update of a bounded loop.

  2. Trace

    Print rooms one to five but skip room three using continue.

  3. Apply

    Identify the stopping condition and avoid stepping beyond the last item.

Loops: follow the data

  1. Start

    A school needs room labels from one to three. Repeating the same print statement by hand makes longer sequences hard to maintain.

  2. Operation

    A loop repeats a body while its continuation rule holds. for places initialization, a condition and an update together; while checks before the body; do-while checks after at least one iteration.

  3. Result

    Room 1 Room 2 Room 3

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

In for (int room = 1; room <= 3; room++), initialization runs once, the condition is checked before each body, and the update follows each completed body. The last failed condition stops the loop.

break leaves the nearest enclosing loop or switch. continue skips the remaining body and proceeds to the next iteration’s appropriate update or test. Ensure a while loop still changes the values that allow termination.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
int main(void) {
  for (int room = 1; room <= 3; room++) {
    printf("Room %d\n", room);
  }
  return 0;
}

Expected output

Room 1
Room 2
Room 3
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  for (int room = 1; room <= 3; room++) {
    printf("Room %d\n", room);
  }
  return 0;
}

room takes values 1, 2 and 3, each satisfying room <= 3.

After printing Room 3, the update makes room 4. The false condition stops the loop without printing a fourth line.

Follow the worked calculation

  1. Read the starting values

    room takes values 1, 2 and 3, each satisfying room <= 3.

  2. Perform the operation

    After printing Room 3, the update makes room 4. The false condition stops the loop without printing a fourth line.

  3. Check the observable result

    Room 1 Room 2 Room 3

    Room 1
    Room 2
    Room 3
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  for (int room = 1; room <= 3; room++) {
    printf("Room %d\n", room);
  }
  return 0;
}

Print rooms one to five but skip room three using continue.

Worked answer

continue skips printf for room three but still reaches the for loop’s update. The sequence advances to four and eventually terminates after five.

C17 · complete program

#include <stdio.h>
int main(void) {
  for (int room = 1; room <= 5; room++) {
    if (room == 3) continue;
    printf("Room %d\n", room);
  }
  return 0;
}

Expected output

Room 1
Room 2
Room 4
Room 5
Run in C compiler →

Recognize the important boundaries

Using <= count for an array indexed from zero accesses one element too many. Use an index strictly less than the element count.

An accidental semicolon after the loop header creates an empty body. A loop also needs a termination argument; increasing a signed counter forever is not safe.

Check your understanding

Choose the answer, then compare the reasoning for every option.

When is room++ evaluated in this for loop?
Choose one answer. You can try again.

Answer and explanation

After the body, before the next condition check.

Correct. That order advances the loop between printed labels.

Use the idea independently

A loop repeats a body while its continuation rule holds. for places initialization, a condition and an update together; while checks before the body; do-while checks after at least one iteration.

Continue with indexed sequences.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Functions, Parameters and Return Values →