The for
loop is used to iterate over a sequence, such as an array or a range of numbers:
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
for item in my_array:
print(item) # Prints each item in my_array
The while
loop is used to repeatedly execute a block of code as long as a certain condition is true:
var i = 0
while i < 5:
print(i) # Prints 0, 1, 2, 3, 4
i += 1
The break
statement is used to exit a loop prematurely:
for i in range(10):
if i == 5:
break
print(i) # Prints 0, 1, 2, 3, 4
The continue
statement is used to skip the rest of the current iteration and proceed to the next one:
for i in range(5):
if i == 2:
continue
print(i) # Prints 0, 1, 3, 4
Comments