MCS-201 · Unit 3

Decision and Loop Control Statements

Section 5 of 7

Block 1 · Unit 3Decision and Loop Control Statements7 min · Section 5 of 7

3.5The for Loop

Covers IGNOU MCS-201 §3.3.3

Most loops count: from 1 to n, over every item, a fixed number of times. The for loop gathers the three parts of a counting loop (start, test, step) into one line, which makes such loops easy to read and hard to get wrong.

Syntax and order of execution

Syntax
for (initialisation; condition; update) {
    body
}

The for loop executes its parts in this order:

  1. Initialisation: runs once, before anything else.
  2. Condition: tested before every repetition; if false, the loop ends.
  3. Body: runs when the condition is true.
  4. Update: runs after the body; then control returns to step 2.
Equivalent while loop
initialisation;
while (condition) {
    body
    update;
}

Flowchart of a for loop

Optional parts

Any of the three parts may be left empty, but both semicolons must stay:

FormMeaning
for (; i <= n; i++)No initialisation: i is set before the loop
for (i = 1; i <= n; )No update: i changes inside the body
for (i = 1; ; i++)No condition: treated as always true; the body must break
for (;;)Infinite loop; exits only by break, return or goto

Several variables can be initialised or updated using the comma operator: for (i = 0, j = n; i < j; i++, j--). Since C99, the loop variable can be declared in the loop itself: for (int i = 0; i < n; i++); it then exists only inside the loop.

Choosing a loop

SituationBest loop
The number of repetitions is known (1 to n, 10 times)for
Repeat until something happens; may be zero times (read until 0)while
Must run at least once (menu, input validation)do–while

All three can express any loop; the choice is about clarity.

Key takeaways

  • for (init; condition; update) puts all the counting logic in one line.
  • Order: init once, then condition → body → update, repeated.
  • Any part may be empty; for (;;) is an infinite loop.
  • Use for for counted loops, while for condition-driven loops, do–while when the body must run once.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In for (A; B; C) D;, in which order are the parts first executed?

Show answer

Answer: B. A, B, D, C

Initialisation A runs once, then the condition B, then the body D, then the update C, after which B is tested again. The update never runs before the first body.

Question 2 · multiple choice

How many times does printf run in for (int i = 0; i < 5; i += 2) printf("*");?

Show answer

Answer: C. 3

i takes the values 0, 2 and 4; at 6 the test fails. So three stars are printed.

Question 3 · multiple choice

What does this program print?

C · for_semicolon.c
#include <stdio.h>

int main(void)
{
    int i;
    for (i = 1; i <= 3; i++);
    printf("%d\n", i);
    return 0;
}
Show answer

Answer: D. 4

The semicolon after the header is the loop’s empty body; the loop just counts i up to 4, then printf runs once and prints 4.

Question 4 · multiple choice

Which for header creates an infinite loop?

Show answer

Answer: A. for (;;)

An empty condition is treated as true, so for (;;) never ends on its own. for (;;;) is a syntax error.

Question 5 · multiple choice

What does this program print (Check Your Progress 2)?

C · three.c
#include <stdio.h>

int main(void)
{
    int i;
    for (i = 0; i < 3; i++)
        printf("%d ", i);
    printf("\n");
    return 0;
}
Show answer

Answer: C. 0 1 2 (each followed by a space)

i takes 0, 1 and 2, and the format "%d " prints a space after each. i < 3 stops before 3.

Question 6 · short answer

Rewrite for (i = 10; i >= 1; i--) printf("%d ", i); as a while loop and state what it prints.

Show worked answer

i = 10; while (i >= 1) { printf("%d ", i); i--; }. The initialisation moves before the loop, the condition stays in the while, and the update becomes the last statement of the body. It prints 10 9 8 7 6 5 4 3 2 1 (each number followed by a space).

↑ ↓ move↵ openTab switch type