How to restart a loop 1

How to restart a loop

i=2
while i < n:
    if something:
       do something
       i += 1
    else: 
       do something else  
       i = 2 #restart the loop  

Here is what the above code is Doing:
1. The while loop is set to run as long as i is less than n.
2. The first time through the loop, i is 2, so the loop runs.
3. The loop runs until it hits the if statement.
4. If the if statement is true, the loop runs the first block of code, then adds 1 to i.
5. If the if statement is false, the loop runs the second block of code, then sets i back to 2.
6. The loop continues until i is no longer less than n.

Similar Posts