Header Ads Widget

Responsive Advertisement

Iteration/looping statements : For loop and While loop


The iteration statements or repetition statements allows a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. Python provides tow kinds of loops L for loop and while loops to represent tow categories of loops.

Counting Loops: the loops that repeat a certain number of times ; python's for loop is a counting loops.

Conditional loops: the loops that repeat until a certain thing happens , they keep repeating as long as some condition is true, Python's while loop is conditional loop. 

According to your CBSE class 11 syllabus there is only for loop in your syllabus. We will discuss both loops. we will discuss "while loop" for your knowledge.


for loop: The for loop of python is designed to process the items fo any sequence, such as a lost or a string, one by one. The general form of for loop is a s given below as show in given figure 

for < variable > in < sequence > :

      statements_to_repeat


for a in range(1,10):

    print("Value of a",a,"\n")

 


Program code for slit each character 

for ch in "calm" :
    print(ch)
 

Output of the above code:  



Program :  Write code to print table of any given number by user

num=int(input("Enter a value you want to Create table of a Number"))

print("Table of ",num)

for a in range(1,11):

    print(num,"*",a,"=",a*num)



Program : write a program to print sum of natural number between 1 to 7: 

sum= 0

for a in range(1,8):

    sum+=a

print("Sum of ",a , "natural number is",sum )




Nested Loop: A loop may contain another loop in its body. This form a of a loop is called nested loop. But in a nested loop, the inner loop must terminate before the outer loop. The following is an example of a nested loop. Where a for loop is nested within another for loop. 
Example of nested loop: 


for a in range(1,6):
    for j in range(1,a):
        print("*",end=" ")

    print()
    





Program: 

for a in range(1,6):

    for j in range(1,a+1):

        print(a,end=" ")


    print()




Post a Comment

0 Comments