When considering a python while loop we provide condition before, that is in first step then we add increment or decrement later. Which is different when compared to for loop implementation.
Python while loop is implemented to run a piece of code until a certain condition is satisfied or requirement is reached i.e, a while loop is used to repeatedly execute a block of code as long as a condition is true.
The code block under the while loop will be executed repeatedly as long as the condition is true. When the condition becomes false, the program will exit the loop and continue with the next line of code.
It’s important to ensure that the condition in a while loop will eventually become false, otherwise the loop will continue indefinitely and the program will hang. To avoid this, you can use a break statement to exit the loop if a certain condition is met, or a counter variable that limits the number of times the loop can execute.
Now let’s start implementing a sample while loop to understand the concept.
x = 10 while x < 10; print("Welcome")
When you run the above code then you will never observe any output because x is never less than 10.
And when you slight change the code by making x = 1 instead of 10.
x = 1 while x < 10; print("Welcome")
Output :
Welcome Welcome Welcome Welcome Welcome Welcome Welcome . . .
Python While Loop Video Tutorial :
Go through the below tutorial on python while loop for detailed implementation.
The output is never ending because we have not provided any increment or decrement conditions.
Now try to add them and run the code.
x = 1 while x < 10; print("Welcome") x+=1
Output :
Welcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome
Now welcome is printed 9 times as condition is satisfied at last step i..e, x > 10 so 10th time its not printed.
Indentation is the key step because without proper indentation code is not recognized and errors may arise.
Nested While Loop :
We can add nested while loops inside a while loop so that we can have multiple loops going to process data.
A nested while loop in Python is a loop that is placed inside another loop. The nested loop will execute its code block multiple times for each iteration of the outer loop.
It’s very simple to implement if you understand the concept.
x = 1 y = 5 while x < 10; print("Welcome") while y < 10; print("Python"); y+=1 x+=1
Output :
You can clearly observe that initially Welcome is printed later Python is printed for 5 times and followed by Welcome again if you observe the code it says the same.
Welcome Python Python Python Python Python Welcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome
We can observe python is been printed in output.
I have clearly explained the flow in video tutorial attached with debug process.
break :
Using break condition in while loop is super easy again as the word states it breaks the flow and returns control.
The break
statement can be used inside a while loop to exit the loop prematurely, even if the loop condition has not yet become false. The break
statement immediately terminates the innermost loop that it is contained within.
x = 1 while x < 10; print("Welcome") if x == 5 break; x+=1
Output :
Welcome Welcome Welcome Welcome Welcome Welcome
The output is terminated at x == 5 so exited.
If you have any query’s in this tutorial on Python While Loop do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..