While Loops in Python!
4 min read
In this article we're gonna learn about While loops in Python!
Let's start: 😉
1. What are while loops? 👑
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don't know the number of times to iterate beforehand.
2. Code on while loops!! ✨
Print z as long as it is less than 8
z = 1 while z < 8: print(z) z = z + 1
First of all I have made the variable
zand set this at
Then we had used this
whilefunction!! And Code:
while z < 8python will execute
while 1 < 8And also we are also putting
whilestatement because with the colon (:) it helps to indent and
indentationis must in python If you would not put the (:) then it does not work!!
Then I am incrementing z Like it will execute the code as long as it is less than
8and it stops!!
Note: Remember to increment z, or else the loop will continue forever.
while loop requires relevant variables to be ready, in this example we need to define an indexing variable,
z which we set to 1.
2.1 Shortcut of
(z = z + 1) 🎈
So, If I do like that: 👇
z = 1 while z < 8: print(z) z += 1
If it is
z += 1 then also it would work and it is the easy way to do that!!😉
Hopefully you are done with it, let's move to the
break statement now!!
3. The break statement 🤚
With the break statement we can stop the loop even if the while condition is true:
# The Break statement x = 10 while x > 5: print(x) if (x == 8): break x -= 1
So here simply we had set the value of
Then we had used the
while x > 5:Python will execute
while 10 > 5:.
After that there is
print()statement which is printing
Now there is an
ifstatement and Code is:
if (x == 8)means if it comes to
8then there is an
breakstatement which helps to stop the code!!
Then there is
iterationand Code is:
x = x - 1which helps to iterate!
I want you to write, understand and run this code💜
4. The Continue Statement 🏏
continue statement we can stop the current iteration, and continue with the next:
Example for continue statement...
Continue to the next iteration if i is 2.
# The continue statement i = 0 while i < 5: i += 1 if (i == 2): continue print(i)
So, here first of all I have made a variable
iand set it as
Then we have used the while function and written
while i < 5:Python will execute
while 0 < 5:.
Then we have written
i += 1means
i = i + 1.
If statement comes:
if (i == 2):Python will execute
if (0 == 2):then it will continue means what that it will stope the current iteration which is
2. After that we have done
print(i). Look at the Output above !!
So legends, 😎 I am sure that you all are totally clear by the concepts!! Let's dive in to the next one:
5. The else statement 🪀
else statement we can run a block of code once when the condition no longer is true:
Example of else statement...
Print a message once the condition is false:
# The else statement a = 1 while a < 11: print(a) a += 1 else: print("a is no longer less than 10")
Explanation: It is so simple for you!!😎
- So, it is like if the condition is once false so can use the
elsestatement and print a message to make the user understand the code!
Super guys🥳🎉, we have cracked the while loops!!
Mini question 1 🎃
I want you to solve this!!💜
Mini question 2 🎃
Can you solve it??
Mini question 3 🎃
If you solve this one you are pro😎
Mini question 4 🎃
Let me see if you can solve this! 😉
So, that is it for today guys!!😊 If you wanna check your answers run in the compiler!!
And if you have any doubt any query please mention it in the comments below💙
And for now Goodbye👋