Programming Fundamentals in C++
Lab -9: Loops
Lab Objectives
Lab Objectives
After completing this lab, students will be able to:
1. Understand the concept and need of loops in C++.
2. Implement for, while, do-while, and nested loops.
3. Use break and continue statements to control loops.
4. Apply loops to real-life problem-solving situations.
1. Concept of Loops
A loop allows executing a block of code multiple times as long as a condition is true.
Why Loops Are Used
• To perform repetitive tasks efficiently.
• To avoid redundant code.
• To make programs dynamic and interactive.
2. Types of Loops in C++
Loop Type Description When to Use
for loop Executes code a specific number of times. When the number of iterations is known.
while loop Repeats as long as a condition is true. When the number of iterations is not
known.
do-while Executes code at least once, then checks the When you must run the loop once no
loop condition. matter what.
nested loop A loop inside another loop. When working with patterns, tables, or
grids.
3. Syntax and Examples
Example 1: For Loop Explanation:
#include <iostream> • int i = 1; → Initializes the counter variable.
using namespace std; • i <= 5; → Loop continues as long as this condition is true.
int main() { • i++ → Increases the value of i after every iteration.
cout << "Counting from 1 to 5:\n";
for (int i = 1; i <= 5; i++) { • cout << i; → Prints each number on the same line.
cout << i << " ";
} Output:
return 0; 12345
}
Example 2: While Loop Explanation:
#include <iostream> • The variable i starts at 1.
using namespace std; • Before each iteration, the condition i <= 5 is checked.
int main() { • If true, the loop body executes.
int i = 1;
while (i <= 5) { • When i becomes 6, the condition is false → loop stops.
cout << "Number: " << i << endl;
i++; Output:
}
return 0; Number: 1
} Number: 2
Number: 3
Number: 4
Number: 5
Example 3: Do-While Loop Explanation:
#include <iostream> • do block executes first, even before checking the condition.
using namespace std; • After each iteration, the condition i <= 3 is checked.
int main() { • The loop runs three times, then stops.
int i = 1;
do {
cout << "Hello, iteration " << i << endl; Output:
i++;
} while (i <= 3); Hello, iteration 1
return 0; Hello, iteration 2
} Hello, iteration 3
Example 4: Nested Loop Explanation:
#include <iostream> • Outer loop (i) runs 3 times.
using namespace std; • For each value of i, the inner loop (j) runs 3 times.
int main() { • So total iterations = 3 × 3 = 9.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) { • Prints coordinates like a grid (3 rows, 3 columns).
cout << "(" << i << "," << j << ") ";
} Output:
cout << endl;
} (1,1) (1,2) (1,3)
return 0; (2,1) (2,2) (2,3)
} (3,1) (3,2) (3,3)
Example 5: Break and Continue Explanation:
#include <iostream> • When i == 3, continue skips printing 3.
using namespace std; • When i == 5, break exits the loop completely.
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skips 3 Output:
if (i == 5) break; // stops loop at 5 124
cout << i << " ";
}
return 0;
}
4. Real-Life Scenario-Based Lab Report Tasks
Task 1: Attendance Counter (For Loop)
Scenario:
A teacher wants to automatically mark roll numbers of students.
Explanation:
The loop runs n times, printing roll numbers from 1 to n.
Task 2: Savings Growth Calculator (While Loop)
Scenario:
A person invests $1000 at 10% interest each year. Calculate how many years it takes to double the money.
Explanation:
Each iteration adds 10% of the amount until it doubles.
The while loop continues until amount >= 2000.
Task 3: Amusement Park Entry (Do-While Loop)
Scenario:
A visitor can re-enter the park as long as they have a valid pass.
Explanation:
The loop executes at least once before checking if the visitor wants to continue.
Task 4: Multiplication Table Generator (Nested Loops)
Scenario:
Display multiplication tables from 2 to 5.
Explanation:
• Outer loop → changes the table number.
• Inner loop → multiplies by 1–10.
• Prints all tables from 2 to 5.
6. Viva Questions
1. What is a loop and why do we use it?
2. Difference between for, while, and do-while.
3. What is an entry-controlled vs exit-controlled loop?
4. What is a nested loop?
5. Can we use break and continue together?
6. What happens if the loop condition never becomes false?
7. What are real-life examples where loops are useful?
***Good Luck***