Code viewer for World: Control Flow (Loops): Basi...
"""
Topic: Control Flow (Loops): Basic while Loops
Purpose: Learn how to repeat code using while loops
Output: Shows how Python executes code while a condition remains True
"""

while True:
    pass
# -----------------------------
# 1. Basic while loop
# -----------------------------
# The loop runs as long as the condition is True
count = 1

while count <= 5:
    print("Count:", count)
    count += 1

# -----------------------------
# 2. while loop with user-like condition
# -----------------------------
# Simulating a condition that changes over time
energy = 3

while energy > 0:
    print("Energy level:", energy)
    energy -= 1

print("Out of energy!")

# -----------------------------
# 3. while loop with conditional logic
# -----------------------------
number = 1

while number <= 10:
    if number % 2 == 0:
        print(number, "is even")
    else:
        print(number, "is odd")
    number += 1

# -----------------------------
# 4. Preventing infinite loops
# -----------------------------
# Always make sure the condition eventually becomes False
timer = 5

while timer > 0:
    print("Timer:", timer)
    timer -= 1

print("Time's up!")

# -----------------------------
# 5. Notes
# -----------------------------
# 1. while loops run as long as the condition is True.
# 2. Be careful to update variables inside the loop.
# 3. Forgetting to update the condition causes infinite loops.
# 4. while loops are useful when the number of iterations is unknown.
# 5. Use while loops for condition-based repetition.