Slide 2
Quick Recap of Lecture 12
5 minutes · homework check
Homework 1
- do {
- cout << "Saving per month (Rs.): ";
- cin >> perMonth;
- } while (perMonth <= 0);
- Today's question
- Every counter loop so far had its start, condition and update on three different lines. Forget one → bug.
- Can we write all three together?
Recap
Programming Fundamentals · Lecture 13
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & three parts in one line
3 min
The for loop: syntax and order of steps
10 min
for vs while: choosing a loop
7 min
Patterns: steps, products, tables, input
13 min
Class activity
7 min
Traps & recap quiz
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 13
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Write a for loop and explain its three parts
SLO 13.1 → CLO4 · C3
Convert between for and while loops and choose the right loop for a problem
SLO 13.2 → CLO4 · C2
SLO 13.3 → CLO1 · C3
Use for loops for counting, totals, tables and simple series
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 13
Slide 5
Three Parts, One Line
The same loop written two ways
while (Lecture 11)
for (today)
- int i = 1; // start
- while (i <= 5) { // condition
- cout << i << " ";
- i++; // update
- }
- for (int i = 1; i <= 5; i++) {
- cout << i << " ";
- }
Both print
1 2 3 4 5
for ( start ; condition ; update ) — two semicolons inside the brackets
Module 1 · The for Loop
Programming Fundamentals · Lecture 13
Slide 6
How a for Loop Runs
Start once → check → body → update → check …
Syntax
start
int i = 1
- for (int i = 1; i <= 5; i++) {
- // body
- }
runs only once
condition
i <= 5
checked before every round; false → leave
- Real-world: 10 laps of the track
- 1 stand on the start line (once)
- 2 ask: fewer than 10 laps done?
- 3 run one lap
- 4 add one to the lap count, then ask again
body
{ … }
runs when the condition is true
update
i++
runs after the body, then back to 2
Module 1 · The for Loop
Programming Fundamentals · Lecture 13
Slide 7
Example: Multiplication Table
A counter loop that runs exactly 10 times
table7.cpp (loop part)
Output
- for (int i = 1; i <= 10; i++) {
- cout << "7 x " << i << " = " << 7 * i << endl;
- }
- 7 x 1 = 7
- 7 x 2 = 14
- 7 x 3 = 21
- 7 x 4 = 28
- 7 x 5 = 35
- 7 x 6 = 42
- 7 x 7 = 49
- 7 x 8 = 56
- 7 x 9 = 63
- 7 x 10 = 70
Module 1 · The for Loop
Programming Fundamentals · Lecture 13
Slide 8
Other Steps and Directions
The update can add 2, subtract 1, or anything else
even_sum.cpp
countdown.cpp
- int sum = 0;
- for (int i = 2; i <= 100; i += 2) {
- sum += i;
- }
- for (int i = 10; i >= 1; i--) {
- cout << i << " ";
- }
- cout << "Lift-off!" << endl;
Output
Output
Sum of even numbers 2 to 100 = 2550
10 9 8 7 6 5 4 3 2 1 Lift-off!
Module 1 · The for Loop
Programming Fundamentals · Lecture 13
Slide 9
for ↔ while: The Same Loop
Any for loop can be written as a while loop, and back
for
while
- for (int i = 1; i <= n; i++) {
- sum += i;
- }
- int i = 1;
- while (i <= n) {
- sum += i;
- i++;
- }
Module 2 · Choosing a Loop
Programming Fundamentals · Lecture 13
Slide 10
Which Loop Should I Use?
Three loops, one job each
for
while
do-while
We know HOW MANY times
Repeat UNTIL something happens (maybe 0 times)
Must run AT LEAST once
tables, sums of 1..n, n students' marks
digits, savings goal, sentinel input
menus, input validation, PIN
- Check yourself
- Print 1 to 100 → for. Read marks until -1 → while. ATM menu → do-while. Count digits of a number → while.
Module 2 · Choosing a Loop
Programming Fundamentals · Lecture 13
Slide 11
The Product Pattern: Factorial
n! = 1 × 2 × 3 × … × n — the product must start at 1
factorial.cpp
- int n;
- cout << "Enter n: ";
- cin >> n;
- long long fact = 1; // start at 1, not 0
- for (int i = 1; i <= n; i++) {
- fact = fact * i;
- }
- cout << n << "! = " << fact << endl;
- Overflow
- 21! is bigger than the largest long long, so the value wraps round into a wrong negative number. No error is shown!
Sum: start at 0. Product: start at 1.
Module 3 · Patterns with for
Programming Fundamentals · Lecture 13
Slide 12
Example: Savings Growth Table
Rs. 100,000 at 10% profit per year (example rate) for 5 years
savings_table.cpp
Output
- double amount = 100000; // deposit in Rs.
- const double RATE = 0.10; // 10% profit per year
- cout << "Year Amount (Rs.)" << endl;
- cout << fixed << setprecision(2);
- for (int year = 1; year <= 5; year++) {
- amount = amount + amount * RATE;
- cout << setw(4) << year << " " << setw(12) << amount << endl;
- }
- Year Amount (Rs.)
- 1 110000.00
- 2 121000.00
- 3 133100.00
- 4 146410.00
- 5 161051.00
- Real-world
- Bank savings certificates and term deposits grow this way (actual rates differ).
Module 3 · Patterns with for
Programming Fundamentals · Lecture 13
Slide 13
Reading n Values with a for Loop
Ask how many first, then loop that many times
average.cpp
Output
- int n;
- double marks, total = 0;
- cout << "How many students? ";
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cout << "Marks of student " << i << ": ";
- cin >> marks;
- total += marks;
- }
- cout << "Average = " << total / n << endl;
- How many students? 3
- Marks of student 1: 70
- Marks of student 2: 85
- Marks of student 3: 64
- Average = 73
- Danger
- n = 0 → total / n divides by zero. Validate n first.
Count known → for. Count unknown, stop value → while with a sentinel (Lecture 12).
Module 3 · Patterns with for
Programming Fundamentals · Lecture 13
Slide 14
CLASS ACTIVITY · 7 MIN · IN PAIRS
for Loop Practice
Write each loop. For 4, answer without running it.
Tasks
Check yourself
- 1. Print the odd numbers 1 3 5 … 19.
- 2. Add the squares 1² + 2² + … + 10².
- 3. Rewrite as a for loop:
- int k = 5; while (k <= 50) { cout << k; k += 5; }
- 4. How many times does this body run?
- for (int i = 0; i < 10; i += 3)
- Which part of the for brackets changes in task 1?
- List the values of i in task 4.
Slide 15
Two for Loop Traps
Real results
1 Stray semicolon
2 Using i after the loop
- int i;
- for (i = 1; i <= 5; i++); // stray ;
- {
- cout << "i = " << i << endl;
- }
- for (int i = 1; i <= 3; i++) {
- cout << i << " ";
- }
- cout << "Last i: " << i << endl;
Output — printed once, not 5 times
g++ says
i = 6
error: 'i' was not declared in this scope
warning: this 'for' clause does not guard...
Fixes: no ; after for ( … ) · declare i before the loop if you need it afterwards.
Programming Fundamentals · Lecture 13
Slide 16
Quick Recap
Answer aloud
Q1
Name the three parts inside the brackets of a for loop.
Q2
In what order do the parts run?
Q3
Which loop: (a) table of 9 (b) read until -1 (c) menu?
Q4
Why must a product (like factorial) start at 1?
Programming Fundamentals · Lecture 13
Slide 17
Homework
Print the table of any number n from n × 1 to n × 20.
Input n; print 1 + 2 + … + n and n! (use long long). Test 5 and 10.
Input 10 temperatures with a for loop; print the average.
Series: print 1 + 1/2 + 1/3 + … + 1/10 as a decimal (careful: integer division!).
Next lecture: break, continue and common loop patterns (counters, sums, sentinels)