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:
  1. Initialization
  2. Condition
  3. Updation

How to python while loop

Python while loop

Syntax
while (condition) : statement(s)
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.
initialization; while(condition) { //Code block to execute something }
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. example
x=0 while(x < =5): print(x) x+=1
output
0 1 2 3 4 5
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.
  1. break leaves a loop.
  2. continue jumps to the next iteration.

break statement in Python while loop


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. example
x=10 while True: print (x) x+=2; if x>20: break print("After Break")
output
10 12 14 16 18 20 After Break
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


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. example
x=0 while x < 50: x+=10 if x==30: continue print (x) print("Loop Over")
output
10 20 40 50 Loop Over
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

Syntax
while (condition) : statement(s) else statement(s)
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. example
x = 5 while (x <=10): print (x ) x = x +1 else: print(x , " Inside Else")
output
5 6 7 8 9 10 11 Inside Else
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. example
x = 11 while (x <=10): print (x ) x = x +1 else: print(x, " Inside Else")
output
11 Inside Else
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


python nested while loop
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. example
i = 1 while i <= 5: j = 1 while j <= i: print(j, end = '') j += 1 print() i += 1
output
1 12 123 1234 12345

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 (;): example
x,y = 0,5 while (x<y): x +=1; print(x);
output
1 2 3 4 5

is same as:

x = 0 y = 5 while x < y: x +=1 print(x)
output
1 2 3 4 5

Python Infinite while Loop


python infinite while loop
As the name suggests, an infinite loop runs forever. In the context of a computer programming, this means that the loops runs until the program terminates . An infinite loop can be useful in a program that is always looking for some kind of input. While it is waiting for the input, it can be sitting idle. We can program an infinite loop using Python while statement . If the condition of while loop is always True, we get an infinite loop. example
# Press Ctrl + c to exit from loop while True: print ("This is an infinite Loop")

How to Emulate a do-while loop in Python?

Python doesn't have do-while loop . The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluated at the bottom of the loop body.
condition = True while condition: # loop body here print("Execute at least one time") condition = False
output
Execute at least one time