Python Loops @ Coding:
While Loops:
The while loop checks if the codition is True and executes the code below (and within it) until the condition is no longer met.
Break Statement
The break statement allows you to end the loop even if the condition is still met / True.
num = 0
while num < 7:
print(num)
num += 1
if num == 5:
break
print(f"Break you reached the number")
Continue Statement:
The continue statment allows you to stop the current iteration (everytime the loop is carried out) and starts the next one.
Else Statement:
The else statement shown in the Basics Page can also be used in a while loop.
For Loops:
The for loops are used to iterate (go over) over a list, tuple, dictionary, set or a string.
list = [1,2,3,4,5,6,7]
for x in list:
print(x)
# Below is IF statement inside the for loop to check if the variable x is 7 and if so it prints "end".
if x == 7:
print("end")
Tip
We reccomend you learn about Data Structures before trying out with loops. Click Here
Other Statements:
You can continue to use if, break and continue statements in for loops in the "same" way.