Python While Loops


Python Loops

Two primitive loop commands are in Python :

  • while loops
  • for loops
You can also search for these topics, python loops programs, explain the python loops, advance of loops in python, define the all loops in python, python different loops, loop keyword in python, types of loops in python.

The while Loop

We can make a number of statements with the while loop, provided that a condition is true.

Example :- Print i as long as i is less than 6 :

i = 1
while i < 6:
  print(i)
  i += 1

Output :-

1
2
3
4
5

Note : keep in mind that the loop will continue forever without increment of i value, or increase i.

In the while loop, we need to define an index variable i in this instance, which we set to 1.

You can also search for these topics, python the while loop exercises, multiple condition used in python while loop, while loop assignment in python, how to list the while loop condition in python, python while loop and the code, while loop do nothing using python, python while loop doesn't work, working of python function with while loop, check the error handling in python while loop, Example for while loop using python.

The break Statement

We can end the loop even if the condition is true with the break statement :

Example :- Exit the loop when i is 3 :

i = 1
while i < 6:
  print(i)
  if (i == 3):
    break
  i += 1

Output :-

1
2
3

You can also search for these topics, python the break statement example, check the error in python break statement, working of function in break statement using python, python breaking statement not working, break statement loop in python, Example for break statement using python.

The continue Statement

We can stop the current iteration with the continue statement and proceed with the next :

Example :- When i is 3 stop the current iteration and Continue to the next iteration:

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

Output :-

1
2
4
5
6

Note that number 3 is missing in the result.

You can also search for these topics, python continue statement example, python continue statement not working, use the python conditional error with continue statement, how does python break statement working, the continue statement syntax error using python, python program using continue statement, Example for python continue statement.

The else Statement

With the else statement, once the condition is inaccurate, we can run a block of code :

Example :- When the condition is false, print a message :

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

Output :-

1
2
3
4
5
i is no longer less than 6

You can also search for these topics, python else statement do nothing, define the python else statement invalid syntax, check the working of python else statement, how syntax error appeared in else statement with python, use of multiple conditions python else statement, return statement else statement in python, python update the value of else statement, Example for else statement using python.