🔀 Control Flow

If, For, While - Decision Making

Control Flow Statements

Control flow determines the order in which code executes. Use if/elif/else for decisions, for loops for iteration, and while loops for repetition.

💻 If Statements

age = 18

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teenager")
else:
    print("Child")

# Ternary operator
status = "Adult" if age >= 18 else "Minor"

🔄 For Loops

# Iterate over list
for fruit in ["apple", "banana", "cherry"]:
    print(fruit)

# Range
for i in range(5):  # 0 to 4
    print(i)

for i in range(1, 6):  # 1 to 5
    print(i)

for i in range(0, 10, 2):  # 0,2,4,6,8
    print(i)

# Enumerate (get index and value)
fruits = ["apple", "banana"]
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

🔁 While Loops

count = 0
while count < 5:
    print(count)
    count += 1

# Break and continue
for i in range(10):
    if i == 3:
        continue  # Skip 3
    if i == 7:
        break  # Stop at 7
    print(i)

🎯 Key Takeaways