how to repeat code in python until a condition is met
a = 1 b = 1 while (a<10): print ('Iteration',a) a = a + 1 b = b + 1 if (b == 4): break print ('While loop terminated')
Here is what the above code is Doing:
1. The while loop is set to run as long as a is less than 10.
2. The first time the loop runs, a is 1, so the loop runs.
3. The loop prints 'Iteration 1' and then adds 1 to a and b.
4. The loop then checks if b is equal to 4. Since it isn't, the loop continues.
5. The loop prints 'Iteration 2' and then adds 1 to a and b.
6. The loop then checks if b is equal to 4. Since it isn't, the loop continues.
7. The loop prints 'Iteration 3' and then adds 1 to a and b.
8. The loop then checks if b is equal to 4. Since it is, the loop breaks.
9. The loop then prints 'While loop terminated' and ends.