In this tutorial, we will discuss one more loop which is while loop, as like for loop while loop also used to repeat a block of code. In this article, you will code and learn all the concepts of while loop.
Table of Content
- What is while loop
- The syntax of while loop
- While loop with else statement
Like for loop while loop is used to repeat a block of code or statements. Unlike for loop while does not need any sequence to iterate, rather than it works on Boolean values like True and False.
A while loop has two parts one is an expression which should be a Boolean value (True or False) or return a Boolean value and second is its body.
The while loop continues to execute its body or block of code until its expression turns False. In case if the expression does not turn false the body of while loop executes for infinite times.
The syntax of a While Loop.
while expression: statement() or While body
First the python interpreter checks the while loop expression if the expression state True the while body execute one time, after executing the complete body of while loop the interpreter again come to the top of while loop and again check its expression and if it finds the expression True its goanna executes the whole body again and again until the expression become False.
Python everting treat everything True Except 0, False and None.
Let’s understand while loop with an example:
number = 10 while number: print("number value inside while loop",":" ,number) number = number -1 print("the value of number outside the while loop: ", number)
number value inside while loop: 10 number value inside while loop: 9 number value inside while loop: 8 number value inside while loop: 7 number value inside while loop: 6 number value inside while loop: 5 number value inside while loop: 4 number value inside while loop : 3 number value inside while loop: 2 number value inside while loop: 1 the value of number outside the while loop: 0
in the above example, you can see that the moment the value of number turns 0, python interpreter treats it False and we get out of the while loop.
else statement with while loop
Like if and for loop statements you can also use else statement with the while loop. The else statement blocks of code or body will automatically execute when the expression of while loop become False.
The syntax of while loop with else:
while expresion: while loop body or statements() else: else body or statement()
Apparently, the execution of else statements depends on the Turning False of while expression.
Let’s understand it with an example:
true=True number = 10 while true: if number < 4: true = False print(number) number = number-1 else: print("the value of true becomes False and we are out of the while loop")
10 9 8 7 6 5 4 3
the value of true becomes False and we are out of the while loop
in the above example, we have used if statement inside the while loop. The moment, the value of number becomes less than 4 the if the statement of while loop executed and the value of expression true become False which terminate while and throw us out of while loop. After the expression become False the else statement which is corresponded to the while loop executes.
Points to remember:
- There should be a logic inside the while loop body which can make the expression False least the while loop executes infinite time and form an infinite loop.
- The while loop treats its expression as a Boolean value
- Like if and for statements you can use else statement with while loop
- Keyword & Identifiers
- Variables, Constraints, & Literals
- Python Comments, Statements, Indentation & Docstring
- Python Type Conversion & Type Casting
- Python Input Output Import
- Python Name, Namespace, & Scope