Free Online Code Editor
Repeat actions with for and while loops.
# For loop
print("For loop:")
for i in range(5):
print(f" Count: {i}")
# Loop through list
fruits = ["apple", "banana", "cherry"]
print("\nFruits:")
for fruit in fruits:
print(f" {fruit}")
# While loop
print("\nWhile loop:")
count = 0
while count < 3:
print(f" Count: {count}")
count += 1
# Break and continue
print("\nBreak example:")
for i in range(10):
if i == 5:
break
print(f" {i}", end=" ")For loops iterate over sequences. While loops repeat while a condition is true. Break exits loops, continue skips to next iteration.