UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 8 · STUDENT NOTES
Conditional Statements I: if and if-else
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
4 | Lecture8_If_and_If_Else.pptx | Practical 6 | N08 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 8.1 | Explain how an if statement changes the flow of execution and draw its flowchart | CLO4: Describe the syntax and semantics of the C++ programming language. | C2 |
SLO 8.2 | Write if and if-else statements with correct conditions and braces | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 8.3 | 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. | C3 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
1. Programs That Decide
Until now every program was sequential: every line ran, from top to bottom, every time. Real life is full of decisions — a traffic signal, an ATM, a result card. Flow of control (or execution flow) is the order in which statements run. A conditional statement (also called a selection statement) lets the program choose which statements to run. (Slide L08-S05)
Situation | Decision |
|---|---|
Traffic signal | If the light is red, stop; otherwise go. |
ATM | If the balance is enough, give cash; otherwise show 'Insufficient balance'. |
Result card | If marks ≥ 50, print PASS; otherwise FAIL. |
Shopping | If the bill is Rs. 5,000 or more, give 10% off. |
2. The if Statement
2.1 Syntax and flowchart
An if statement runs a block only when its condition is true. If the condition is false, the block is skipped and the program continues after the closing brace. (Slide L08-S06)
Syntax if (condition) { // runs only when condition is true } // program continues here |
Figure 8.1 – Flowchart of an if statement
- if is a keyword — always small letters.
- The condition goes in round brackets and can be any true/false expression from Lecture 4.
- The block is the statements inside the curly braces { }.
2.2 Example: 10% off on big bills
A shop gives 10% off when the bill is Rs. 5,000 or more. (Slide L08-S07)
discount.cpp #include <iostream> using namespace std;
int main() { 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; return 0; } |
Run 1 – condition true Enter bill (Rs.): 6000 You got a 10% discount! Amount to pay: Rs. 5400 |
Run 2 – condition false (the block is skipped) Enter bill (Rs.): 3000 Amount to pay: Rs. 3000 |
The last cout is outside the braces, so it runs in both cases.
2.3 Blocks and braces
The braces decide which lines belong to the if. Without braces, only the one next statement belongs to it. Indentation (spaces at the start of a line) helps humans read the code, but the compiler ignores it. (Slide L08-S08)
Course rule Always write braces { }, even for a single statement. It prevents the most common if-bug (§5). |
3. The if-else Statement
3.1 Syntax and flowchart
else means "otherwise". It has no condition of its own: it runs when the if condition is false. Exactly one of the two blocks runs — never both, never neither. (Slide L08-S09)
Syntax if (condition) { // runs when condition is true } else { // runs when condition is false } |
Figure 8.2 – Flowchart of an if-else statement
Real-world: if it is raining, take an umbrella; otherwise wear sunglasses. You do one or the other.
3.2 Example: PASS or FAIL
(Slide L08-S10)
passfail.cpp #include <iostream> using namespace std;
int main() { int marks; cout << "Enter marks: "; cin >> marks;
if (marks >= 50) { cout << "PASS" << endl; } else { cout << "FAIL" << endl; } cout << "Result checked." << endl; return 0; } |
Run 1 Enter marks: 72 PASS Result checked. |
Run 2 Enter marks: 38 FAIL Result checked. |
3.3 Example: ATM withdrawal
Each branch can contain several statements. (Slide L08-S11)
atm.cpp #include <iostream> using namespace std;
int main() { 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; } return 0; } |
Run 1 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.
3.4 One-statement branches
When a branch has only one statement the braces may be left out (but our course rule is to keep them).
evenodd.cpp #include <iostream> using namespace std;
int main() { int n; cout << "Enter a number: "; cin >> n; if (n % 2 == 0) cout << n << " is even" << endl; else cout << n << " is odd" << endl; return 0; } |
Run 1 Enter a number: 7 7 is odd |
Run 2 Enter a number: -4 -4 is even |
4. Real-World Rules and Testing
4.1 Conditions from Lecture 4
Any relational or logical expression can be the condition. (Slide L08-S12)
Rule in English | C++ |
|---|---|
Can vote: 18 or older AND has a CNIC | if (age >= 18 && hasCnic) { … } |
Weekend: Saturday OR Sunday (6 or 7) | if (day == 6 || day == 7) { … } |
Even number | if (n % 2 == 0) { … } else { … } |
Pleasant weather: 20 to 30 °C | if (temp >= 20 && temp <= 30) { … } |
Not a hostelite | if (!isHostelite) { … } |
Leap year | if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { … } |
4.2 Test both branches and the boundary
A decision is only tested when each branch has run at least once. The boundary (edge) value is the value exactly at the limit — most if-bugs hide there. (Slide L08-S13)
Input | Why test it? | Expected (marks >= 50) |
|---|---|---|
72 | normal pass | PASS |
38 | normal fail | FAIL |
50 | boundary — exactly the limit | PASS |
49 | just below | FAIL |
51 | just above | PASS |
If you wrote marks > 50 by mistake, only the test with 50 shows the bug (it prints FAIL).
5. Three Traps That Compile
All three programs below compile and run (marks = 35), but give wrong results. Turn on warnings: in Dev-C++ go to Tools → Compiler Options and add -Wall -Wextra. (Slide L08-S15)
5.1 Stray semicolon
trap_semicolon.cpp #include <iostream> using namespace std;
int main() { int marks = 35; if (marks >= 50); // <- stray semicolon cout << "PASS" << endl; return 0; } |
Output (wrong — marks is 35) PASS |
The ; ends the if with an empty statement, so the cout is not controlled by the if. g++ warns: suggest braces around empty body in an 'if' statement.
5.2 Missing braces
trap_braces.cpp #include <iostream> using namespace std;
int main() { int marks = 35; if (marks >= 50) cout << "PASS" << endl; cout << "Congratulations!" << endl; // not inside the if! return 0; } |
Output (wrong) Congratulations! |
Only the first cout belongs to the if; the second always runs. g++ warns: this 'if' clause does not guard.... Fix: put both lines inside braces.
5.3 = instead of ==
trap_assign.cpp #include <iostream> using namespace std;
int main() { int marks = 35; if (marks = 100) // = instead of == cout << "Full marks!" << endl; cout << "marks is now " << marks << endl; return 0; } |
Output (wrong) Full marks! marks is now 100 |
marks = 100 stores 100 in marks; any non-zero value counts as true. g++ warns: suggest parentheses around assignment used as truth value. Fix: marks == 100.
6. Word Bank (Key Terms)
Word | Simple meaning |
|---|---|
Flow of control | The order in which statements run |
Sequential | Every statement runs, one after another |
Selection / conditional statement | A statement that chooses which code runs (if, if-else, switch) |
Condition | An expression that is true or false |
Block | Statements grouped inside { } |
Branch | One of the paths after a decision |
else | "Otherwise" — runs when the if condition is false |
Indentation | Spaces at the start of a line to show structure |
Boundary (edge) value | A test value exactly at the limit, e.g. 50 for marks >= 50 |
7. Self-Check Questions
1. What happens to an if block when its condition is false?
2. Draw the flowchart of an if-else statement.
3. In an if-else, can both blocks run? Can neither run? Explain.
4. Write an if-else that prints "Child fare" for age under 12 and "Adult fare" otherwise. Which three ages would you test?
5. Why does if (marks >= 50); always print the next line?
6. What is printed by int x = 5; if (x = 0) cout << "A"; else cout << "B";? Why?
Answers are in §2.1, §3.1, §3.1, §4.2, §5.1 and §5.3 (the last prints B: x = 0 stores 0, which is false). Homework is on Slide L08-S17.