Hi Legends!!π
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
Output:
Explanation:
First of all I have made the variable
z
and set this at1
.Then we had used this
while
function!! And Code:while z < 8
python will executewhile 1 < 8
And also we are also puttingcolon(:)
after thewhile
statement because with the colon (:) it helps to indent andindentation
is must in python If you would not put the (:) then it does not work!!Then
print
statement is there!!Then I am incrementing z Like it will execute the code as long as it is less than
8
and it stops!!
Note: Remember to increment z, or else the loop will continue forever.
The 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
Output:
Explanation:
So here simply we had set the value of
x
as10
.Then we had used the
while
statement!! Code:while x > 5:
Python will executewhile 10 > 5:
.After that there is
print()
statement which is printingx
.Now there is an
if
statement and Code is:if (x == 8)
means if it comes to8
then there is anbreak
statement which helps to stop the code!!Then there is
iteration
and Code is:x = x - 1
which helps to iterate!
I want you to write, understand and run this codeπ
4. The Continue Statement π
With the 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)
Output:
Explanation:
So, here first of all I have made a variable
i
and set it as0
.Then we have used the while function and written
while i < 5:
Python will executewhile 0 < 5:
.Then we have written
i += 1
meansi = i + 1
.If statement comes:
if (i == 2):
Python will executeif (0 == 2):
then it will continue means what that it will stope the current iteration which is2
. After that we have doneprint(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 πͺ
With the 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")
Output:
Explanation: It is so simple for you!!π
- So, it is like if the condition is once false so can use the
else
statement 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π