Let’s remind ourselves of control flow statements. In if else control flow, when the condition gets satisfied then a certain block of code is executed otherwise skipped. It means the code is executed only once. In this tutorial we are going to learn about executing the block of codes a finite number of times till the condition is satisfied. This is generally termed as a loop. In Python, we have three types of loops i.e for, while and do-while. In this tutorial, we will learn about while loop in python.
In the last tutorial, we looked for loop in Python, where the number of iterations were known already. But what if we don’t know the number of iterations the codes need to be executed ? In this case we use a while loop. The while loop in Python is used to iterate blocks of code till test expression (condition) is true. Otherwise, it skips the block.
syntax
---------------
while condition:
#body_of_while
In the while loop, first of all the condition given is checked. If the condition is satisfied then control flow executes the given block of code and iterates the code execution. If the testing condition is not satisfied, it skips the while body statements and jumps outside the while loop block.
EXAMPLE 01: PRINT YOUR NAME 5 TIMES
>>> name = str(input("enter your name : "))
>>> c= 5
>>> while (c>0):
print(name)
c=c-1
Output:
enter your name : Diwas
Diwas
Diwas
Diwas
Diwas
Diwas
In the above example variable value is set as 5. We have defined the condition that the while block is iterated until the value is greater than 0. Initially c is 5 which is greater than 0 hence the while block gets executed. In the first iteration, the value of c is decreased by 1 and hence becomes 4. In the fifth iteration the value becomes 0. While testing the condition, 0>0 which returns false so the condition is not satisfied and hence iteration stops.
remember to update value of counter, else the loop will continue forever
EXAMPLE 02 : PRINT SUM OF NUMBER FROM 1 TO 10
>>> n = 10
>>> sum = 0
>>> i = 1
>>> while i <= n:
sum = sum + i
i = i+1 # update counter
>>> print("The sum is", sum)
Output:
The sum is 55
EXAMPLE 03 : PRINT FROM 5 TO 1
>>> i = 5
>>> while i >= 1:
print(i)
i = i -1
>>> print("Done")
Output:
5
4
3
2
1
Done
EXAMPLE 04 : DRAW A PATTERN
>>> i = 1
>>> while i <= 5:
print( '*' * i)
i = i + 1
Output:
*
**
***
****
*****
Nested While Loop
Nest is something like we have 7 days a week and each day has 24 hours. Number of days is the outer loop and the number of hours is the inner loop. For more details, If you start with Sunday and Sunday has 24 hours and it will start with 0th hour, 1 hour and so on to complete the whole day. And when you complete the entire 24 hour it will start new day i.e. Monday and for next day Monday should complete its entire hour which is similar to nested while loop means it should complete the inner loop first and then outer loop.
Simply, When a while loop is present inside another while loop then it is called nested while loop. Lets learn from examples:
>>> a = 2
>>> while (a>0):
print("first loop")
b=2
while(b>0):
print ("second loop")
b=b-1
a=a-1
Output:
first loop
second loop
second loop
first loop
second loop
second loop
We are going to show you how to build a guessing game using a while loop where you have only three chances to guess the number. After you guess the number, it will display message as “You Won” otherwise “Lost “
secrete_number = 9
guess_start = 0
max_guess = 3
while guess_start < max_guess:
guess = int(input('Enter number:'))
guess_start = guess_start + 1
if guess == secrete_number:
print('You won')
break
print('Sorry')
Output:
Enter number: 3
Sorry
Enter number: 9
You won
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
I seriously love your website.. Very nice colors & theme.
Did you build this website yourself? Please reply back as I’m wanting to create my very
own blog and want to know where you got this from or just what the theme is named.
Thank you!
Hi, I do think this is a great blog. I stumbledupon it 😉 I’m going to come back
yet again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.