← Back to Dashboard

Control Flow Conditionals Loops: Concept Notes

"""
Topic 03: Control Flow (Conditionals & Loops) - Concept Notes

Control flow statements allow you to control the execution order of your code based on conditions.

1. Conditional Statements (if-elif-else)
   - Checks if a condition is True before running a block of code.
   - elif (short for else if) allows checking multiple conditions.
   - else runs if none of the preceding conditions were met.

2. Loops
   - for loops: Iterate over a sequence (like a range, list, or string).
   - while loops: Repeatedly execute a block as long as a condition remains True.

3. Flow Control Keywords
   - break: Exits the loop immediately.
   - continue: Skips the current iteration and moves to the next.
   - pass: A null statement, used as a placeholder.
"""

# 1. if-elif-else
age = 18
if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
else:
    print("Adult")

# 2. for loop (Range)
print("\nCounting to 5:")
for i in range(1, 6):
    print(i, end=" ")

# 3. while loop
print("\n\nCountdown:")
count = 5
while count > 0:
    print(count)
    count -= 1

# 4. break and continue
print("\nEven numbers skip 4:")
for i in range(2, 11, 2):
    if i == 4:
        continue
    if i == 8:
        print("Eight is enough, stopping.")
        break
    print(i)