Python File handling

Python while Loop

Looping statement is used to execute a group of statement multiple times based on some condition.Python supports two loops.

Python While Loop

Python while loop is used when we want to execute some set of statement until some condition is True.When we don’t know how much time we want to repeat a statement at that time while loop is required.

Syntax

while condition:
    while-loop-body
#end of while loop

In the syntax of while loop, while is a keyword and condition is used decide how much time body of the loop is executed. Inside the body you write statement that you want to execute.

If condition Is True then execute a body of the loop and after executing the statement of body again check for the condition until the condition is True.

If condition return False then terminates a loop and go to the first statement after the while loop.You can also allow to terminate loop explicitly using break.

Python while loop

In the above image shown the flow of while loop using flowchart.

Example

In this example try to print number from 1 to 5.

i=1;
while i<=5:
    print(i);
    i=i+1;

Output

1
2
3
4
5

In this output print(i) statement execute until the value of i<=5. When the value of i become a 6 at that time loop is terminated.

In this example, try to print list elements

lst=[10,17,22]
i=0
while i

output

10
17
22

In this output, print value of list until value of i<len(lst)

Python Nested while Loop

Nested while loop means while loop inside the another while loop body.

Syntax

while condition:
    outer-loop-body
    while condition:
        inner-loop-body
    outer-loop-body
#end of nested while

In syntax we observe that inside outer while loop body, another while loop is available

Example

n=int(input("Enter Number : "))
i=1
while i<=n:
    j=1
    while j<=i:
        print('*',end=" ")
        j=j+1
    print()
    i=i+1

Output

Enter Number : 3
* 
* * 
* * * 

Python while loop with else

The else block is executed when the loop condition became False. If while loop is terminated using break, then else block is not executed. Otherwise in all the case else block is executed.

The else block is executed only once, then the loop gets terminated.

This is suitable when we try to read data from the result set of data and after reading of data that want to delete that object.

Syntax

while condition:
    while-block-statement
else:
    else-block-statement

For while loop only one else block is allowed. More else block not allow to write with single while loop

Example

lst=[10,17,22]
i=0
while i

Output

10
17
22
Next element not available

In this example, while loop body execute until i<len(lst).When value of i is greater then the length of the list at that time condition became false and enter into else block and execute else block statement.After executing else block statements, loop gets terminated.

Infinite While loop

while True:
    print("Mcaprogramming")

This loop print Mcaprogramming infinite time.

2 thoughts on “Python while Loop”

  1. Pingback: Python For Loop | MCAProgramming

  2. Pingback: Python Break Continue And Pass | MCAProgramming

Leave a Comment

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