Slide 2
Quick Recap of Lecture 10
5 minutes · homework check
Homework 1
Three ways to control flow
- switch (month) {
- case 1: case 3: case 5: case 7:
- case 8: case 10: case 12: days = 31; break;
- case 4: case 6: case 9: case 11: days = 30; break;
- case 2: days = 28; break;
- default: cout << "Invalid month";
- }
- Sequence — one after another (Weeks 1–3)
- Selection — choose a path (Week 4)
- Repetition — do it again (today!)
- Today's question
- How do we print 1 to 1000 without writing 1000 couts?
Recap
Programming Fundamentals · Lecture 11
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & why repeat
3 min
The while loop and its flowchart
10 min
Counter loops: count, sum, trace table
12 min
Condition loops: savings, digits
7 min
Class activity: four small loops
8 min
Loop traps & recap quiz
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 11
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Explain why programs need repetition and how the condition of a while loop controls it
SLO 11.1 → CLO4 · C2
Write a while loop with a start value, a condition and an update
SLO 11.2 → CLO4 · C3
SLO 11.3 → CLO1 · C4
Trace a loop with a trace table and find infinite-loop and off-by-one mistakes
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 11
Slide 5
Why Do Programs Repeat?
Many real jobs are the same small step done again and again
Without a loop
With a loop: 4 lines for any number
- cout << 1 << " ";
- cout << 2 << " ";
- cout << 3 << " ";
- cout << 4 << " ";
- cout << 5 << " ";
- // … 1000 lines for 1 to 1000?
- int i = 1;
- while (i <= 1000) {
- cout << i << " ";
- i++;
- }
Tasbeeh
Track
Cashier
ATM
count 33 beads
run 4 laps
scan until the basket is empty
ask PIN again if wrong
Module 1 · The while Loop
Programming Fundamentals · Lecture 11
Slide 6
The while Loop
Repeat the block AS LONG AS the condition is true
before the loop
Syntax
- while (condition) {
- // body: runs again and again
- // while condition is true
- }
- // continues here when false
condition?
false
repeat
true
body
- Like if — but it comes back
- if: check once, maybe run once.
- while: check, run, check again, run again … until false.
after the loop
Module 1 · The while Loop
Programming Fundamentals · Lecture 11
Slide 7
Every Counter Loop Has Three Parts
Start · condition · update
count.cpp
1 Start
- int i = 1; // 1. start
- while (i <= 5) { // 2. condition
- cout << i << " ";
- i++; // 3. update
- }
- cout << endl << "Loop finished, i = " << i << endl;
give the counter its first value
2 Condition
checked before every round
Output
3 Update
- 1 2 3 4 5
- Loop finished, i = 6
change the counter so the loop can end
Module 1 · The while Loop
Programming Fundamentals · Lecture 11
Slide 8
Trace Table for count.cpp
One row per check of the condition
- Count them
- Condition checked: 6 times
- Body ran: 5 times
- Iteration
- One round of the loop body is called an iteration.
Module 2 · Counter Loops
Programming Fundamentals · Lecture 11
Slide 9
Adding Up: The Sum (Accumulator) Pattern
Keep a running total and add to it in every round
sum_n.cpp
Run 1
- int n;
- cout << "Enter n: ";
- cin >> n;
- int i = 1, sum = 0;
- while (i <= n) {
- sum = sum + i;
- i++;
- }
- cout << "Sum of 1 to " << n << " = " << sum << endl;
- Enter n: 10
- Sum of 1 to 10 = 55
Run 2
- Enter n: 100
- Sum of 1 to 100 = 5050
sum must start at 0!
Module 2 · Counter Loops
Programming Fundamentals · Lecture 11
Slide 10
Two Kinds of Loops
Do we know HOW MANY times in advance?
- Counter-controlled
- We know the number of rounds.
- Print 1 to 100; add 10 marks; 33 tasbeeh beads.
- while (i <= n) { …; i++; }
- Condition-controlled
- We stop when something becomes true.
- Save until the goal; read digits until none are left; ask the PIN until correct.
- while (saved < GOAL) { … }
Both kinds need something inside the body that eventually makes the condition false.
Module 2 · Counter Loops
Programming Fundamentals · Lecture 11
Slide 11
Example: Saving for a New Phone
Repeat until the goal is reached — we don't know how many months
savings.cpp
Run 1
- const int GOAL = 30000; // price of a new mobile phone
- int perMonth, saved = 0, months = 0;
- cout << "Saving per month (Rs.): ";
- cin >> perMonth;
- while (saved < GOAL) {
- saved = saved + perMonth;
- months++;
- }
- cout << "Months needed: " << months << endl;
- cout << "Total saved : Rs. " << saved << endl;
- Saving per month (Rs.): 5000
- Months needed: 6
- Total saved : Rs. 30000
Run 2
- Saving per month (Rs.): 7000
- Months needed: 5
- Total saved : Rs. 35000
Danger: input 0 → saved never grows → infinite loop. Lecture 12 shows how to reject bad input.
Module 3 · Condition Loops
Programming Fundamentals · Lecture 11
Slide 12
Example: Count and Add the Digits
Remove one digit each round until nothing is left
digits.cpp (loop part)
- while (n > 0) {
- sum = sum + n % 10; // add the last digit
- n = n / 10; // remove the last digit
- count++;
- }
- cout << "Digits: " << count << ", sum of digits: " << sum;
Output
Real-world: bank card numbers use a digit-sum check (the Luhn check) to catch typing mistakes.
- Enter a positive number: 4072
- Digits: 4, sum of digits: 13
Module 3 · Condition Loops
Programming Fundamentals · Lecture 11
Slide 13
CLASS ACTIVITY · 8 MIN · IN PAIRS
Four Small Loops
Write the while loop (or the trace) for each. Name the start, condition and update every time.
Tasks
Check yourself
- 1. Print 10 9 8 … 1 (count down).
- 2. Add the even numbers 2 + 4 + … + 20.
- 3. A tree is 50 cm tall and grows 30 cm a year. How many years until it is at least 300 cm?
- 4. Trace: i = 1; while (i < 20) i = i * 3; — final i?
- Which tasks are counter-controlled and which are condition-controlled?
- Does every loop have an update?
Slide 14
Three Loop Traps
Two of them never stop!
1 Missing update
2 Stray semicolon
3 Off by one
- int i = 1;
- while (i <= 5) {
- cout << i << " ";
- }
- int i = 1;
- while (i <= 5);
- {
- cout << i; i++;
- }
- int i = 1;
- while (i < 5) {
- cout << i << " "; i++;
- }
Output
Output
Output
1 1 1 1 1 1 1 … (never stops)
(nothing — the program hangs)
1 2 3 4 (5 is missing)
Fixes: always update the counter · no ; after while ( ) · check the last value with a trace table. Stop a hung program: Ctrl + C.
Programming Fundamentals · Lecture 11
Slide 15
Quick Recap
Answer aloud
Q1
What are the three parts of a counter loop?
Q2
If the condition is false at the start, how many times does a while body run?
Q3
int i = 1; while (i <= 5) i++; — what is i after the loop?
Q4
What is an infinite loop? Give one cause.
Programming Fundamentals · Lecture 11
Slide 16
Homework
Print the multiplication table of 7 (7 × 1 = 7 … 7 × 10 = 70) with a while loop.
Input n; print the sum and the average of 1 … n. Test with 10 and 1.
Input a number; print it reversed using % and / (4072 → 2704).
Rs. 100,000 grows by 10% every year. Using while, find how many years until it is at least Rs. 200,000.
Next lecture: do-while loops and checking the user's input (validation)