Loop statements loop in python

Loop statements loop in python

Loop statements in python

Loop in python are used to execute a statement or group of statements repeatedly either number of times a particular condition is satisfied. Python have two loop in python for and while

 

For loop statement in python

The for loop statement is used to iterate in a sequence. Following is the syntax for python for loop statement

             for variable in sequence :

                          statement(s):

Each iteration assigns the value in variable from the sequence one by one, and then executes the statements in the body. The loop finishes when the last element in the sequence is reached

 

Program for demonstration of for loop

for number in range(1, 6):

print(number, end =” “)

 

Output

1 2 3 4 5

 

Program to print n natural numbers

n = int(input(“Enter the last natural number: \n“)

print(“First”,n, “natural numbers are“)

for number in range(1, n+i):

               print(number, end” “)

 

Output

Enter the last natural number: 10

First 10 natural numbers are

1 2 3 4 5 6 7 8 9 10

 

While loop statement in python

The while loop statement is used to repeat a statement or group of statements till a particular condition satisfy Following Is the syntax of while loop statement:

while expression:

         statement(s)

 

Statements of while loop will executes till expression gives True i.e. till condition is satisfied

 

Program for demonstration of while loop in python

n =int(input(‘Enter a number“))

while n!=0:

          d=n%10

           print(d,end=””)

           n//=10

 

Output

Enter a number: 423

324

Break statement

The break statement is used to terminate the loop execution immediately. It brings the program flow to the statement just after the body of loop

 

 Program for demonstration of break statement

for i in range(1, 6):

              if(i==3):

                       break

             print(i)

 

Output

1

2

 Continue statement in python

The continue statement is used to skip the loop single statement or group of statements execution for certain iteration(s). Loop does not terminate, it continues with the next iteration.

Program for demonstration of continue statement

for i in range(1, 6):

                         continue

print(i)

 

Output

1

2

3

4

5

Else statement with loops

In python you can associate else statement with loop statements. The else statement can be used to find out loop is executed completely or not because the else statement with loop is executed only if loop is terminated after complete execution not due to break statement.

Program for demonstration of else statement with loop in python

for i in range(1, 4):

                  print()

else:

                 print(“Out of loop“)

 

Output

1

2

3

Out of loop

 

Program for demonstration of else and break statement with loop

for number in range(1, 6):

               if(number==3):

                           break

               print(number)

else:

               print(“Out of loop“)

 

Output

1

2

 

Program for using else and break statement with loop in python

n = int ( input ( ” Enter a number : ” ) )

for i in range(2, n):

               if n%i==0:

                          print(n, “is not a prime number”)

                         break 

else: 

              print(n, “is a prime number“)

Output 1

Enter a number: 5

5 is a prime number

Output 2

Enter a number: 9

9 is not a prime number

Nested loop in python

Putting loop inside a loop is called nested loop and it is used to repeat loop. The syntax for nested for loop is given below:

 for variable in sequence: 

              for variable in sequence: 

                            statement(s) 

            statement(s)

You can also nest while loop and syntax for nested while loop is given below

while expression: 

           while expression:

                      statement(s)

           while expression:

You can also nest mix loops. For example, a for loop can be inside a while loop or while loop can be inside a for loop.

 Program for demonstration of nested loop in python

for i in range(1, 11):

              for j in range(1, 11):

                       k=i*j

                     print(k,end=” “)

print()

Output 

1  2  3  4  5  6  7  8  9  10

2  46  8  10  12  14  16  18  20

3  69  12  15  18  21  24  27  30

4  8  12  16  20  24  28  32  36  40

5  10  15  20  25  30  35  40  45  50

6  12  18  24  30  36  42  48  54  60

7  14  21  28  35  42  49  56  63  70 

8  16  24  32  40  48  56  64  72  80 

9  18  27  36  45  54  63  72  81  90

10   20  30  40  50  60  70  80  90  100

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *