In this tutorial, we are going to understand the while loop in python.
What is Python While Loop?
The while loop in python is used to execute a set of statements until the given test expression or condition is true.
Syntax of python while loop.
As we can see in the above syntax, we are first using the while keyword to implement the while loop, and we are giving test conditions, and in the while loop, we are giving the body of while with some statements.
The loop will test the condition, and it will run statements in the body only if the giving condition is true, just like for loop while in python again checking the condition. Once the condition gets false it will exit from the loop.
Example of Python while loop
When we run the following program, we will get the below output
Note: remember to add increment in i, or else the loop will continue forever.
Same as numbers, we can also print strings inside a while loop.
Example of while loop using string
The output of the above program:
As in python index starts from 0, it is only printing strings for 4 times only. Because in python calculations begin to like :
Nested while loops
In nested while loop, we can add while loop within while loop as a body of while loop.
Let see an example of nested while loops.
When we execute this program, we will get the following result.
The output of nested while loop
While loop with else statement
Using else statements in the while loop can run a set of code once when the given condition is no longer true.
Example of While loop with else statement.
The output of following the program.
Loop Control Statements
Loop control statements mean a conditional statement as a body of a while loop. We can add multiple condition statements using a conditional statement based on the loop sequence of the loop.
Let see an example of the loop control statement:
When we run this program, we will get the following output:
In the code above program, we can see while loop executes the statements in between the while loop body and when every while loop is getting 5 as a value. It will go to if conditional statement, and the string will be executed once the value of the number is 5.
With the break statement in the while loop, we can stop the while loop even after the given condition is true:
You can learn more about python break statements from here.
The Python continue statement immediately terminates the current loop iteration.
Learn more about python continue statements from here.
One-Line while Loops
As with an if statement, a while loop can be specified on one line. If there are multiple statements in the block that makes up the loop body, they can be separated by semicolons (;)
Infinite While Loops
As the name suggests, an infinite loop will continually execute statements in a while loop until the user stops manually, like we write a while loop that theoretically never ends.
Sound interesting. Let sees the example.
Same code we can write differently as follow.
We are printing Infinite continuously until we stop using the Crt + X key using the keyboard and are also throwing an error.