While Loop in Python with Examples

In this article, We are going to cover while loop in python with examples, Break Statement in python while loop, Continue Statement in Python While Loop, Pass Statement in Python While Loop,while-else Loop in Python.

What is While Loop in Python ?

While loop in python repeatedly executes a target statement until a given condition is true. If a condition is true then the body of loop is executed. After body executed then again go back at the beginning, and the condition is checked if it is true then executed until the condition become false. Once the condition is false then goes out of the loop. In while loop, if condition is not true then the body of loop will not executed, not even once

Syntax:

while (condition):
	body of while

Example of while loop in python

n = 10
add = 0
i = 1
while i <= n:
    add = add + i
    i = i+1   
print("The addition is", add)

Output:

The addition is 55
While Loop in Python with Examples 1

Loop Control Statements in Python

Python supports following Loop control statements

  • Break Statement
  • Continue Statement
  • Pass Statement

Break Statement in python while loop

Break condition is used to terminate the loop when a certain condition is met. The break statement is used a while loop. The break statement in python terminates the current loop and resume execution at the next statement. Break statement can be used in both for and while loops. The break keyword is used to out a while loop.

Syntax:

break

Example of break statement in python while loop

s = 'FOSSTechNix'
i = 0
while True: 
    print(s[i]) 
    if s[i] == 'S': 
        break
    i += 1
  
print("end of while loop")

Output:

F
O
S
end of while loop
While Loop in Python with Examples 2

Continue Statement in Python While Loop

In continue Statement we can stop the iteration and continue. Continue statement is opposite to break statement. This statement is used to skip the rest of code inside a loop and execute the next iteration of the loop. This loop does not terminate and continue with next iteration.

Syntax :

Continue

 Example of continue statement in Python While Loop:

i = 0
s = 'FOSSTechNix'
  
while i < len(s):  
    if s[i] == 'T' or s[i] == 'S':  
        i += 1
        continue
    print(s[i]) 
    i += 1

Output:

F
O
e
c
h
N
I
X
While Loop in Python with Examples 3

Pass Statement in Python While Loop

The pass statement is a null statement. Pass statement is generally used as a placeholder. Pass is used to when we don’t want execute any code, so simply places pass at  that line. Difference between pass and comment comment is ignored by interpreter and pass is not ignored.

Syntax:

Pass

Example of pass statement in python while loop:

s = 'python'
i = 0
while i < len(s): 
    i += 1
    pass
print('Length of i :', i)     

Output:

Length of i : 6
While Loop in Python with Examples 4

while-else Loop in Python

In this while-else loop there is keyword break can be used to stop a for loop. Some cases else part is ignored. else block after while is executed when the loop is not end with break statement.

Example of while-else loop in python:

s = 0 
while s < 5: 
    s += 1
    print(s)  
else: 
    print("No Break")  

Output:

1
2
3
4
5
No Break
While Loop in Python with Examples 5

Another Example of while-else loop in python:

i = 0 
while i < 4:  
    i += 1
    print(i)  
    break
else:
    print("No Break")

Output:

1
While Loop in Python with Examples 6

Conclusion:

We have covered, while loop in python with examples, Break Statement in python while loop, Continue Statement in Python While Loop, Pass Statement in Python While Loop,while-else Loop in Python.

Related Articles:

Loops in Python 3 with Examples

7 Python Operators with Examples

Python Introduction for Programmers [Part 1]

6 Python Conditional Statements with Examples

Functions in Python with Examples

classes and objects in python with examples

Reference:

Python official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

3 thoughts on “While Loop in Python with Examples”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link