Code viewer for World: Control Flow (Loops): Basi...
"""
Topic: Control Flow (Loops): Basic for Loops with range()
Purpose: Learn how to repeat code using for loops and the range() function
Output: Shows how Python executes loops a fixed number of times
"""

# -----------------------------
# 1. Basic for loop
# -----------------------------
# range(5) generates numbers from 0 to 4
for i in range(5):
    print(i)

# -----------------------------
# 2. for loop with a custom start
# -----------------------------
# range(start, stop)
for i in range(1, 6):
    print("Count:", i)

# -----------------------------
# 3. for loop with a step value
# -----------------------------
# range(start, stop, step)
for i in range(0, 10, 2):
    print("Even number:", i)

# -----------------------------
# 4. Looping in reverse
# -----------------------------
# Negative step counts backwards
for i in range(5, 0, -1):
    print("Countdown:", i)

# -----------------------------
# 5. Using loop variable in logic
# -----------------------------
# Combining loops with conditions
for i in range(1, 11):
    if i % 2 == 0:
        print(i, "is even")
    else:
        print(i, "is odd")

# -----------------------------
# 6. Notes
# -----------------------------
# 1. for loops are used when the number of iterations is known.
# 2. range() does not include the stop value.
# 3. The loop variable updates automatically each iteration.
# 4. range() can take 1, 2, or 3 arguments.
# 5. for loops are commonly used for counting and repetition.