Python while loop Statements
Loops are one of the most important features in computer programming languages . As the name suggests is the process that get repeated again and again . It offer a quick and easy way to do something repeated until a certain condition is reached. Every loop has 3 parts:
Python while loop
In Python, while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. That means, while loop tells the computer to do something as long as the condition is met. It consists of condition/expression and a block of code. The condition/expression is evaluated, and if the condition/expression is true, the code within the block is executed. This repeats until the condition/expression becomes false.
For example, if I initialize the value of a variable x as 0 and set the condition x < =5 then the condition will be held true. But if I set the condition x>=5 the condition will become false. After checking the condition in while clause, if it holds true, the body of the loop is executed. While executing the body of loop it can update the statement inside while loop . After updating, the condition is checked again. This process is repeated as long as the condition is true and once the condition becomes false the program breaks out of the loop.
Here the conditional of x < =5 (while(x < =5):) and x was previously declared and set equal to 0 (x=0). So, the first item printed out was 0 (print(x)), which makes sense. In the next line x+=1 means x = x+1, now the value of x = 1. After updating x , the condition is checked again. This process is repeated as long as the condition is true and once the condition becomes false the program breaks out of the loop . Of course, once a becomes equal to 5, we will no longer run through the loop.
Python while loop: break and continue
Python provides two keywords that terminate a loop iteration prematurely: break and continue.
- break leaves a loop.
- continue jumps to the next iteration.
break statement in Python while loop
Sometimes it's necessary to exit from a Python while loop before the loop has finished fully iterating over all the step values. This is typically achieved by a "break" statement.
In the above example, when the condition x>20, the break statement executed and immediately terminated the while loop and the program control resumes at the next statement.
continue statement in Python while loop
The continue statement in Python while loop is used when we want to skip one or more statements in loop's body and to transfer the control to the next iteration.
In the above example, we can see in the output the 30 is missing. It is because when the condition x==30 the loop encounter the continue statement and control go back to start of the loop.
Else clause on Python while statement
This is a unique feature of Python and not found in most other programming languages. The else clause in Python while loop is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed.
In the above example, you can see the condition is (x < =10). Up to the condition, the while block executed and the final value of x=11. Then the condition is false and the control goes to else clause of while loop and print the statement.
Here the initial value of x=11 and condition is (x < =10). So there is no way to enter inside the while loop . So, the control directly goes to else block of while loop and print the statement.
Nested while Loops
A nested while loop is a loop within a while loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer while loop triggers the inner while loop , which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.
One-Line while Loops
As with an if statement, a Python while loop can be specified on one line. If there are multiple statements in the loop code block that makes up the loop body , they can be separated by semicolons (;):