do-while Loop in C
An ATM always shows the menu first, and only then asks if you want to continue. A form shows the input box first, and only then checks if your answer is valid. In both cases, the action must happen at least once before any condition is checked.
That's exactly what the do-while loop does. In this lesson you will learn its syntax and build a menu-driven program and an input-validation loop.
What You Will Learn
- The syntax and flowchart of do-while.
- Why it always runs at least once.
- A menu-driven program with do-while.
- Repeating input until it is valid.
- The semicolon rule of do-while.
do-while Loop in C Explained
Exam definition: The do-while loop is an exit-controlled loop: the body runs first and the condition is checked after each iteration. Therefore the body executes at least once, even if the condition is false from the start.
Memory trick: do-while = "Do it first, ask later." Like tasting food: you taste first, then decide if you want more.
Syntax of do-while Loop in C
do
{
/* body: runs at least once */
} while (condition); /* semicolon is required here */
Flowchart of the do-while loop
+--> [ body ]
| |
| v
+-- < condition? > (True: repeat)
| False
v
[ exit loop ]
Example 1: Menu Driven Program Using do-while
#include <stdio.h>
int main(void)
{
int choice, balance = 1000;
do
{
printf("Menu: 1.Balance 2.Deposit 500 3.Exit\n");
printf("Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("Balance: %d\n", balance); break;
case 2: balance += 500; printf("Deposited.\n"); break;
case 3: printf("Goodbye!\n"); break;
default: printf("Invalid choice\n");
}
} while (choice != 3);
return 0;
}
Output
Menu: 1.Balance 2.Deposit 500 3.Exit Choice: 1 Balance: 1000 Menu: 1.Balance 2.Deposit 500 3.Exit Choice: 2 Deposited. Menu: 1.Balance 2.Deposit 500 3.Exit Choice: 1 Balance: 1500 Menu: 1.Balance 2.Deposit 500 3.Exit Choice: 3 Goodbye!
Code Explanation
- The menu is printed before any check, because the body of a do-while runs first.
- The user's choice is handled with a switch.
while (choice != 3);is checked at the end. If the user didn't choose 3, the menu appears again.- When the user enters 3, "Goodbye!" is printed, the condition becomes false, and the loop ends.
- A while loop would need the menu code written twice (once before the loop), or a trick value for
choice. do-while is cleaner here.
How the do-while Loop Works
- Run the body.
- Check the condition.
- If true, go back to step 1. If false, leave the loop.
- Because the check comes after the body, the body always runs at least once: one more time than a while loop would in the case where the condition starts false.
Example 2: Repeat Input Until It Is Valid
#include <stdio.h>
int main(void)
{
int marks;
do
{
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks < 0 || marks > 100)
printf("Invalid! Please try again.\n");
} while (marks < 0 || marks > 100);
printf("You entered %d\n", marks);
return 0;
}
Output
Enter marks (0-100): 150 Invalid! Please try again. Enter marks (0-100): 85 You entered 85
The program must ask at least once, and keeps asking as long as the value is out of range. This is the classic use of do-while.
Important Points to Remember
- do-while is exit-controlled.
- The body runs at least once.
- A semicolon is required after
while (condition). - Best for menus and input validation.
- The loop variable used in the condition must be set inside the body.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Missing ; after while (...) | Syntax error | } while (cond); |
| Condition never changes inside the body | Infinite loop | Read or update the variable in the body |
| Using do-while when the body must not run for invalid start values | Body runs once wrongly | Use a while loop |
Wrong condition direction (choice == 3) | Stops after one menu, or never stops | Loop while the user hasn't chosen exit: choice != 3 |
do-while Loop Practice Questions
- Write the syntax of the do-while loop.
- Why is do-while called an exit-controlled loop?
- How many times does
int i = 10; do { i++; } while (i < 5);run? - Give two real situations where do-while is the best choice.
- Where is a semicolon needed in a do-while loop?
Show answers
do { body } while (condition);- Because the condition is checked at the exit (after the body).
- Once.
- Menu-driven programs and validating user input (also: "Do you want to continue? (y/n)").
- After the closing bracket of the condition:
while (condition);
Coding Exercise
Keep reading numbers and adding them to a total until the user enters 0. Then print the total. Use do-while.
Show solution
#include <stdio.h>
int main(void)
{
int n, total = 0;
do
{
printf("Enter a number (0 to stop): ");
scanf("%d", &n);
total += n;
} while (n != 0);
printf("Total = %d\n", total);
return 0;
}
Enter a number (0 to stop): 10 Enter a number (0 to stop): 25 Enter a number (0 to stop): 5 Enter a number (0 to stop): 0 Total = 40
Quick Quiz: do-while Loop in C (MCQ)
-
The do-while loop is:
A) Entry-controlled B) Exit-controlled C) Not a loop D) Infinite always
-
Minimum number of times a do-while body runs:
A) 0 B) 1 C) 2 D) Depends
-
Which is correct?
A)
do { } while (x)B)do { } while (x);C)do while (x) { }D)do (x) { } -
Output:
int i = 5; do { printf("%d ", i); i++; } while (i < 3);A) Nothing B) 5 C) 5 6 D) 3 4
-
Best loop for a menu that must appear at least once:
A) for B) while C) do-while D) goto
Show answers
- B: exit-controlled.
- B: 1.
- B:
do { } while (x); - B: 5 (runs once, then 6 < 3 is false).
- C: do-while.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Syntax | do { body } while (condition); |
|---|---|
| Type | Exit-controlled |
| Minimum runs | 1 |
| Semicolon | Required after while (...) |
| Best for | Menus, input validation |
Frequently asked questions
- Why does do-while run at least once?
- Its condition is checked after the body, so the body runs before any check.
- Why is do-while good for menu programs?
- The menu must be shown at least once, and repeated until the user chooses exit.
- Is the semicolon after while compulsory in do-while?
- Yes.
} while (condition);
Remember
- do-while = do first, ask later (taste, then decide).
- The only loop with a semicolon after while().
Next Topic
Next lesson: while vs do-while in C. You will compare while and do-while side by side, a favourite exam question.