Learn the C++ do-while loop with beginner-friendly syntax, examples, and comparison to while loops. Understand when to use do-while in C++ programs and exercises.
By the end of this lecture, students will be able to:
do-while loop.while and do-while.do-while.do-while Loop?A do-while loop repeats a block of code while a condition is true.
The important difference is that the do-while loop executes its body at least once, even if the condition is false at the beginning.
Imagine a university ATM menu. When a user inserts their card, the ATM displays a menu. After performing an operation, the user may choose to display the menu again.
The menu must appear at least once before the program can ask whether the user wants to continue.
do-while Loopdo
{
// Statements to execute
}
while (condition);
Important: Notice the semicolon ; after while (condition).
The execution order is:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
do
{
cout << "Welcome to Ghazi University!" << endl;
i++;
}
while (i <= 5);
return 0;
}
int i = 1; initializes the counter.do marks the beginning of the loop body.cout displays the message.i++ increases the counter by 1.while (i <= 5); checks whether the loop should repeat.Output:
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
"I am learning C++.".i <= 7.i along with the message.#include <iostream>
using namespace std;
int main()
{
int i = 1;
do
{
cout << i << " ";
i++;
}
while (i <= 10);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
The counter starts at 1. The loop prints the current value and increases it by 1. The condition is checked after each repetition. When i becomes 11, the condition is false and the loop stops.
Hint for descending numbers: start from 10, use i >= 1, and decrease the counter using i--.
This is the most important example for understanding do-while.
#include <iostream>
using namespace std;
int main()
{
int i = 10;
do
{
cout << "Value of i = " << i << endl;
i++;
}
while (i <= 5);
return 0;
}
Output:
Value of i = 10
i <= 5 is false.Although the condition was false, the message was displayed once because the condition is checked after executing the loop body.
i = 10 to i = 3. Predict the output before running the program.i <= 12.i++ and explain what happens when the condition is true.Write a program that asks the user to enter a number and then displays the numbers from 1 up to that number.
#include <iostream>
using namespace std;
int main()
{
int n;
int i = 1;
cout << "Enter a number: ";
cin >> n;
do
{
cout << i << " ";
i++;
}
while (i <= n);
return 0;
}
Sample output:
Enter a number: 5
1 2 3 4 5
If the user enters 5, the program displays numbers from 1 to 5.
However, if the user enters 0, the program still displays 1 once. Why? Because do-while executes the loop body before checking the condition.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
int sum = 0;
do
{
sum = sum + i;
i++;
}
while (i <= 5);
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 15
i controls the loop.sum stores the running total.sum = sum + i; adds the current number to the total.i++ moves to the next number.i becomes 6.Current i |
Calculation | sum |
|---|---|---|
| 1 | 0 + 1 | 1 |
| 2 | 1 + 2 | 3 |
| 3 | 3 + 3 | 6 |
| 4 | 6 + 4 | 10 |
| 5 | 10 + 5 | 15 |
A menu-driven program allows a user to select an option. The menu is displayed again until the user chooses to exit.
#include <iostream>
using namespace std;
int main()
{
int choice;
do
{
cout << "\n--- University Menu ---" << endl;
cout << "1. Student Information" << endl;
cout << "2. Display Message" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1)
{
cout << "Student Information Selected" << endl;
}
else if (choice == 2)
{
cout << "Welcome to Ghazi University!" << endl;
}
else if (choice == 3)
{
cout << "Program ended." << endl;
}
else
{
cout << "Invalid choice!" << endl;
}
}
while (choice != 3);
return 0;
}
if-else-if determines which message to display.choice != 3 becomes false, and the loop stops."Admissions Fall 2026".#include <iostream>
using namespace std;
int main() {
int i = 10;
do {
cout << "This will print once\n";
} while (i < 5);
return 0;
}
Explanation:
while loopwhile and do-while| Feature | while loop |
do-while loop |
|---|---|---|
| Condition checked | Before the body | After the body |
| Minimum executions | Zero | One |
| Semicolon after condition | Not required | Required |
| Useful for | Condition-controlled repetition | Menus and actions that must happen at least once |
Compare these programs.
Using while:
int i = 10;
while (i <= 5)
{
cout << i;
}
Output: Nothing.
Using do-while:
int i = 10;
do
{
cout << i;
}
while (i <= 5);
Output:
10
Incorrect:
do
{
cout << "Hello";
}
while (i <= 5)
Correct:
do
{
cout << "Hello";
}
while (i <= 5);
If the condition remains true and the counter never changes, the loop may run indefinitely.
A do-while loop always executes its body at least once, even when its condition is initially false.
Solve Coding Exercises 7–16 in the do-while loop section. Write, run, and test each C++ program to verify the output.
do-while loop executes the body before checking the condition.do-while.Next lecture: The for loop — syntax, counting, sequences, sum calculations, code modification, and coding exercises.