Conditions and Decision Making
Learn to make decisions in your code. Control program flow with if, else, and logical operators.
Learning Objectives:
- β’Make decisions with if/else statements
- β’Use comparison operators (==, !=, <, >, <=, >=)
- β’Combine conditions with logical operators (and, or, not)
- β’Handle multiple branches with if/elif/else
Conditions and Decision Making
Why Conditions Matter
Real programs don't execute the same code every time. They make decisions based on conditions.
For example:
- "If the user is an adult, show the premium content"
- "If the password is correct, allow login"
- "If the temperature is below 0, display a warning"
Conditions let your program adapt to different situations.
Simple Conditions: if
The simplest decision is if something is true, execute a block.
Python
pythonage = 18 if age >= 18: print("You are an adult")
C
c#include <stdio.h> int main() { int age = 18; if (age >= 18) { printf("You are an adult\n"); } return 0; }
Java
javapublic class Main { public static void main(String[] args) { int age = 18; if (age >= 18) { System.out.println("You are an adult"); } } }
Comparison Operators
To write conditions, you compare values using operators:
| Operator | Meaning | Example |
|----------|---------|---------|
| == | Equal to | 5 == 5 β true |
| != | Not equal to | 5 != 3 β true |
| < | Less than | 3 < 5 β true |
| > | Greater than | 5 > 3 β true |
| <= | Less than or equal | 5 <= 5 β true |
| >= | Greater than or equal | 5 >= 3 β true |
Example
pythonx = 10 print(x > 5) # true print(x == 10) # true print(x != 0) # true print(x < 5) # false
if/else: Two Branches
If you want to execute one block when true and another when false, use if/else:
Python
pythonage = 15 if age >= 18: print("You are an adult") else: print("You are a minor")
C
c#include <stdio.h> int main() { int age = 15; if (age >= 18) { printf("You are an adult\n"); } else { printf("You are a minor\n"); } return 0; }
Java
javapublic class Main { public static void main(String[] args) { int age = 15; if (age >= 18) { System.out.println("You are an adult"); } else { System.out.println("You are a minor"); } } }
Multiple Branches: if/elif/else
Sometimes you have more than two cases.
Python
pythongrade = 85 if grade >= 90: print("A") elif grade >= 80: print("B") elif grade >= 70: print("C") else: print("F")
C / C++ / Java (if/else if/else)
c#include <stdio.h> int main() { int grade = 85; if (grade >= 90) { printf("A\n"); } else if (grade >= 80) { printf("B\n"); } else if (grade >= 70) { printf("C\n"); } else { printf("F\n"); } return 0; }
Key: Each elif (or else if) is checked in order. Once one is true, the rest are skipped.
Logical Operators
You often need to combine multiple conditions.
and(or&&): both conditions must be true.or(or||): at least one condition must be true.not(or!): reverses the condition.
Python
pythonage = 25 has_license = True # Both conditions must be true if age >= 18 and has_license: print("Can drive") # At least one must be true is_weekend = True is_holiday = False if is_weekend or is_holiday: print("No work today") # Reverse a condition is_raining = True if not is_raining: print("Go outside") else: print("Stay inside")
C
c#include <stdio.h> #include <stdbool.h> int main() { int age = 25; bool has_license = true; if (age >= 18 && has_license) { printf("Can drive\n"); } bool is_weekend = true; bool is_holiday = false; if (is_weekend || is_holiday) { printf("No work today\n"); } bool is_raining = true; if (!is_raining) { printf("Go outside\n"); } else { printf("Stay inside\n"); } return 0; }
Java
javapublic class Main { public static void main(String[] args) { int age = 25; boolean has_license = true; if (age >= 18 && has_license) { System.out.println("Can drive"); } boolean is_weekend = true; boolean is_holiday = false; if (is_weekend || is_holiday) { System.out.println("No work today"); } boolean is_raining = true; if (!is_raining) { System.out.println("Go outside"); } else { System.out.println("Stay inside"); } } }
Nested Conditions
You can place conditions inside other conditions:
Python
pythonage = 25 has_license = True is_sober = True if age >= 18: if has_license: if is_sober: print("Safe to drive") else: print("Don't drive β you're not sober") else: print("Get a license first") else: print("You're too young")
Better (using logical operators):
pythonif age >= 18 and has_license and is_sober: print("Safe to drive")
Real-World Example: Grade Classifier
Problem: Read a grade (0-100) and classify it.
Python:
pythongrade = int(input("Enter grade: ")) if grade >= 90: print("A - Excellent") elif grade >= 80: print("B - Good") elif grade >= 70: print("C - Satisfactory") elif grade >= 60: print("D - Needs improvement") else: print("F - Failing")
Java:
javaimport java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int grade = sc.nextInt(); if (grade >= 90) { System.out.println("A - Excellent"); } else if (grade >= 80) { System.out.println("B - Good"); } else if (grade >= 70) { System.out.println("C - Satisfactory"); } else if (grade >= 60) { System.out.println("D - Needs improvement"); } else { System.out.println("F - Failing"); } } }
C:
c#include <stdio.h> int main() { int grade; scanf("%d", &grade); if (grade >= 90) { printf("A - Excellent\n"); } else if (grade >= 80) { printf("B - Good\n"); } else if (grade >= 70) { printf("C - Satisfactory\n"); } else if (grade >= 60) { printf("D - Needs improvement\n"); } else { printf("F - Failing\n"); } return 0; }
Finding Minimum and Maximum
A common task: given two numbers, find the smaller/larger one.
Finding the Minimum
pythona = 10 b = 5 if a < b: min_value = a else: min_value = b print(min_value) # 5
Or more elegantly:
pythonmin_value = a if a < b else b
Finding the Maximum of Three Numbers
pythona, b, c = 10, 5, 8 max_value = a if b > max_value: max_value = b if c > max_value: max_value = c print(max_value) # 10
Key Takeaways
- Conditions let programs make decisions
- Comparison operators (
==,!=,<,>,<=,>=) test relationships - if/else executes different code based on a condition
- if/elif/else handles multiple cases
- Logical operators (and, or, not) combine conditions
- Nested conditions allow complex logic
- Use parentheses to clarify complex boolean expressions
Summary
You now understand how to make decisions in your code. This is a fundamental programming skill. With variables, input, arithmetic, and conditions, you can write programs that solve real-world problems.
You've now completed Level 1: Fundamentals. You have learned:
- Output
- Loops
- Variables and Calculations
- Reading Input
- Conditions and Decision Making
Progress: 5/8 chapters complete in Level 1
Exercises
Exercise 1: Simple Condition
Use a condition to decide which result to display.
Exercise 2: If and Else
Choose between two outcomes with an if/else statement.
Exercise 3: If, Elif, and Else
Handle several possible cases with a conditional chain.
Exercise 4: Nested Conditions
Combine nested conditions to handle the cases in the exercise.
Exercise 5: Minimum and Maximum
Compare values and find the minimum and maximum requested.
Exercise 6: Grade Classifier
Use conditions to classify a grade according to the exercise rules.