Loops are utilized in programming to repeat a particular block of code. In this article, you will figure out how to create a while loop in Python.
What is while loop in Python programming?
The while loop in Python is utilized to repeat over a block of code as long as the test expression (condition) is valid.
We generally utilize this loop when we don't know the number of times to repeat beforehand.
Syntax:-
while test_expression:Body of while
In the while loop, test expression is actually taken a look from the outset.The body of the loop is placed provided that the test_expression assesses to True. After one iteration, the test expression is checked once more. This cycle go on until the test_expression assesses to False.
In Python, the body of the while loop is determined through space.
The body begins with space and the first unindented line denotes the end.
Python interprets any non-zero worth as True. None and 0 are interpreted as False.
Example: Python while Loop
# Program to add natural
# numbers up to
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1
# update counter
# print the sum
print("The sum is", sum)
Output:-
Enter n: 10The sum is 55
In the above program, the test expression will be True however long as our counter variable i is less than or equal to n (10 in our program).
We really want to expand the value of the counter variable in the body of the loop. This is very important (and generally forgotten). Neglecting to do so will result in an infinite loop (ceaseless loop).
At long last, the result is displayed.
Loop Control Statements in Python
Loop control statements change execution from its generally expected grouping. At the point when execution leaves a scope, all automatic objects that were made in that scope are destroyed. Python upholds the accompanying control statements.
Continue Statement
Python Continue Statement returns the control to the starting of the loop.
Example: Python while loop with continue statement
# Prints all letters except 'c' and 's'
i = 0
a = 'sinhacoch'
while i < len(a):
if a[i] == 'c' or a[i] == 's':
i += 1
continue
print('Current Letter :', a[i])
i += 1
Output:
Current Letter : iCurrent Letter : n
Current Letter : h
Current Letter : a
Current Letter : o
Current Letter : h
Break Statement
Python Break proclamation brings control out of the loop.
Example: Python while loop with break statement
# break the loop as soon it sees 'i'
# or 'c'
i = 0
a = 'sinhacoch'
while i < len(a):
if a[i] == 'i' or a[i] == 'c':
i += 1
break
print('Current Letter :', a[i])
i += 1
Output:
Current Letter : sPass Statement
The Python pass proclamation to compose empty loops. Pass is likewise utilized for empty control statements, functions, and classes.
Example: Python while loop with pass statement
# An empty loop
a = 'sinhacoch'
i = 0
while i < len(a):
i += 1
pass
print('Value of i :', i)
Output:
Value of i : 9While loop with else
Same as with for loops, while loops can likewise have a discretionary else block.
The else part is executed if the condition in the while loop assesses to False.
The while loop can be ended with a break statement. In such cases, the else part is disregarded. Consequently, some time while loop's else part runs assuming no break happens and the condition is false.
Example:-
'''
Example to illustrate the
use of else statement with
the while loop
'''
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
Output
Inside loopInside loop
Inside loop
Inside else
Here, we utilize a counter variable to print the string within loop three times.
On the fourth iteration, the condition in while becomes False. Consequently, the else part is executed.
Sentinel Controlled Statement
In this, we don’t utilize any counter variable since we don’t know that how often the loop will execute. Here user concludes that how often he needs to execute the loop. For this, we utilize a sentinel value. A sentinel value is a value that is utilized to end a loop whenever a user enters it, for the most part, the sentinel value is -1.
Example: Python while loop with user input
a = int(
input('Enter a number (-1 to quit): ')
)
while a != -1:
a = int(
input('Enter a number (-1 to quit):')
)
Output:
Enter a number (-1 to quit):2
Enter a number (-1 to quit):
6
Enter a number (-1 to quit):
-1
0 Comments
🙏🙏please don't enter any spam links on the comment box.....