Imagine having to write "I will not throw paper airplanes" 100 times on the blackboard. Would you rather write the same line 100 times, or tell someone "Write this line 100 times"? That's exactly what loops do - they automate repetition!
The FOR loop is like a precise counter - perfect when you know exactly how many times to repeat.
// Count from 1 to 10
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
// Output: 1 2 3 4 5 6 7 8 9 10
// Count backwards
for (int i = 10; i >= 1; i--) {
cout << i << " ";
}
// Output: 10 9 8 7 6 5 4 3 2 1
// Count by 2s
for (int i = 0; i <= 20; i += 2) {
cout << i << " ";
}
// Output: 0 2 4 6 8 10 12 14 16 18 20
WHILE loops are like a security guard asking "Do you have permission?" - they keep going as long as the answer is yes!
// Basic while loop
int count = 0;
while (count < 5) {
cout << "Count is: " << count << endl;
count++;
}
// User-controlled loop
char answer = 'y';
while (answer == 'y' || answer == 'Y') {
cout << "Continue? (y/n): ";
cin >> answer;
}
// Input validation
int age;
cout << "Enter your age (1-120): ";
cin >> age;
while (age < 1 || age > 120) {
cout << "Invalid age! Try again: ";
cin >> age;
}
DO-WHILE is like trying food before deciding if you like it - you do it first, then decide whether to continue.
int choice;
do {
cout << "\n=== MENU ===" << endl;
cout << "1. Play Game" << endl;
cout << "2. View Scores" << endl;
cout << "3. Settings" << endl;
cout << "4. Exit" << endl;
cout << "Choose (1-4): ";
cin >> choice;
switch(choice) {
case 1:
cout << "Starting game..." << endl;
break;
case 2:
cout << "High scores..." << endl;
break;
case 3:
cout << "Settings..." << endl;
break;
case 4:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);
Nested loops are like a clock - the minute hand completes a full rotation for each hour!
// Print a rectangle of stars
for (int i = 0; i < 4; i++) { // Rows
for (int j = 0; j < 6; j++) { // Columns
cout << "* ";
}
cout << endl;
}
/* Output:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
*/
// Multiplication table
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
cout << i * j << "\t";
}
cout << endl;
}
/* Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
*/
BREAK and CONTINUE are like emergency controls - BREAK is the emergency exit, CONTINUE is the skip button!
// Break example - find first number divisible by 7
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) {
cout << "First number divisible by 7: " << i << endl;
break; // Exit loop
}
}
// Continue example - print only odd numbers
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}
// Output: 1 3 5 7 9
Infinite loops are like a hamster wheel - they keep going forever unless you stop them!
Create a number guessing game that:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int secretNumber = rand() % 100 + 1; // 1-100
int guess;
int attempts = 0;
cout << "I'm thinking of a number between 1 and 100!" << endl;
// TODO: Use a do-while loop for the game
// Hint: Keep looping while guess != secretNumber
return 0;
}
do {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess > secretNumber) {
cout << "Too high! Try again." << endl;
} else if (guess < secretNumber) {
cout << "Too low! Try again." << endl;
} else {
cout << "Congratulations! You got it!" << endl;
cout << "It took you " << attempts << " guesses." << endl;
}
} while (guess != secretNumber);
Create a program that prints these patterns using nested loops:
Pattern 1: Pattern 2: Pattern 3: * * * * * * 1 * * * * * * 2 3 * * * * * * 4 5 6 * * * * * * 7 8 9 10 * * * * * * 11 12 13 14 15
Use nested loops where the inner loop runs from 0 to i
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
// Calculate class average with input validation
int numStudents;
double total = 0;
double grade;
// Get number of students with validation
do {
cout << "How many students? (1-50): ";
cin >> numStudents;
} while (numStudents < 1 || numStudents > 50);
// Input grades for each student
for (int i = 1; i <= numStudents; i++) {
do {
cout << "Enter grade for student " << i << " (0-100): ";
cin >> grade;
if (grade < 0 || grade > 100) {
cout << "Invalid grade! Try again." << endl;
}
} while (grade < 0 || grade > 100);
total += grade;
}
// Calculate and display average
double average = total / numStudents;
cout << "\nClass average: " << average << endl;
// Determine class performance
if (average >= 90) {
cout << "Outstanding class performance!" << endl;
} else if (average >= 80) {
cout << "Good class performance." << endl;
} else if (average >= 70) {
cout << "Satisfactory performance." << endl;
} else {
cout << "Class needs improvement." << endl;
}