Python supports basic loop structures through iterative statements. Actually, iterative statements are decision control statements that are used to repeat the execution of a group of statements.
Python supports the following iterative statements:
- while loop
- for loop
while loop:
while loop provides a mechanism to repeat one or more statements while a particular condition is true.
The general form of while loop is given below:
initialization
while(test condition):
body of loop
statement-y
The test condition is evaluated, and if the condition is true, then the body of the loop is executed. After the execution of the loop body, the test condition is once again evaluated, and it is true, the loop body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false, and the control is transferred to statement-y, that is, the immediate statement outside the while loop block.
Example 1:
i=0 while(i<=15): print(i,end=" ") i=i+2
Output:
0 2 4 6 8 10 12 14
Example 2:
number=int(input("Enter the value of number=")) s=number sum=0 while(s!=0): temp=s%10 sum=sum+temp*temp*temp s=s//10 if(sum==number): print("The number is an armstrong number") else: print("The number is not an armstrong number")
Output:
Enter the value of number=153
The number is an armstrong number
for loop:
The for loop also provides a mechanism to repeat a task until a particular condition is true. The for loop is usually known as a determinate or definite loop because we know exactly how many times the loop is repeated.
The for…in the statement is a looping statement used in Python to iterate over a sequence of objects, i.e., go through each item in a sequence.
The general form of for loop is given below:
for loop_control_var in sequence:
statement block
when a for loop is used, a range of sequence is specified (only once). The items of the sequence are assigned to the loop control variable one after the other. The for loop is executed for each item in the sequence. With every iteration of the loop, a check is made to identify whether the loop control variable has been assigned all the values in the range. If all the values have been assigned, the statement block of the loop is executed else, the statements comprising the statement block of the for loop are skipped, and the control is transferred to the immediate statement following the for loop body.
Example 1:
n=int(input("How many elements=")) sum=0 for i in range(n): sum=sum+i print("The Sum of n numbers=",sum)
Output:
How many elements=10
The Sum of n numbers= 45
Example 2:
n=int(input("How many elements=")) fact=1 for i in range(1,n+1): fact=fact*i Text="The factorial of {number} is={result}".format(number=n,result=fact) print(Text)
Output:
How many elements=5
The factorial of 5 is=120
Python also supports the following types of loops:
- entry-controlled or pre-test loop
- exit-controlled or post-test loop
- counter-controlled loop
- conditional-controlled loop
entry-controlled loop:
For the entry-controlled loop, the test-condition is tested before the loop starts. If the condition is not met, then the loop is never executed. For loop, while loop is the example of the entry-controlled loop.
Example A:
number=int(input("Enter the value of number=")) sum=0 for i in range(1,number+1): sum=sum+i print("The sum of first",number,"natural number is=",sum)
Output:
Enter the value of number=10
The sum of first 10 natural number is=55
Example B:
sum=0 n=int(input("Enter the value of n=")) for i in range(1,n+1): sum=sum+i if(i<n): print(i,"+",end=" ") else: print(i,end=" ") print("=",sum,end=" ")
Output:
Enter the value of n=10
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
exit-controlled loop:
For an exit-controlled loop, the test-condition is tested after the loop is executed. In this loop, the body of the loop is executed unconditionally for the first time.
counter-controlled loop:
For a counter-controlled loop, we know in advance the number of times the loop is executed. The counter is a variable that must be initialized, tested, and updated for performing the loop operations.
Example 1:
i=0 while(i<=20): print(i,end=" ") i+=2
Output:
0 2 4 6 8 10 12 14 16 18 20
Example 2:
for i in range(10): print(i,end=" ")
Output:
0 1 2 3 4 5 6 7 8 9
conditional-controlled loop:
When we do not know in advance the number of times the loop is executed, then we use a condition-controlled loop. This loop is often useful for indefinite repetition as we use a True/ False condition to control the number of times the loop is executed. Actually, the conditional-controlled loop can be implemented using for loop, but while loop offers a better option.
Example 1:
i=1 while(i>0): print(i,end=" ") i+=1 if(i==10): break
Output:
1 2 3 4 5 6 7 8 9
Leave a comment