I'm not sure if we learn this concept in class, but you can use continue
and break
to skip to the rest of the loop.
For example, with continue
, it will skip the rest of the code inside a loop for the current iteration to the next iteration:
number = 0
while number < 10:
if number == 5:
continue
print(number)
Output:
1
2
3
4
6
7
8
9
10
With break
, it will immediately stop the the loop:
number = 0
while number < 10:
number = number + 1
if number == 5:
break
print(number)
Output:
1
2
3
4