Python While Loop
while loop is a control flow statement that allow to execute the code until the given condition is false. Sometimes, we need to execute a statement more than one time. We can do the so by Python Loops. Unlike other programming languages, Python has only two types of loops:
- while loop
- for loop
In this tutorial, we are going to learn about the while loop. If you want to know about for loop, then click here.
Python While Loop
With the help of the Python While Loop, we can execute a specific statement until the given condition is false. Once the condition becomes false, then the flow of the program comes out of the loop.
Syntax of while loop
while condition:
statement
With the help of while
keyword, we define the while loop. Just after while keyword, we specify the condition on that basis loop will work. Then in the next line, with the indentation, we give the statement to be executed.
Example of printing a consecutive series from 1 to 5 by using while loop.
num = 1
while num<=5:
print(num)
num+=1 # num=num+1
1
2
3
4
5
Break Statement
With the help of the break
statement, we can terminate the loop. It will terminate the loop even if the condition is true. By the break
keyword we describe the break
statement.
Example of break statement
num = 1
while num <= 5:
if(num == 4):
break
print(num)
num += 1
print("WELCOME")
1
2
3
Note: The break statement only terminates the loop, it does not completely stop the flow of the program.
Continue Statement
The continue statement uses inside the block of loops. When the continue statement encounters control of loop jumps at the beginning of the loop for the next iteration. It will terminate only one turn of the loop even if the condition is true. By the continue keyword we describe the break statement. It doesn’t terminate the loop,
Example of continue statement
num = 0
while num < 5:
num += 1 # similiar to num=num+1
if num == 4:
continue
print(num)
print("WELCOME")
1
2
3
5
Note: The continue statement only skips one iteration of the loop, it does not completely stop the flow of the program.
Python While Loop – with else statement
We can also use else statement with the while loop just like if.
Syntax
while condition:
statement of loop
else:
statement of else
If the condition of a while loop is satisfied then the loop statement will execute. Otherwise, the else statement will execute.
Example of while loop with else statement
var = 0
while var <= 5:
print("loop's code")
var += 1
else:
print("else's code")
loop’s code
loop’s code
loop’s code
loop’s code
loop’s code
else’s code
Nested while loop
Nested while loop means a while loop inside another while loop. Like a nested if, we can also perform some actions by the nested while loop as well.
We generally use nested while loop, when we are going to execute a block of code on the basis of another block of code, number of times or with the other block of code.
Example of nested while loop
num_1 = 1
num_2 = 6
while num_1 <=5:
while num_2 <=10:
print(num_1, ",", num_2)
num_2 += 1
num_1 += 1
1 , 6
2 , 7
3 , 8
4 , 9
5 , 10
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |