Module 4: Conditional Statements
Learn if, if-else, if-elif-else, nested conditions, logical expressions and decision-making applications using C, C++, Java and Python.
Conditions
Decisions
Logic
Apps
4.1 Module Overview
Conditional statements allow programs to make decisions. A program can check whether a condition is true or false and then execute different blocks of code.
This module compares conditional statements in C, C++, Java and Python. The logic is the same in all languages, but the syntax is different.
4.2 Conditional Syntax Comparison
| Concept | C / C++ / Java | Python |
|---|---|---|
| if | if (condition) { } | if condition: |
| else if | else if (condition) { } | elif condition: |
| else | else { } | else: |
| AND | && | and |
| OR | || | or |
| NOT | ! | not |
4.3 if Statement
The if statement executes a block of code only when the condition is true.
Working Example: Check Adult Age
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("Adult");
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int age = 20;
if (age >= 18) {
cout << "Adult";
}
return 0;
}public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
}
}age = 20
if age >= 18:
print("Adult")Adult
Online Working Example
4.4 if-else Statement
The if-else statement gives two possible paths. If the condition is true, one block runs. Otherwise, the else block runs.
Working Example: Pass or Fail
#include <stdio.h>
int main() {
int marks = 65;
if (marks >= 50) {
printf("Pass");
} else {
printf("Fail");
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int marks = 65;
if (marks >= 50) {
cout << "Pass";
} else {
cout << "Fail";
}
return 0;
}public class Main {
public static void main(String[] args) {
int marks = 65;
if (marks >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}marks = 65
if marks >= 50:
print("Pass")
else:
print("Fail")Online Working Example
4.5 if-elif-else / else-if Ladder
This structure is used when there are multiple possible decisions.
Working Example: Grade System
#include <stdio.h>
int main() {
int marks = 78;
if (marks >= 80) printf("Grade A");
else if (marks >= 70) printf("Grade B");
else if (marks >= 60) printf("Grade C");
else if (marks >= 50) printf("Grade D");
else printf("Fail");
return 0;
}#include <iostream>
using namespace std;
int main() {
int marks = 78;
if (marks >= 80) cout << "Grade A";
else if (marks >= 70) cout << "Grade B";
else if (marks >= 60) cout << "Grade C";
else if (marks >= 50) cout << "Grade D";
else cout << "Fail";
return 0;
}public class Main {
public static void main(String[] args) {
int marks = 78;
if (marks >= 80) System.out.println("Grade A");
else if (marks >= 70) System.out.println("Grade B");
else if (marks >= 60) System.out.println("Grade C");
else if (marks >= 50) System.out.println("Grade D");
else System.out.println("Fail");
}
}marks = 78
if marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 50:
print("Grade D")
else:
print("Fail")Online Working Example
4.6 Nested Conditions
Nested conditions mean placing one if statement inside another if statement. This is used when one decision depends on another decision.
Working Example: Login Check
#include <stdio.h>
#include <string.h>
int main() {
char username[] = "admin";
char password[] = "1234";
if (strcmp(username, "admin") == 0) {
if (strcmp(password, "1234") == 0) printf("Login Successful");
else printf("Wrong Password");
} else {
printf("Wrong Username");
}
return 0;
}#include <iostream>
using namespace std;
int main() {
string username = "admin";
string password = "1234";
if (username == "admin") {
if (password == "1234") cout << "Login Successful";
else cout << "Wrong Password";
} else {
cout << "Wrong Username";
}
return 0;
}public class Main {
public static void main(String[] args) {
String username = "admin";
String password = "1234";
if (username.equals("admin")) {
if (password.equals("1234")) System.out.println("Login Successful");
else System.out.println("Wrong Password");
} else {
System.out.println("Wrong Username");
}
}
}username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Wrong Username")Online Working Example
4.7 Logical Expressions
Logical expressions combine multiple conditions using AND, OR and NOT.
Working Example: Scholarship Eligibility
#include <stdio.h>
int main() {
int marks = 85, attendance = 92;
if (marks >= 80 && attendance >= 90) {
printf("Eligible");
} else {
printf("Not Eligible");
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int marks = 85, attendance = 92;
if (marks >= 80 && attendance >= 90) {
cout << "Eligible";
} else {
cout << "Not Eligible";
}
return 0;
}public class Main {
public static void main(String[] args) {
int marks = 85, attendance = 92;
if (marks >= 80 && attendance >= 90) {
System.out.println("Eligible");
} else {
System.out.println("Not Eligible");
}
}
}marks = 85
attendance = 92
if marks >= 80 and attendance >= 90:
print("Eligible")
else:
print("Not Eligible")Online Working Example
4.8 Decision Making Applications
Conditional statements are used in real applications such as result systems, login systems, discount calculators, loan eligibility checkers and attendance monitoring.
Working Example: Discount Calculator
If purchase amount is RM500 or above, give 10% discount. Otherwise, no discount.
4.9 Practical Activities
Activity 1: Adult Checker
Create an if statement program to check whether age is 18 or above.
Activity 2: Pass or Fail
Create an if-else program to check whether marks are 50 or above.
Activity 3: Grade System
Create an if-elif-else / else-if ladder for A, B, C, D and Fail.
Activity 4: Login System
Create nested conditions to check username and password.
Mini Project
Create a scholarship eligibility system using marks, attendance and family income in all four languages.
4.10 Interactive Final Assessment Quiz
Each correct answer gives +1 mark. Each wrong answer gives -0.5 mark.
1. Conditional statements help programs make decisions.
2. Python uses which keyword for else-if?
3. C, C++ and Java use braces { } for condition blocks.
4. Which operator means AND in C, C++ and Java?
5. Nested condition means one condition inside another condition.
6. if-else is used when there are two possible paths.
7. In Python, condition blocks are controlled by indentation.
8. Which operator means OR in C, C++ and Java?
9. Grade systems can be built using if-elif-else / else-if ladder.
10. Logical expressions cannot combine multiple conditions.
Your Score: 0
4.11 Module Summary
This module explained conditional statements using C, C++, Java and Python. Learners practiced if, if-else, else-if / elif, nested conditions, logical expressions and decision-making applications through working online examples.