MCS-201 · Unit 3

Decision and Loop Control Statements

Section 4 of 7

Block 1 · Unit 3Decision and Loop Control Statements8 min · Section 4 of 7

3.4The while and do-while Loops

Covers IGNOU MCS-201 §3.3, 3.3.1, 3.3.2

A loop repeats a block of statements. The while and do–while loops repeat as long as a condition stays true, which suits tasks where the number of repetitions is not known in advance, such as reading input until the user enters 0.

The while loop

Syntax
while (condition) {
    body
}

The condition is tested before each repetition. If it is true, the body runs and the condition is tested again; when it becomes false, control moves to the statement after the loop. If the condition is false at the start, the body never runs. Because the test happens on entry, while is called an entry-controlled loop.

Every loop needs something in its body that eventually makes the condition false; otherwise it is an infinite loop.

Flowchart of a while loop

The do-while loop

Syntax
do {
    body
} while (condition);

The body runs first and the condition is tested after each repetition, so the body always runs at least once. do–while is therefore an exit-controlled loop. Note the semicolon after while (condition).

It suits tasks that must happen at least once, such as showing a menu and then asking whether to show it again.

while versus do-while

Aspectwhiledo-while
Test positionBefore the body (entry-controlled)After the body (exit-controlled)
Minimum executions01
Semicolon after while (...)No (would make an empty loop)Required
Typical useRead until a sentinel value, unknown countMenus, input validation that must run once

Key takeaways

  • while is entry-controlled: the test comes first, so the body may run zero times.
  • do–while is exit-controlled: the body runs first, so it runs at least once.
  • The body must change something that eventually makes the condition false.
  • printf prints exactly what its format says, including spaces.

Practice questions

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

Question 1 · multiple choice

How many times does the body run in int i = 10; while (i < 5) { i++; }?

Show answer

Answer: A. 0

The test 10 < 5 is false before the first repetition, so the body never runs. A do–while would run once.

Question 2 · multiple choice

How many times does the body run in int i = 10; do { i++; } while (i < 5);?

Show answer

Answer: B. 1

The body runs once before the first test; then 11 < 5 is false and the loop ends.

Question 3 · multiple choice

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

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

int main(void)
{
    int i = 1;
    do {
        printf("%d", i);
    } while ((i = i - 1));
    printf("\n");
    return 0;
}
Show answer

Answer: C. 1

The body prints 1; then i = i - 1 sets i to 0, and the value 0 ends the loop. The 0 is never printed.

Question 4 · multiple choice

Which loop is called exit-controlled?

Show answer

Answer: D. do–while

do–while tests its condition at the exit of each repetition; while and for test on entry. switch is not a loop.

Question 5 · multiple choice

What does this program print?

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

int main(void)
{
    int n = 3;
    while (n > 0) {
        printf("%d ", n);
        n--;
    }
    printf("Go\n");
    return 0;
}
Show answer

Answer: B. 3 2 1 Go

The loop prints 3, 2 and 1; when n becomes 0 the test 0 > 0 fails, so 0 is not printed.

Question 6 · short answer

Write a while loop that adds the digits of a positive integer n (for example 472 → 13), and trace it for 472.

Show worked answer

int sum = 0; while (n > 0) { sum = sum + n % 10; n = n / 10; }. n % 10 gives the last digit and n / 10 removes it. Trace: n = 472 → sum = 2, n = 47; → sum = 2 + 7 = 9, n = 4; → sum = 9 + 4 = 13, n = 0; the test 0 > 0 fails. Result: 13.

↑ ↓ move↵ openTab switch type