Copy the following code on the left in your IDE to execute the process shown in the image on the right.
While Loop
In this example, the while loop is complete when the variable (x) reaches 6. This is shown in the "Final output" statement.
x = 0 Jupyter Notebook View
while x < 6:
x+=2
print("Looping output:", x)
print("Final output:", x)
While Loop with Break
In this example, the WHILE loop has been updated to run until x reaches 12 while the IF statement breaks the loop at 6. Due to this, the loop will not reach 12 and the final output will be 6.
x = 0 Jupyter Notebook View
while x < 12:
x+=2
if x == 6:
break
print("Looping output:", x)
While Loop with Continue
In this example, continue is used to interrupt the loop when x becomes 6. Therefore, 6 will not be printed as a "Looping output:".
x = 0 Jupyter Notebook View
while x < 12:
x+=2
if x == 6:
continue
print("Looping output:", x)
print("Final output:", x)