Loop control statements are used to execute a block of statements repeatedly until a specific condition is satisfying.
Python supports two types of loop constructs:
- while
- for
While Loop
A while loop statement of Python executes a block of statements until a given condition is returning true.
#Example of while loop
x=1
while (x<=5):
print (“Hi”)
x=x+1
print (“Rest of the program”)
Three requirements of any kind of loop:
- Initialisation of Control Variable
- Loop Condition based on the Control Variable
- Modification of value of control variable inside the loop body so that it makes loop condition false at some iteration.
The Infinite Loop
A loop becomes infinite loop if its condition never returns FALSE. This type of loop repeatedly executes its corresponding block and never allows the rest of code in the program to execute. Developer should take care to avoid infinite loop using his logic.
But infinite loop is not a problem. Sometimes developer can use it to get desirable input from the user using this kind of loop.
#Example of infinite loop
n = 1
while (n<=5) : # Condition satisfies forever since no change in value of n
print(“Hi”)
print (“End”)
Using else Statement with While Loop
Python supports to have an else statement associated with a loop statement. If the else statement is used with a while loop, the else statement is executed when the condition becomes false or while loop completed all iterations. But the else part will be executed only once.
Example:
count = 0
while (count < 5):
print (count, ” is less than 5″)
count = count + 1
else:
print (count, ” is not less than 5″)
The for Loop
This loop is used to repeat a block of statements for certain number of times. In python, for loop can work with a range of values or list or a string.
Syntax1:
for var in range(StopVal):
statements
Example:
for i in range(6):
print(i)
in the above syntax when you specified the stop value as 6, python starts its range from 0 and ends at 5.
You can also specify the start value in range
Example:
for i in range(1,6):
print(i)
the output of above code will print numbers starting from 1 to 5 incremented by adding 1 by default.
You can also specify by which value the increment to be done in range
Example:
for i in range(1,20,2):
print(i)
The for loop can also work with sequence like list or string.
Syntax
for var in sequence:
statements
Example:
for x in ‘Python’ :
print (x)
fruits = [‘banana’, ‘apple’, ‘mango’]
for fruit in fruits:
print (fruit)
Working with index value of sequence
We can also use the index value of a sequence with for loop to iterate to the values.
Example:
fruits = [‘banana’, ‘apple’, ‘mango’]
for index in range(len(fruits)):
print (fruits[index])
Using else Statement with For Loop
Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has completed its iterations or loop condition is returned false.
Nested Loop
Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept
Syntax
for var in sequence:
for var in sequence:
statements
The syntax for a nested while loop statement in Python programming language is as follows
while condition:
while condition:
statement
You can put any type of loop inside of any other type of loop.