python for loop syntax – Looping Statement

Looping Statement

Also known as “Iteration statement” or “Repetition statement”, looping statements are the commands which repeat the whole program or a part of it depending upon a given condition. Python provides two types of looping statements: for() loop and while() loop.

1) while() loop: It can be called as conditional loop as it repeats until a certain condition is True.

2) for() loop: It can be called as counting loop as it repeats up to a fixed number of time.

1) while() loop

This looping statement is a conditional loop which repeats a program and any part of it until a given condition is True. The general format of the command is~

Syntax

intialization
while <condition>:
         body of loop
updation

Description: computer first evaluates the given condition. When the condition is found True then it executes the statements written as body of loop. After execution the condition will be tested again and if it is still True the body of the loop is executed again. This execution of body of loop is repeated until the condition remains True. When the condition is evaluated to False the repetition is terminated and control of the program goes to the statement written after body of loop. Body of loop could be a single statement or a group of statements written with a little indentation.




1) WAP to print 1 to 10 number
i=1
while i<=10:
print(i)
i=i+1

2) WAP to print table of any number
num=int(input(“Enter any number”))
i=1
while i<=10:
table=num*i
print(num,”*”,i,”=”,table)
i=i+1

2) for() loop

“for” loop is a counting loop and repeats its body for a pre-defined number of times. It is a ver versatile statement and has many forms. The most basic form of for() loop is:

Syntax-1

for <control variable> in range(<number>):
body of loop

First of all, the <control variable> will be assigned value ‘0’ (zero) and the statements in the body of the loop will be executed once. Next the value of the <control variable> will be increased by 1 Now it will be compared with the <number> given in the “range”, if the value is still less than t given number, the body of the loop is executed again. The loop is terminated when value control variable becomes equals to the <number> given in “range”.

Syntax-2

for <control variable> in range(<number 1>,<number 2>):
body of loop

Syntax-3

for <control variable> in range(<number 1>,<number 2>,<number 3>):
body of loop

Syntax-4

for <control variable> in <any sequence>:
                           body of loop

<any sequence>-list,tuple and string etc.

3) WAP to count number of major and minor in the given age.
age=[43,56,34,42,16,17,54,10,33,36]
mj=mn=0
for i in age:
             if i>=18:
                         mj=mj+1
             else:
                        mn=mn+1
print(“Number of Majors=”,mj)
print(“Number of Minors=”,mn)

Jumping Statement

There are the statement that change the process of repetition of looping statement. Python offer two types of Jumping Statement
1)’break’ statement
2)’continue’ statement

1) ‘break’ statement

This Statement terminate the loop repetition and trasfer control of the program statement following body of the loop.

Syntax-
——-
while <condition>:
           statement_1
         if <condition>:
                        break
              statement_2
statement_3

for <control variable> in range(<N>):
                 statement_1
                  if <condition>:
                                         break
                         statement_2
statement_3

4) WAP to input two number one as numerator and second denominator. Calculate and print result of division of numbers.Program should repeat this process for 5 pair of number. Program should terminate its repetition when user input 0 as denominator

i=1
while i<=5: 
          n=int(input(“Enter Numerator”))
          d=int(input(“Enter Denominator”))
          if d==0:
                    print(“Division by zero.. Loop Terminated!!”)
                    break
r=n/d
print(“Result=”,r)
i=i+1
print(“Program Over!!!”)

2) ‘continue’ statement

while <condition>:
          statement_1
        if <condition>:
                    continue
          statement_2
statement_3

for <control variable> in range(<N>):
                   statement_1
                 if <condition>:
                          continue
                  statement_2
statement_3


5) WAP to input 10 integer. Calculate and print the average of only positive integer among them. Negative number should not be processes and 0 should treated as a end of input.

sum=0
c=0
for i in range(10):
               n=int(input(“Enter the number”))
               if n==0:
                       break
               if n<0:
                       continue
             sum=sum+n
           c=c+1
avg=sum/c
print(“Average of Positive Integer=”,avg)

Loop else statement

The “else” clause of a Python loop executes on normal termination of loop which means when test condition, in while loop, results into false or for loop has completed its all pre-defined petitions. The else clause is not executed when the break statement terminates the loop. The general format of loop else is

while <condition>:
statement 1
statement 2
…..
….
else:
statement N




WAP to input radius of a circle. Calculate and print its area. Repeat this process for 3 different circles. Radius entered as 0 shoud terminate the process. Check and print a message when program terminated abnormoally.
i=1
while i<=3:
r=float(input(“Enter the radius”))
if r==0:
print(“Loop terminated becoz r=0 entered by user”)
break
area=3.14*r*r
print(“Area of circle=”,area)
i=i+1
else:
print(“Loop Completed…”)

For More Deatils and Examples Watch our Video class on youtube by click on below link

Complete Python Loop Video

Also Check

Control statements in Python-Conditional Statement

 

Leave a Comment