| Course: Programming Fundamentals | Level: Beginner | Language: C++ |
By the end of this lesson, students will be able to:
A loop is a programming structure that repeats a block of code multiple times.
In real life, we often repeat the same task. For example:
Instead of writing the same instructions repeatedly, we can use a loop.
Simple definition: A loop allows us to execute the same instructions repeatedly without writing them again and again.
Suppose we want to display a message five times.
Without a loop:
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to Ghazi University!" << endl;
cout << "Welcome to Ghazi University!" << endl;
cout << "Welcome to Ghazi University!" << endl;
cout << "Welcome to Ghazi University!" << endl;
cout << "Welcome to Ghazi University!" << endl;
return 0;
}
Output:
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Problem: We have written the same instruction five times. What if we need to display it 100 times?
Writing 100 separate cout statements would be unnecessary and difficult to maintain.
We can solve this problem using a loop.
We will use a while loop to display the same message five times.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 5)
{
cout << "Welcome to Ghazi University!" << endl;
i++;
}
return 0;
}
Output:
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
Welcome to Ghazi University!
The output is the same, but the code is shorter and easier to manage.
Let’s understand the program in simple language.
int i = 1;
int means the variable stores an integer.i is the variable name.1 is its initial value.Think of i as a counter that keeps track of how many times we have repeated the task.
while (i <= 5)
The condition checks whether i is less than or equal to 5.
cout << "Welcome to Ghazi University!" << endl;
This statement displays the message.
i++;
This increases i by 1.
For example:
i++;
is equivalent to:
i = i + 1;
Without this update, the condition would remain true indefinitely in this example, creating an infinite loop.
A trace table helps us understand how a program executes step by step.
| Iteration | Value of i |
Condition i <= 5 |
Action |
|---|---|---|---|
| 1 | 1 | True | Display message |
| 2 | 2 | True | Display message |
| 3 | 3 | True | Display message |
| 4 | 4 | True | Display message |
| 5 | 5 | True | Display message |
| End check | 6 | False | Stop the loop |
Notice that the loop checks the condition again after each update. When i becomes 6, the condition is false, so the program exits the loop.
Imagine placing five books on a table, one by one.
Closed book on desk by blackboard
1. Initialization
Start counting from book number 1.
2. Condition
Ask: Have we placed five books or fewer?
a diligent boy sits at a Desk and puts books on stacks.
3. Repetition and update
Place another book and increase the count.
Business Research Methods – Online Course – FutureLearn
4. Stop
Stop when five books have been placed.
The same idea applies to programming: check, execute, update, and repeat until the condition becomes false.
Start with the following program.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 5)
{
cout << "Hello Students!" << endl;
i++;
}
return 0;
}
Complete these activities in order.
Modification checklist
0 of 5
Change the repetition count
Change i <= 5 to i <= 3. How many times will the message appear?
Change the message
Replace Hello Students! with Welcome to the Programming Lab!
Change the starting value
Change int i = 1 to int i = 2. Keep the condition i <= 5. How many times will the message appear?
Change the update
Replace i++ with i = i + 2. Keep the starting value 1 and condition i <= 5. Predict the number of messages.
Display a counter
Replace the message with cout « i « endl;. Predict the output before running the program.
Students should attempt these exercises independently.
Exercise 1 — Easy
Display a message
Write a C++ program that displays I am learning C++ five times using a while loop.
Exercise 2 — Easy
Print numbers
Write a program to display numbers from 1 to 10.
Exercise 3 — Easy
Countdown
Write a program to display numbers from 10 down to 1.
Exercise 4 — Intermediate
Even numbers
Write a program to display even numbers from 2 to 20.
Exercise 5 — Intermediate
User-controlled repetition
Ask the user to enter a number N. Display Welcome! exactly N times.
i++ with i = i + 2: The first increases the value by 1; the second increases it by 2.i?i <= 5 mean?i++?int i = 1;
while (i <= 4)
{
cout << i << endl;
i++;
}
Expected output:
1
2
3
4
Students should remember these four concepts:
The next lesson should cover the while loop in detail, including user input, number sequences, sums, trace tables, and exercises.