Slide 2
Quick Recap of Lecture 7
5 minutes · homework check
Remember
- Understand → plan → do → check
- Good algorithm: finite, definite, input, output, effective
- Top-down design: break into parts
- Trace table: values after each step
- Lecture 4: conditions give true / false
Recap
Programming Fundamentals · Lecture 8
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & why programs decide
3 min
The if statement and its flowchart
10 min
The if-else statement
10 min
Real-world rules & testing both branches
7 min
Class activity: rules → if-else
8 min
Three traps & recap quiz
6 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 8
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Explain how an if statement changes the flow of execution and draw its flowchart
SLO 8.1 → CLO4 · C2
Write if and if-else statements with correct conditions and braces
SLO 8.2 → CLO4 · C3
SLO 8.3 → CLO1 · C3
Turn a real-world yes/no rule into an if-else program and test both branches
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 8
Slide 5
Programs That Decide
So far: every line runs, every time. Real life is not like that.
Traffic signal
ATM
If the light is red, stop; otherwise go.
If the balance is enough, give cash; otherwise show 'Insufficient balance'.
Result card
Shopping
If marks ≥ 50, print PASS; otherwise FAIL.
If the bill is Rs. 5,000 or more, give 10% off.
Sequential = a straight road (every line runs). Selection (decision) = a fork in the road: only one path is taken.
Module 1 · The if Statement
Programming Fundamentals · Lecture 8
Slide 6
The if Statement
Run a block ONLY when the condition is true
previous statement
Syntax
- if (condition) {
- // runs only when condition is true
- }
- // program continues here
condition?
false
true
- Parts
- if — keyword (small letters)
- ( condition ) — any true/false expression from Lecture 4
- { block } — one or more statements
block
next statement
Module 1 · The if Statement
Programming Fundamentals · Lecture 8
Slide 7
Example: 10% Off on Big Bills
A shop in Faisalabad gives 10% off when the bill is Rs. 5,000 or more
discount.cpp
Run 1 (true)
- double bill;
- cout << "Enter bill (Rs.): ";
- cin >> bill;
- if (bill >= 5000) {
- bill = bill - bill * 0.10; // 10% discount
- cout << "You got a 10% discount!" << endl;
- }
- cout << "Amount to pay: Rs. " << bill << endl;
- Enter bill (Rs.): 6000
- You got a 10% discount!
- Amount to pay: Rs. 5400
Run 2 (false)
- Enter bill (Rs.): 3000
- Amount to pay: Rs. 3000
The last line is outside the braces, so it always runs.
Module 1 · The if Statement
Programming Fundamentals · Lecture 8
Slide 8
Blocks and Braces { }
Braces decide WHICH lines belong to the if
Two statements: braces needed
One statement: braces optional
- if (marks >= 50) {
- cout << "PASS" << endl;
- cout << "Well done!" << endl;
- }
- if (n % 2 == 0)
- cout << "even" << endl;
- else
- cout << "odd" << endl;
- Course rule
- Always write braces { } — even for one statement. It prevents the most common if-bug.
Indent the block by 4 spaces so the reader sees what is inside. The compiler does NOT look at spaces — only at braces.
Module 1 · The if Statement
Programming Fundamentals · Lecture 8
Slide 9
The if-else Statement
Two paths: exactly ONE of them runs
Syntax
true
condition?
false
- if (condition) {
- // runs when condition is true
- } else {
- // runs when condition is false
- }
if block
else block
- Real-world
- If it is raining, take an umbrella; otherwise wear sunglasses.
- You do one or the other — never both.
next statement
Module 2 · The if-else Statement
Programming Fundamentals · Lecture 8
Slide 10
Example: PASS or FAIL
The classic if-else
passfail.cpp
Run 1
- int marks;
- cout << "Enter marks: ";
- cin >> marks;
- if (marks >= 50) {
- cout << "PASS" << endl;
- } else {
- cout << "FAIL" << endl;
- }
- cout << "Result checked." << endl;
- Enter marks: 72
- PASS
- Result checked.
Run 2
- Enter marks: 38
- FAIL
- Result checked.
Shortcut from Lecture 4: cout << (marks >= 50 ? "PASS" : "FAIL"); does the same in one line (more in Lecture 10).
Module 2 · The if-else Statement
Programming Fundamentals · Lecture 8
Slide 11
Example: ATM Withdrawal
Each branch can have several statements
atm.cpp
Run 1
- int balance = 12000, amount;
- cout << "Amount to withdraw: ";
- cin >> amount;
- if (amount <= balance) {
- balance = balance - amount;
- cout << "Please take your cash." << endl;
- cout << "New balance: Rs. " << balance << endl;
- } else {
- cout << "Insufficient balance." << endl;
- }
- Amount to withdraw: 5000
- Please take your cash.
- New balance: Rs. 7000
Run 2
- Amount to withdraw: 15000
- Insufficient balance.
The else branch protects the bank: the balance never becomes negative.
Module 2 · The if-else Statement
Programming Fundamentals · Lecture 8
Slide 12
Any Condition from Lecture 4 Can Be Used
Relational and logical operators inside the brackets
Module 3 · Real-World Rules & Testing
Programming Fundamentals · Lecture 8
Slide 13
Test BOTH Branches — and the Boundary
A decision is only tested when each path has run at least once
- Bug found by 50
- If you wrote marks > 50 by mistake, the input 50 prints FAIL. Only the boundary test shows it!
- Real-world
- A lift is tested with exactly its maximum load, not just an empty cabin.
Module 3 · Real-World Rules & Testing
Programming Fundamentals · Lecture 8
Slide 14
CLASS ACTIVITY · 8 MIN · IN PAIRS
Rules → if-else
Write the if or if-else for each rule (just the decision part). Then write two test inputs for each: one for each branch.
Rules
Check yourself
- 1. If the temperature is above 40, print "Heatwave alert".
- 2. A number is positive or not positive (zero/negative).
- 3. Bus fare: Rs. 600 for children under 12, otherwise Rs. 1,200.
- 4. Print "Fee paid" if paid is true, else "Fee due".
- 5. A year is leap or not (use Lecture 4's condition).
- Which rules need else?
- Did you use braces?
- Did you test the boundary (40, 0, 12)?
Slide 15
Three Traps (They All Compile!)
Real outputs with marks = 35 — g++ -Wall -Wextra warns about each one
1 Stray semicolon
2 Missing braces
3 = instead of ==
- if (marks >= 50);
- cout << "PASS";
- if (marks >= 50)
- cout << "PASS";
- cout << "Congratulations!";
- if (marks = 100)
- cout << "Full marks!";
Output
Output
Output
PASS
Congratulations!
- Full marks!
- marks is now 100
warning: suggest braces around empty body
warning: this 'if' clause does not guard...
warning: suggest parentheses around assignment
Fix all three: no ; after the condition · always use braces · use == to compare.
Programming Fundamentals · Lecture 8
Slide 16
Quick Recap
Answer aloud
Q1
What happens to the if block when the condition is false?
Q2
In an if-else, can both blocks run? Can neither run?
Q3
Which inputs would you test for: if (age >= 18)?
Q4
Why does if (marks >= 50); always print PASS?
Programming Fundamentals · Lecture 8
Slide 17
Homework
Draw flowcharts for passfail.cpp and atm.cpp.
Program: input a number and print whether it is positive, or zero/negative (if-else). Test 5, 0, -3.
Program: input units of electricity; if units > 200 print "Not protected", else "Protected consumer". Test 199, 200, 201.
Program: input two numbers and print the larger one using if-else. What happens when they are equal?
Next lecture: Conditional statements II — nested if and the else-if ladder (many choices)