Iteration in Python
Looping Statement:
Looping means repetition, its execute some statements of a program of multiple times.
Using looping statement we can do complex program to easy program. We repeat flow of statement using loop instead writing the code again again.
For example : if you want to display your name 10 times, so it is easy to use looping statement instead to use the print statement of 10 times.
There are 3 types of looping statement:
For Loop:
Syntax:
for variable in sequence:
statements
Example 1:
Program to display the natural numbers till n terms.
n=int(input("Enter limit"))
for i in range(n):
print(i)
Output:
Enter limit5
0
1
2
3
4
On the above example variable 'i' is used as iteration variable. Here range() is a in-built function used to generate sequence of numbers in a range, it also produce next value start from 0 (by default) with increment by 1 (by default) and ends with specified value. So here, range function generate the numbers and assign into variable i.
If you display the numbers from 1 to 10, then the source code will be changed, given below:
See the figure 1.1:-
Example 2:
Program to display the odd numbers upto n terms.
n=int(input("Enter limit:"))
for x in range(1,n,2):
print(x)
Output:
Enter limit:10
1
3
5
7
9
On the above example: variable 'x' used as iteration variable and range() function takes 3 parameter (start,stop,step value). Here I have print the odd numbers from 1 to n terms, but range() function produce value start from 0, so the last value will be always n-1.
See the figure 1.2:
While Loop:
Syntax:
initialization;
while (condition):
body of loop
update value
Example:
Program to display the natural numbers upto n terms and dispaly the sum of those numbers.
s=0
n=int(input("Enter limit:"))
x=1
while(x<=n):
print(x)
s=s+x
x=x+1
print("Sum is:",s)
Output:
Enter limit:5
1
2
3
4
5
Sum is: 15
On the above example variable 'x' is a counter variable which is initialized by 1 and variable 's' which is used to store the sum of the numbers.
The control test the condition first, then execute the loop body, then increase value by '1'. The condition will be true as long as the counter variable (x) is less than or equal to 'n' (Look at the output, the value of 'n' is 5).
Look at the figure. 2.1. where n=5, s=0 (initial value) and x=1 (starting value).
Emulate do-while loop:
Syntax:
initialization
while True:
body of loop
update value
if (condition):
break
Example:
Program to display the numbers: 1, 4, 7, 10....... n terms .
n=int(input("Enter limit:"))
i=1
while True:
print(i)
i=i+3
if(i>n):
break
Output:
Enter limit:10
1
4
7
10
On the above example variable 'i' is used for counter variable, increase the value by 3, and the condition will be true as long as the counter variable 'i' is above 'n' (where value of n is: 10) .
Look at the figure 3.1. where n=10, x=1(starting value) .
Looping means repetition, its execute some statements of a program of multiple times.
Using looping statement we can do complex program to easy program. We repeat flow of statement using loop instead writing the code again again.
For example : if you want to display your name 10 times, so it is easy to use looping statement instead to use the print statement of 10 times.
There are 3 types of looping statement:
Loop Statement | Description |
---|---|
For Loop | The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. |
While Loop | The while loop is to be used where we don't know the number of iterations in advance. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop. |
do-While Loop | The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs). Though Python doesn't have it explicitly, we can surely emulate it. |
For Loop:
Syntax:
for variable in sequence:
statements
Example 1:
Program to display the natural numbers till n terms.
n=int(input("Enter limit"))
for i in range(n):
print(i)
Output:
Enter limit5
0
1
2
3
4
On the above example variable 'i' is used as iteration variable. Here range() is a in-built function used to generate sequence of numbers in a range, it also produce next value start from 0 (by default) with increment by 1 (by default) and ends with specified value. So here, range function generate the numbers and assign into variable i.
If you display the numbers from 1 to 10, then the source code will be changed, given below:
for i in range(1,10+1):
Here the range () function produce number with starting value '0', so that the last value always will be n-1, but here rage () function takes 2 parameter (start, stop) and increased by 1 by default. Here starting value is 1 and stop value is (10+1),
See the figure 1.1:-
Figure 1.1 |
Example 2:
Program to display the odd numbers upto n terms.
n=int(input("Enter limit:"))
for x in range(1,n,2):
print(x)
Output:
Enter limit:10
1
3
5
7
9
On the above example: variable 'x' used as iteration variable and range() function takes 3 parameter (start,stop,step value). Here I have print the odd numbers from 1 to n terms, but range() function produce value start from 0, so the last value will be always n-1.
See the figure 1.2:
Figure 1.2 |
While Loop:
Syntax:
initialization;
while (condition):
body of loop
update value
Example:
Program to display the natural numbers upto n terms and dispaly the sum of those numbers.
s=0
n=int(input("Enter limit:"))
x=1
while(x<=n):
print(x)
s=s+x
x=x+1
print("Sum is:",s)
Output:
Enter limit:5
1
2
3
4
5
Sum is: 15
On the above example variable 'x' is a counter variable which is initialized by 1 and variable 's' which is used to store the sum of the numbers.
The control test the condition first, then execute the loop body, then increase value by '1'. The condition will be true as long as the counter variable (x) is less than or equal to 'n' (Look at the output, the value of 'n' is 5).
Look at the figure. 2.1. where n=5, s=0 (initial value) and x=1 (starting value).
Figure 2.1 |
Syntax:
initialization
while True:
body of loop
update value
if (condition):
break
Example:
Program to display the numbers: 1, 4, 7, 10....... n terms .
n=int(input("Enter limit:"))
i=1
while True:
print(i)
i=i+3
if(i>n):
break
Output:
Enter limit:10
1
4
7
10
On the above example variable 'i' is used for counter variable, increase the value by 3, and the condition will be true as long as the counter variable 'i' is above 'n' (where value of n is: 10) .
Look at the figure 3.1. where n=10, x=1(starting value) .
Figure 3.1. |
Comments
Post a Comment
If you have any doubt, Please let me know..