Introduction
Welcome to my 18th blog post. Today I learned about the while loops and using else statements within while loops. As there are no Do while loops in python, I learnt to emulate do-while loops. Let's dive into detail.
So let's get started.....
While loops
While loops get executed when the expression in the while loop is true, i.e the condition inside the "while" loop is true. As soon as the condition becomes false, the interpreter moves out of the "while" loop.
Example -
count = 5
while (count > 0):
print(count)
count = count - 1
5
4
3
2
1
Note - The count variable decrements on each iteration. Depending on the condition we either increment or decrement the variable. If this is not done then the while loop keeps running forever turning out to be an infinite loop.
Else with While Loop
We can even use the else statement with the while loop. As soon as the condition in the while loop turns out to be false, then the interpreter control moves out of the while loop and the else statement is executed.
Example -
x = 5
while (x > 0):
print(x)
x = x - 1
else:
print('counter is 0')
5
4
3
2
1
counter is 0
Do-While loop in python
For the while loop to work, the condition inside the while should be TRUE. The do..while is a loop in which a set of instructions will be executed at least once (irrespective of the condition). The repetition of the loop's body will depend on the condition passed at the end of the while loop.
Do-while loop is also known as an exit-controlled loop.
How to emulate do while loop in python?
Since python does not have a do-while loop, you need to modify the while loop a bit to get similar behaviour to a do-while loop.
The most common technique to emulate a do-while loop in Python is to use an infinite while loop with a break statement that is wrapped in an if statement that checks a given condition and breaks the iteration if that condition becomes true:
while True:
number = int(input("Enter a positive number: "))
print(number)
if not number > 0: #As we enter a negative number the condition is true as the control moves out
break
Enter a positive number: 1
1
Enter a positive number: 4
4
Enter a positive number: -1
-1
Explanation
Do while loop uses True as its base condition. This trick turns the loop into an infinite loop. Before the conditional statement, the loop runs all the required processing and updates the breaking condition. If this condition evaluates to true, then the break statement breaks out of the loop, and the program execution continues its normal path.
Resources Used
You can watch the video of Day#18 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. We learnt about while loops and also more about using else statements with while loops. In python we don't have do-while loops, but still, a similar behaviour can be made in python as that of do-while loops in python
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding This is it for Day #18
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.