while Loop| Subject: Programming Fundamentals | Language: C++ | Level: Beginner |
By the end of this lecture, students will be able to:
while loop.while loop using a condition.while Loop?A while loop repeats a block of code as long as a given condition is true.
Real-life example: Imagine filling a water bottle. You continue filling it while it is not full. When the bottle becomes full, you stop.
A while loop works in a similar way:
while Loopwhile (condition)
{
// Statements to repeat
}
Example:
int i = 1;
while (i <= 5)
{
cout << i << endl;
i++;
}
int i = 1; — starts the counter at 1.while (i <= 5) — repeats the loop while i is less than or equal to 5.cout << i << endl; — displays the current value of i.i++; — increases i by 1.Output:
1
2
3
4
5
Remember: i++ means i = i + 1.
Let’s trace the previous program.
Value of i |
Condition i <= 5 |
Output |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 2 |
| 3 | True | 3 |
| 4 | True | 4 |
| 5 | True | 5 |
| 6 | False | Loop stops |
When i becomes 6, the condition is false, so the loop ends.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 5)
{
cout << "Welcome to Ghazi University!" << endl;
i++;
}
return 0;
}
Understand the code: The loop starts at 1 and repeats until i becomes greater than 5. The message is displayed five times.
Modify the code:
"Welcome to C++ Programming!".i at 2 and repeat while i <= 6. How many times will the message appear?#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
cout << i << " ";
i++;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Understand the code: The counter begins at 1. Each repetition prints the current number and increases it by 1. The loop stops when the counter reaches 11.
Modify the code:
#include <iostream>
using namespace std;
int main()
{
int i = 2;
while (i <= 20)
{
cout << i << " ";
i = i + 2;
}
return 0;
}
Output:
2 4 6 8 10 12 14 16 18 20
Understand the code: The counter starts at 2 and increases by 2 after each repetition. Therefore, only even numbers are displayed.
Modify the code:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
while (i >= 1)
{
cout << i << " ";
i--;
}
return 0;
}
Output:
10 9 8 7 6 5 4 3 2 1
Understand the code:
i >= 1.i-- decreases the counter by 1.i becomes 0.Modify the code: Change the countdown to start from 20 and stop at 1.
A loop can do more than display numbers. It can also calculate their sum.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
int sum = 0;
while (i <= 5)
{
sum = sum + i;
i++;
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 15
i controls how many times the loop repeats.sum stores the running total.sum = sum + i; adds the current value of i to sum.i++ moves to the next number.i |
Calculation | New sum |
|---|---|---|
| 1 | 0 + 1 | 1 |
| 2 | 1 + 2 | 3 |
| 3 | 3 + 3 | 6 |
| 4 | 6 + 4 | 10 |
| 5 | 10 + 5 | 15 |
Modify the code:
Suppose the user enters a number, and the program displays all 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;
while (i <= n)
{
cout << i << " ";
i++;
}
return 0;
}
Sample output:
Enter a number: 5
1 2 3 4 5
Understand the code: The value of n comes from the user. If the user enters 5, the loop displays numbers from 1 to 5. If the user enters 8, it displays numbers from 1 to 8.
Modify the code: Display numbers from n down to 1.
while LoopIncorrect:
int i = 1;
while (i <= 5)
{
cout << i << " ";
}
Problem: The value of i never changes. The condition remains true, so the loop runs indefinitely.
Correct:
int i = 1;
while (i <= 5)
{
cout << i << " ";
i++;
}
int i = 1;
while (i >= 5)
{
cout << i;
i++;
}
Problem: The condition is false from the beginning because 1 is not greater than or equal to 5. The loop body never executes.
whileIncorrect:
while (i <= 5);
{
cout << i;
i++;
}
Problem: The semicolon creates an empty loop. It may cause an infinite loop if the condition remains true.
Correct:
while (i <= 5)
{
cout << i;
i++;
}
Student checklist
0 of 6
1. Repeat a message
Write a program to display your name five times.
2. Print a sequence
Display numbers from 1 to 50.
3. Print odd numbers
Display odd numbers from 1 to 25.
4. Countdown
Display numbers from 50 down to 1.
5. Calculate a sum
Calculate the sum of numbers from 1 to 20.
6. User input
Ask the user for a number and display its multiplication table from 1 to 10.
Try to answer before checking the solutions.
1. What happens when a `while` condition is false at the beginning?
The loop executes once
The loop body does not execute
The program always crashes
2. What does `i++` do?
Adds 2 to i
Subtracts 1 from i
Adds 1 to i
3. Which statement is important to prevent an unintended infinite loop?
Update the counter when needed
Use `cout` twice
Always start at zero
Check Answers
while loop repeats statements while its condition is true.Next lecture: The do-while loop — how it differs from the while loop and why it executes its body at least once.