Comprehensive guide to C++ looping structures — while, do-while, and for loops — with clear syntax, annotated examples, menu-driven program patterns, and practical tips for beginners.
What is a do-while loop?
A do-while loop executes the code at least once, then checks the condition.
do {
// code to execute
} while (condition);
👉 Key point: The condition is checked after the loop body.
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
return 0;
}
Output
1 2 3 4 5
Explanation:
i starts at 1i increases by 1i becomes greater than 5#include <iostream>
using namespace std;
int main() {
int count = 1;
do {
cout << "Hello\n";
count++;
} while (count <= 3);
return 0;
}
Explanation:
#include <iostream>
using namespace std;
int main() {
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i <= 5);
cout << "Sum = " << sum;
return 0;
}
Output
Sum = 15
#include <iostream>
using namespace std;
int main() {
int choice;
do {
cout << "\n1. Say Hello";
cout << "\n2. Say Bye";
cout << "\n3. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
if (choice == 1)
cout << "Hello!\n";
else if (choice == 2)
cout << "Bye!\n";
} while (choice != 3);
return 0;
}
Why do-while here?
#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 vs do-while| Feature | while | do-while |
|---|---|---|
| Condition checked | Before loop | After loop |
| Runs at least once | ❌ No | ✅ Yes |
| Best for | Unknown executions | Menus / user input |
do-while✅ When the loop must execute at least once ✅ For menus, password checks, user input
for (initialization; condition; update) {
// code to execute in each iteration
}
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) { // i starts at 1, loop until i <= 5, i increments by 1
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 5
✅ Explanation:
int i = 1 → start counting from 1i <= 5 → stop after 5i++ → increment i by 1 each time#include <iostream>
using namespace std;
int main() {
for (int i = 2; i <= 10; i += 2) { // i increases by 2 each time
cout << i << " ";
}
return 0;
}
Output:
2 4 6 8 10
✅ Explanation:
i += 2 means add 2 to i each time, so we get even numbers.#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i; // sum = sum + i
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 55
✅ Explanation:
sum += i adds each number to the total sum.sum contains the total.for loop is great for known number of iterations.Question: Write a C++ program that asks the user to enter a number and then displays its multiplication table from 1 to 10 using a for loop.
Example:
7, the program should display:7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70
Requirements:
cin to take input from the user.for loop to generate the multiplication table.#include <iostream>
using namespace std;
int main() {
int number;
// Ask the user for the number
cout << "Enter a number to display its multiplication table: ";
cin >> number;
// Loop from 1 to 10
for (int i = 1; i <= 10; i++) {
cout << number << " x " << i << " = " << number * i << endl;
}
return 0;
}
cin >> number; → User inputs the number for which they want the table.for (int i = 1; i <= 10; i++) → Loop runs from 1 to 10.number * i calculates each multiplication.cout prints the result in a readable format.Example Output (if user enters 5):
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Question:
Write a C++ program that asks the user to enter a positive number N and then prints all even numbers from 1 to N using a for loop.
Example:
10Even numbers from 1 to 10 are: 2 4 6 8 10
Requirements:
cin to take input from the user.for loop to iterate from 1 to N.if statement to check if a number is even.#include <iostream>
using namespace std;
int main() {
int n;
// Ask the user for a number
cout << "Enter a positive number: ";
cin >> n;
cout << "Even numbers from 1 to " << n << " are: ";
// For loop to print even numbers
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) { // Check if number is even
cout << i << " ";
}
}
cout << endl;
return 0;
}
n.for (int i = 1; i <= n; i++) runs from 1 to n.if (i % 2 == 0) checks if the number is even.Enter a positive number: 10
Even numbers from 1 to 10 are: 2 4 6 8 10
Question:
Write a C++ program that asks the user to enter a positive number N and then prints a right-angled triangle star pattern with N rows using nested for loops.
Example:
5*
**
***
****
*****
Requirements:
cin to take input from the user.Use a nested for loop:
#include <iostream>
using namespace std;
int main() {
int n;
// Ask the user for the number of rows
cout << "Enter the number of rows: ";
cin >> n;
// Outer loop for each row
for (int i = 1; i <= n; i++) {
// Inner loop to print stars in each row
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl; // Move to next row
}
return 0;
}
n.for (int i = 1; i <= n; i++) → Controls the rows.for (int j = 1; j <= i; j++) → Prints stars in each row.Enter the number of rows: 5
*
**
***
****
*****