Chrome Extension

While loop python

Description of while loop execution in Python
counter = 1
while counter < 6:
    print(counter)
    counter += 1

#WITH ELSE STAEMENT
while counter < 6:
    print(counter)
    counter += 1
else:
    print("counter is greater or equals to 6");
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

You can use a while loop in Python to execute statements until the condition is true. In the code snippet, we are printing the counter value till it is less than 6. 

You can also use an else statement with a while loop that will run right after the condition is false.

Using the continue statement

You can use the continue statement in python to pass an iteration inside the while loop. Below is the example code.

counter = 0
while counter < 6:
  counter += 1
  if counter == 2:
    continue
  print(counter)
Was this helpful?
0 Comments
Join Devsheet Ask a Question