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
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)
The addition is 55
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.
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")
F O S end of while loop
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.
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
F O e c h N I X
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.
Example of pass statement in python while loop:
s = 'python' i = 0 while i < len(s): i += 1 pass print('Length of i :', i)
Length of i : 6
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")
1 2 3 4 5 No Break
Another Example of while-else loop in python:
i = 0 while i < 4: i += 1 print(i) break else: print("No Break")
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.