Python while loop
Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively.
Sometimes, situations arise where you would need to use a piece of code again and again, but you don't want to write the same line of code multiple times so for such conditions we use Loop.
Looping statements are used to execute multiple codes or statement repeatedly until the condition specified in loops is evaluated false.
Python provides mainly two loop statements-
- while loop
- for loop
In this topic, we will explain the while loop.
The while loop is used to execute a statement repeatedly until the condition in the loop becomes false. It is used in that situation where we do not know the number of iteration in advance.
while expression: statement
In the above flow diagram of while loop, firstly the given condition is checked, if the condition evaluates false, the loop will be terminated and the control will transfer to the next statement in the program after the loop.
But if the condition evaluates true, the set of statements inside the loop will be executed and then the control will transfer to the beginning of the loop for the next iteration.
Example of while loop:
Some examples of while loop are as follows:
Note: The loop contains an increment operation where we increase the value of the given variable. It is a crucial step as the while loop must have an increment or decrement operation. Otherwise, the loop will run indefinitely.
1. Write a program to print the table of a given number
num=int(input("Enter the number")) #define a variable for user input i=0 while i<10: #while loop will iterate as long as the value of i is less than 10 i = i + 1 # Increment operation in i c=num*i # Multiply resultant stored in another variable c print(num,'*',i,'=',c)
Enter the number 12 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 12 * 9 = 108 12 * 10 = 120
2. Write a program to print hello world multiple times.
num =1 #there is a variable num in which we store an integer 1. while num<=5: # we print the hello world fives times until condition evaluates false print("Hello world") num=num+1 #we print out "Hello world" and increase the value of number with one.
Hello world Hello world Hello world Hello world Hello world
3. Write a program to count the number of even or odd numbers from the given series.
number = 1 count_odd=0 count_even=0 while number<10: number=number+1 if number%2==0: count_even=count_even+1 else: count_odd=count_odd+1 print('The count of even number:',count_even) print('The count of odd number:',count_odd)
The count of even number: 5 The count of odd number: 4
Infinite while loop
If the given condition in while loop never becomes false, then it will become an infinite while loop and will never terminate by itself. In a while loop, any non-zero value always specifies a true condition. For example:
num=0 while num<5: print("hello")
Above code will print 'hello' indefinitely because inside the loop we are not updating the value of num, so the value of num will always remain 0 and the condition num < 5 will always return true.
while True: print('Hello')
While loop with else statement
Python provides to use of else statement with the while loop. If the given condition becomes false, the else statement will execute. The else statement is an option to use with while loop. For example:
num = 1 while num <6: print(num) num = num+1 else: print("Loop is finished")
1 2 3 4 5 Loop is finished