while loop python

While Loop

The loop allows the execution of a statement or a group of statements multiple times. There are certain conditions defined. This repeatedly tests the condition and if the condition is True then it executes Statement 1; if the condition is False then Statement 2 of the else clause is executed and the loop terminates. The else suite will be always executed irrespective of whether the statements are executed or not.

For example Syntax:

a=1

while a<=10:

print(a)

a+=1

print(“Rest of the code”)

Output:

1
2
3
4
5
6
7
8
9
10
Rest of the code

Firstly, take a variable and than assign ‘1’ value into it. And then after take while condition, if a value lesser and equal to 10 then execute the statement. After this, loop will exit and execute rest of the code.

While Loop with Else

While loop with else repeatedly tests or check the condition and if the condition is True then it execute the Statement 1, if the condition is False then Statement 2 got executed and then it terminate the loop.

This figure is showing the process of while loop with else. Firstly, it will check the condition and if the condition is true then it will execute the statement 1 and if the condition is False then it will execute else statement and then after it execute rest of the code.

Syntax: 

while(condition):

Statement 1

else:

Statement 2

Rest of the Code

Syntax:

a=1

while a<=5:

print(a)

a+=1

else:

print(“While Condition False”)

print(“Rest of the Code”)

Output:

1
2
3
4
5
While Condition False
Rest of the Code

Infinite While Loop

This will execute statement infinite time. It will never stop. It can crash the system or program file. This used in very rare case.

Syntax: 

while(True):

Statement

Rest of the Code

 

Syntax:

while(True):

Statement

if(condition):

break

Rest of the code

For example:

i=0

while True:

i+=1

print(i)

if(i==3):

break

print(“Rest of the Code”)

 

Output:

1

2

3

Firstly, take a variable name ‘i’ with 0 value and then after start while loop with ‘True’ condition and increment with 1.  and then take another condition, when the value of I will be equal to 3 then it enter to the break statement and exit the loop and print the rest of the code. When we will not add a break statement here then it execute statement infinite and system crash the system.

Nested While Loop:

Write While loop inside a while loop is known as nested while loop.

while(condition):

Statements

while(condition):

Statements

Statements

Rest of the Code

 

example such as:

i=1

while i<=3:

print(“Outer Loops”, i)

i+=1

j=1

while j<=5:

print(“Inner Loop”, j)

j+=1

print(“Rest of the Code”)

Output:

Outer Loops 1
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Outer Loops 2
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Outer Loops 3
Inner Loop 1
Inner Loop 2
Inner Loop 3
Inner Loop 4
Inner Loop 5
Rest of the Code

 

About Author

Leave a Reply

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

en_USEnglish
Open chat
Hello
Can we help you?