Flow control statements allow your program to make decisions and repeat actions. They are the building blocks of program logic, enabling your code to respond to different conditions and process data efficiently.
Conditional statements allow your program to execute different code blocks based on whether conditions are true or false.
# Basic if statement
if temperature > 30:
print("It's hot outside!")
# if-else statement
if age >= 18:
print("You can vote")
else:
print("You cannot vote yet")
# if-elif-else statement
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal |
| >= | Greater than or equal |
| and | Both conditions must be True |
| or | At least one condition must be True |
| not | Reverses the boolean value |
# Using logical operators
if age >= 18 and has_license:
print("You can drive")
if is_weekend or is_holiday:
print("Time to relax!")
if not is_raining:
print("Let's go for a walk")Execute code repeatedly while a condition is true.
# Basic while loop
count = 0
while count < 5:
print(count)
count += 1
# While with break
while True:
user_input = input("Enter 'quit' to exit: ")
if user_input == "quit":
break
# While with continue
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue # Skip even numbers
print(i)Iterate over sequences using the range() function.
# Basic for loop
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
# Range with start and stop
for i in range(1, 6):
print(i) # Prints 1, 2, 3, 4, 5
# Range with step
for i in range(0, 10, 2):
print(i) # Prints 0, 2, 4, 6, 8
# Nested loops
for i in range(3):
for j in range(3):
print(f"i={i}, j={j}")The else clause executes when a loop completes normally (without hitting a break statement).
# For-else example
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(f"{n} is not prime")
break
else:
print(f"{n} is prime")A null operation that does nothing. Used as a placeholder.
# Using pass as placeholder
if condition:
pass # TODO: implement this later
else:
handle_error()In Python, many values can be evaluated as True or False in boolean contexts.
# Falsy values: False, None, 0, 0.0, "", [], {}, ()
# Everything else is truthy
items = []
if not items: # Empty list is falsy
print("No items")
name = "Alice"
if name: # Non-empty string is truthy
print(f"Hello, {name}")Visualize how conditional statements flow through different execution paths. Select a code example and step through the execution.
Step through loop execution to see how variables change and understand loop behavior.
Build complex conditions by dragging and dropping operators and values. See how they evaluate in real-time.
Click to add to workspace
Click components from the palette to build a condition
if condition:
# code block
elif condition:
# code block
else:
# code blockwhile condition:
# code block
if exit_condition:
break
if skip_condition:
continuefor var in range(start, stop, step):
# code block
# range(5) → 0,1,2,3,4
# range(2,7) → 2,3,4,5,6
# range(0,10,2) → 0,2,4,6,8| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| and | True if both are True |
| or | True if at least one is True |
| not | Reverses the boolean |
Everything else!
| break | Exit the loop immediately |
| continue | Skip to next iteration |
| pass | Do nothing (placeholder) |
for item in items:
if found:
break
else:
# Runs if no breakwhile True:
user_input = input("Enter a positive number: ")
if user_input.isdigit() and int(user_input) > 0:
number = int(user_input)
break
print("Invalid input. Try again.")count = 0
for item in items:
if item > 10:
count += 1
print(f"Found {count} items greater than 10")for item in items:
if item == target:
print(f"Found {target}!")
break
else:
print(f"{target} not found")Answer the following questions to test your understanding of Python flow control.