C Programming • C Basics, Operators, Loops
C Programming / Control Structures: do-while

Control Structures: do-while

Notes 2 C Basics, Operators, Loops

Definition: The do-while loop is an exit-controlled loop in which the loop body executes first and the condition is checked afterward.

Notes

do-while Loop in C

Definition: The do-while loop is an exit-controlled loop in which the loop body executes first and the condition is checked afterward.

💡 Example: A program can ask the user to enter a number and continue asking until the user enters 0.

Syntax

do
{
    statements;
}
while (condition);

How do-while Works

Execute loop body ↓ Check condition ↓ Condition TRUE? ↓ ↓ Yes No ↓ ↓ Repeat Exit loop

Example

The following program prints the value of i and increases it until i becomes greater than 5.

#include <stdio.h>

int main()
{
    int i = 1;

    do
    {
        printf("%d ", i);
        i++;                // move to the next number
    }
    while (i <= 5);

    return 0;
}

Output

1 2 3 4 5

Why is do-while Called an Exit-Controlled Loop?

The condition of a do-while loop is checked after the loop body executes. Therefore, the body always executes at least once.

💡 Example: Even if the condition is initially false, the statements inside the do block are executed once before the condition is checked.

Real-World Example

A menu-driven program often needs to display the menu at least once before asking whether the user wants to continue.

💡 Example: Show menu → take choice → perform task → ask again.

Practical Example — Input Until Zero

Problem Statement

Write a C program that repeatedly accepts numbers from the user and displays the entered number until the user enters 0.

Learning Outcomes

  • Understand the working of the do-while loop.
  • Use a sentinel value to terminate repeated input.
  • Understand the difference between entry-controlled and exit-controlled loops.

Hint

Read a number inside the loop, display it when it is not zero, and continue while the number is not 0.

Theory

A do-while loop executes its body first and checks the condition afterward. Therefore, the loop body is guaranteed to execute at least once.

Program

#include <stdio.h>

int main()
{
    int num;

    do
    {
        printf("Enter a number (0 to stop): ");
        scanf("%d", &num);

        if (num != 0)              // 0 is the sentinel, so skip printing it
            printf("You entered: %d
", num);

    } while (num != 0);             // condition is checked after the body

    printf("Loop terminated.
");

    return 0;
}

Expected Output

Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 8
You entered: 8
Enter a number (0 to stop): 0
Loop terminated.

Note

Unlike a while loop, a do-while loop always executes its body at least once because the condition is checked after the body.

while vs do-while

while do-while
Entry-controlled loop. Exit-controlled loop.
Condition is checked before the body. Condition is checked after the body.
May execute zero times. Executes at least once.
Syntax starts with while. Syntax starts with do and ends with while.

Important Points for Exam

  • do-while is an exit-controlled loop.
  • The loop body executes before the condition is checked.
  • The body executes at least once.
  • A semicolon is required after the while(condition).
  • It is useful for menus and input-driven programs.
🎯 Easy Rule:

do → Execute First → Check Condition → Repeat or Exit

Quick Revision

Concept Remember
do-while Exit-controlled loop.
First step Execute the loop body.
Second step Check the condition.
Minimum execution At least once.

Important Exam Questions

Short Answer Questions

  1. What is a do-while loop in C?
  2. Why is do-while called an exit-controlled loop?
  3. Write the syntax of a do-while loop.
  4. How is do-while different from while?
  5. Why does a do-while loop execute at least once?
  6. What is a sentinel value?

Long Answer Questions

  1. Explain the do-while loop with syntax and suitable example.
  2. Write a C program using do-while to accept numbers until the user enters 0.
  3. Differentiate between while and do-while loops.
🎥 Recommended Learning

Watch a beginner-friendly explanation of the do-while loop in C.

▶ Watch: do-while Loop in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for the do-while loop will be provided here.

🧠 Mind Map

Use the mind map for quick revision of do, condition, repetition and exit.