TOPIC 3: Conditional Statements & Loops
1. Conditional Statements
Conditional statements allow you to control the flow of your program based on specific conditions.
- if: If the condition is True, execute the code inside the block.
- elif: If the previous conditions are False, check this condition.
- else: Execute the code if all previous conditions are False.
Example 1:
age = 18
if age >= 18:
print("You can vote!")
else:
print("You cannot vote yet.")
Example 2:
number = 10
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
So, what does it mean?
If a number is divisible by 2 with no remainder, then it’s an even number.
Here, 10 % 2 is 0, so the condition is True, and the program prints:
Even number
If you had:
number = 7
Then 7 % 2 is 1, so the condition is False, and it would go to the else block (if it exists), which will print:
Odd number