3: Control Flow
3.1 Conditionals: Making Decisions with if
, elif
, and else
In real life, you make decisions all the time: "If it’s sunny, I’ll go for a walk, if not, I’ll stay home." In programming, conditional statements do exactly that: they allow your code to make decisions based on whether a condition is true or false.
if
statement
The if
statement is the most basic one. It executes a block of code only if a condition is True
.
age = 18
if age >= 18:
print("You are an adult.")
else
statement
The else
statement is used together with if
to execute an alternative block of code when the if
condition is False
.
temperature = 15
if temperature > 25:
print("It’s hot, wear light clothes.")
else:
print("It’s cold, dress warmly.")
elif
(else if) statement
The elif
statement lets you check multiple conditions sequentially. It’s short for "else if" and helps you avoid nesting too many if
statements.
score = 85
if score >= 90:
print("You got an A, excellent!")
elif score >= 80:
print("You got a B, well done!")
elif score >= 70:
print("You got a C, keep practicing!")
else:
print("You need to study more.")
3.2 Comparison and Logical Operators
To create conditions for your if
statements, you need comparison operators and logical operators.
Comparison operators
They compare two values and return a boolean result (True
or False
).
==
Equal to!=
Not equal to>
Greater than<
Less than>=
Greater than or equal to<=
Less than or equal to
x = 10
y = 20
print(x == y) # Output: False
print(x < y) # Output: True
print(x != y) # Output: True
Logical operators
They combine multiple boolean conditions.
and
: ReturnsTrue
if both conditions areTrue
.or
: ReturnsTrue
if at least one condition isTrue
.not
: Reverses the boolean value (fromTrue
toFalse
and vice versa).
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive.")
if age < 18 or not has_license:
print("You cannot drive.")
3.3 Loops: Repeating Tasks with for
and while
Loops let you run a block of code repeatedly, which is essential for automating tasks.
for
loop
The for
loop is used to iterate over a sequence (such as a list, tuple, dictionary, or string).
# Iterate over a list
fruits = ["apple", "banana", "grape"]
for fruit in fruits:
print(fruit)
# Iterate with a range of numbers
for i in range(5): # range(5) generates numbers from 0 to 4
print(i)
while
loop
The while
loop runs as long as a condition is True
. It’s important that the condition eventually becomes False
to avoid an infinite loop.
counter = 0
while counter < 5:
print(counter)
counter += 1 # Same as counter = counter + 1
break
and continue
statements
break
: Stops the loop execution and exits it completely.continue
: Skips the current iteration and moves to the next one.
for number in range(10):
if number == 3:
continue # Skip 3 and continue with the next iteration
if number == 8:
break # Stop the loop completely
print(number)
This module has equipped you with the tools to make your programs dynamic, capable of making decisions and repeating actions. In the next module, you’ll discover how to organize your code into reusable blocks using functions.