Slide 2
Quick Recap of Lecture 3
5 minutes · check homework
Remember
Homework check
- Precedence = BODMAS: ( ) then * / % then + -
- Same level → left to right
- x++ uses then adds; ++x adds then uses
- Cast BEFORE dividing: static_cast<double>(a) / b
- fixed << setprecision(2) for money
- Use pow(), never ^
- int a = 4, b = 3;
- a += b++; // a = 7, b = 4
- b *= --a; // a = 6, b = 24
- // percentage, correctly:
- double p = static_cast<double>(437) / 550 * 100;
- // 79.45
Module 0 · Recap
Programming Fundamentals · Lecture 4
Slide 3
Today's Plan
90 minutes · 6 modules
Recap & Homework Check
5 min
Relational (Comparison) Operators
20 min
Logical Operators && || !
25 min
Real-World Rules as Expressions
20 min
Traps, Precedence & Preview of ?:
15 min
Wrap-up & Homework
5 min
Programming Fundamentals · Lecture 4
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
SLO 4.1 → CLO4 · C2
SLO 4.2 → CLO4 · C3
Compare values with relational operators and explain that the result is a bool
Combine conditions with &&, || and ! using truth tables
SLO 4.3 → CLO1 · C3
SLO 4.4 → CLO4 · C4
Translate real-world rules (eligibility, leap year) into logical expressions
Find and fix common mistakes: = vs ==, chained comparisons, comparing doubles
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 4
Slide 5
Programs Need to Ask Questions
Every decision starts with a yes/no question
Can this person get a CNIC?
Has the student passed?
age >= 18
marks >= 50
Is the shop balance enough?
Is it a weekend?
balance >= price
day == 6 || day == 7
Is the ATM PIN correct?
Is the temperature pleasant?
pin == 4321
temp >= 20 && temp <= 30
Module 1 · Relational Operators
Programming Fundamentals · Lecture 4
Slide 6
The Six Relational Operators
Relational = shows the relation between two values · result is always true or false
- Remember
- = puts a value in a box
- == asks "are they equal?"
- No spaces inside
- Write <= >= == !=
- not < = or = = (error)
Module 1 · Relational Operators
Programming Fundamentals · Lecture 4
Slide 7
A Comparison Gives a bool
true is stored as 1, false as 0
rel.cpp
Output
- int a = 7, b = 10;
- cout << (a < b) << endl; // 1
- cout << (a == b) << endl; // 0
- cout << boolalpha;
- cout << (a != b) << endl; // true
- cout << (a >= 7) << endl; // true
- 1
- 0
- true
- true
boolalpha prints true/false instead of 1/0. It stays on.
Keep brackets: cout << a < b; is a compile error — << runs before <. Write cout << (a < b);
Module 1 · Relational Operators
Programming Fundamentals · Lecture 4
Slide 8
Storing a Condition in a bool Variable
Give the question a meaningful name
result.cpp
- int marks;
- cin >> marks;
- bool isPassed = marks >= 50;
- bool isFullMarks = marks == 100;
- cout << boolalpha << isPassed;
- Naming tip
- Start bool names with is, has, can: isLeap, hasCnic, canVote. Then the code reads like English: canVote = age >= 18;
Module 1 · Relational Operators
Programming Fundamentals · Lecture 4
Slide 9
Comparing Characters and Strings
Characters are compared by their ASCII codes
- Real-world example
- Your phone's contact list is sorted using string comparison: "Ahmed" < "Bilal" < "Zain".
- Careful
- "ali" == "Ali" is false — capital and small letters have different codes.
Module 1 · Relational Operators
Programming Fundamentals · Lecture 4
Slide 10
Logical Operators: AND, OR, NOT
Join small questions into one big question
&&
||
!
AND
OR
NOT
true only if BOTH are true
true if AT LEAST ONE is true
flips: true ↔ false
Real-world: Admission: age OK && marks OK
Real-world: Discount: student || senior citizen
Real-world: !isRaining → go for a walk
Module 2 · Logical Operators
Programming Fundamentals · Lecture 4
Slide 11
Truth Tables
All possible inputs and the result of each operator
- Easy way to remember
- AND (&&): one false makes it false. OR (||): one true makes it true. NOT (!): opposite.
Module 2 · Logical Operators
Programming Fundamentals · Lecture 4
Slide 12
Example: Who Can Vote?
Pakistan: age 18 or more AND a CNIC
eligible.cpp
- int age;
- bool hasCnic;
- cout << "Enter age: ";
- cin >> age;
- cout << "Has CNIC? (1/0): ";
- cin >> hasCnic;
- bool canVote = age >= 18 && hasCnic;
- cout << boolalpha << "Can vote: " << canVote;
- Test every row
- Choose inputs that make each part true and false at least once.
Module 2 · Logical Operators
Programming Fundamentals · Lecture 4
Slide 13
Checking a Range
Is a value BETWEEN two limits?
Inside the range (AND)
Outside the range (OR)
temp >= 20 && temp <= 30
temp < 20 || temp > 30
Pleasant weather: between 20 °C and 30 °C
Uncomfortable weather: colder than 20 or hotter than 30
pleasant (&&)
Module 2 · Logical Operators
Programming Fundamentals · Lecture 4
Slide 14
CLASS ACTIVITY · 8 MIN · IN PAIRS
English → C++
Write one C++ expression for each rule. Use meaningful variable names.
Rules
Check yourself
- 1. Marks are between 0 and 100 (inclusive).
- 2. A customer gets free delivery if the bill is Rs. 2000 or more OR they are a member.
- 3. A number is even AND positive.
- 4. A character is a capital letter.
- 5. The shop is CLOSED on Friday (day 5) between 1 pm and 2 pm (hour == 13).
- Test each expression with one value that should give true and one that should give false.
- Which ones need && and which need ||?
Slide 15
Real-World Rule: Leap Year
A year is leap if divisible by 4 but not by 100 — unless divisible by 400
leap.cpp
- int year;
- cin >> year;
- bool isLeap = (year % 4 == 0 && year % 100 != 0)
- || (year % 400 == 0);
- cout << boolalpha << isLeap;
- Problem-solving tip (CLO1)
- Break a big rule into small yes/no parts → write each part as a comparison → join them with && and ||. Then test with values you already know.
Module 3 · Real-World Rules
Programming Fundamentals · Lecture 4
Slide 16
Saying "NOT" Correctly (De Morgan's Rule)
How to flip a combined condition
- Common mistake
- The opposite of < is >= (not >). The value that is EQUAL must go somewhere!
Module 3 · Real-World Rules
Programming Fundamentals · Lecture 4
Slide 17
Short-Circuit Evaluation
C++ stops as soon as the answer is known
&& stops at the first false
|| stops at the first true
- false && (anything) → false
- The right side is not even looked at.
- true || (anything) → true
- The right side is not even looked at.
guard.cpp
- int y = 0;
- bool ok = (y != 0) && (10 / y > 1); // safe: 10 / y never runs when y is 0
Real-world: a guard at the gate checks your card first — if you have no card, he does not check the guest list.
Module 3 · Real-World Rules
Programming Fundamentals · Lecture 4
Slide 18
Full Precedence Table (So Far)
Arithmetic first, then comparisons, then logic, then assignment
- && before ||
- a || b && c
- means
- a || (b && c)
- ! is strong
- !x > 10 means (!x) > 10. Write !(x > 10)
Module 4 · Traps & Preview
Programming Fundamentals · Lecture 4
Slide 19
Four Traps to Avoid
These compile — but give the wrong answer
!
= instead of ==
!
Chained comparison
bool full = (marks = 100);
0 < x < 10
Puts 100 into marks → always true
(0 < x) is 0 or 1, and that is always < 10
Fix: marks == 100
Fix: 0 < x && x < 10
!
Short OR
!
Comparing doubles with ==
ch == 'a' || 'e'
0.1 + 0.2 == 0.3
'e' alone is non-zero → always true
false! decimals are not exact
Fix: ch == 'a' || ch == 'e'
Fix: fabs(s - 0.3) < 1e-9
Module 4 · Traps & Preview
Programming Fundamentals · Lecture 4
Slide 20
Preview: The Conditional Operator ? :
Choose between two values using a condition
condition ? value if true : value if false
ternary.cpp
- Real-world
- Cinema ticket: student? Rs. 500 : Rs. 800.
- We will learn full if-else in Lecture 7.
- bool passed = marks >= 50;
- cout << (passed ? "PASS" : "FAIL");
- int days = isLeap ? 29 : 28;
- double fee = isStudent ? 50.0 : 100.0;
Module 4 · Traps & Preview
Programming Fundamentals · Lecture 4
Slide 21
Quick Recap
Answer aloud
Q1
What is the difference between = and == ?
Q2
Complete the rule: && is true only when … || is true when …
Q3
Write C++ for: x is between 1 and 10 (inclusive).
Q4
Why does 0 < x < 10 give true even when x = 50?
Programming Fundamentals · Lecture 4
Slide 22
Homework
With a = 5, b = 10, c = 15, evaluate by hand: a < b && b < c; a > b || c > b; !(a + b == c); a * 2 == b && c % a == 0
Write one bool expression: n is even AND divisible by 3 but NOT by 9. Test with 12, 18, 9
Input a character; print true if it is a vowel (a, e, i, o, u). Watch the short-OR trap!
Use De Morgan to rewrite !(t < 10 || t > 40) without the !
Input three angles; print true if they can make a triangle (all > 0 and sum is 180)
Next lecture: Von Neumann architecture — what happens inside the computer when a program runs