While Loop in Control Structure of Python
In some previous sections, we have seen how a code block can be run if the condition of an if statement is true. If the condition is met, the code block is executed only once. The while loop works pretty much the same way. For example – it also needs a condition, and if it is true, its codes run. But the important thing is that it can be run not just once but many times (this is called iteration).
That is, as long as the condition of that while loop is true, the code inside it continues to run. And when the condition is false, it goes out of the while loop and starts running the next statement of the program.
P = 0 while(P < 10): print(P) P = P + 1 print("The while loop is successfully executed!")
0 1 2 3 4 5 6 7 8 9 The while loop is successfully executed!
In the above program, a variable P is taken first and its value is set to 0. A while loop was then initiated. I have already said that it also checks whether the condition like if is true. So, what remains? while P < 10: Here we see that the condition is true. This means that the code under it will work. Then we don’t see what the code is.
First, there is the work of a print that will print the current value of P i.e. 0. After that, there is another statement that also falls under that while. That means it will also be executed. The function of that statement is to increase the value of P by one. Thus the while loop is done once. But it doesn’t end with a once-in-a-lifetime experience. Instead, it goes back to check the condition at the beginning of its activity i.e. while p < 10: in this line.
That too will be executed. The function of that statement is to increase the value of i by one. Thus the while loop is done once. But it doesn’t end with a once-in-a-lifetime experience. Instead, it goes back to check the condition at the beginning of its activity, i.e., while P <10: in this line.
When you come here and check, the value of P is 1, which has always been true, i.e., 1 but less than 10. So he goes back to working in the loop. Again print the value of P, increase its value by one, and go back to the beginning of the loop. Thus, once the value of P is 10, the program checks that P is not less than 10 before returning to the beginning of the loop. Then, without running the code in the loop, he left the loop at once and started running the other statements.
A statement print (“The while loop is successfully executed!”) is located outside the loop. which is run once, but before that, the while loop was run multiple times according to its condition.
One thing that comes to mind is – if a while loop runs as long as a condition is true, then what if we do something; I will set a condition for him that will never lie: P then the work of a while loop is not supposed to be the end, right?
Yes, such a lifelong loop can be called an infinite loop.
while 38 == 38: print("I'm In the infinite loop")
Here we set the condition for the while loop such that – as long as 38 is equal to 38 it will run the code within it. And we all know that 38 and 38 are equal throughout life. And so this loop is logically an infinite loop.
I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite loop I'm In the infinite^CTraceback (most recent call last): File "/home/tuhin/Desktop/Problem-Solving/Python/Contest.py",line 156, in <module> print("I'm In the infinite loop") KeyboardInterrupt
If someone tries to run by writing the above program, the text ” I’m In the loop ” will keep appearing on the standard output screen. In this case, the program can be stopped by pressing ctrl + C on the keyboard.
Break Statement in Control Structure
But if you do not like to close such a pair, it is better to know that – programmatically while loop work can be stopped at any time regardless of the condition. For this, you just have to write break.
P = 0 while(P < 10): print(P) P = P + 1 if P == 5: break
0 1 2 3 4
Continue Statement is Control Structure
This is another interesting thing. Suppose we write a statement to do several things in a loop, and we want the things in it to be repeated as long as the loop is true (not 100 times). But, it may be that we don’t want to do the whole thing a few times out of those 100 times, but we want to let the loop do the work 100 times; What can I do then?
This is why continue. Whenever continue is executed in a while loop, the next code in the loop will not be executed and the control of the loop will go to the very beginning.
The following example illustrates the point,
P = 0 while True: P = P +1 if P == 2: print("Skipping 2") continue if P == 5: print("Stop") break print(P) print("Finished")
1 Skipping 2 3 4 Stop Finished
When the value of P is 2, the loop has been added to the beginning through continue execution, so the 2 of that bar has not been printed. Then again 3,4 has been printed in general. When I got 5 again, the break was executed due to the authenticity of if and the loop ended prematurely (it was planned to last forever).