Python - While Loop
With the loop while you can execute statements as long as the condition is true.
i = 1
while i < 8:
print(i)
i += 1
The loop while requires a variable to stop. In our case it's the variable i, don't forget to increment the variable i otherwise the loop runs indefinitely.
break in while
With the statement break the loop can be stopped even if the condition is true.
i = 1
while i < 8:
print(i)
if i == 4:
break
i += 1
continue in while
With the statement continue we can skip the current iteration and move on to the next iteration.
i = 0
while i < 8:
i += 1
if i == 4:
continue
print(i)
else in while
With the statement else you can skip executing a block of statements once the condition of the loop while is false.
i = 1
while i < 8:
print(i)
i += 1
else:
print("i is greater than 8")