Decrement in While Loop in Python
Last Updated :
13 Feb, 2023
A loop is an iterative control structure capable of directing the flow of the program based on the authenticity of a condition. Such structures are required for the automation of tasks. There are 2 types of loops presenting the Python programming language, which are:
This article will see how to decrement in while loop in Python.
While loop:
A while loop is an iterative structure that keeps on executing unless the condition of the while loop is falsified. The condition needs to be satisfied (under most cases); otherwise, the loop may become infinite. The syntax of a while loop is as follows:
while test_expression:
Body of while
Where the test_expression is generally a relation, the correctness of which determines whether the loop would execute or not. The condition is traditionally satisfied by a continual increment of a variable. i.e., there must exist a variable in a while loop (unless there exists a break statement wherein), the change in whose value would falsify the condition at one point. Unlike for loops where the update statement could be defined in the loop's header, the while loops need to contain at least one statement for that purpose.
Decrement in While Loop in Python
As asserted earlier, a condition must be present inside the while loop for its termination. In the upcoming example, this would be demonstrated by a variable whose value continually decrement inside the loop per iteration until the loop condition is falsified:
Python3
# Python code to display the first 5 natural numbers
# A variable that would be used in the loop control statement
n = 5
# The loop would execute until the value of n is greater than 0
while n > 0:
# Displaying the value of n for that iteration
print(n)
# Decrementing the value by 1
n = n - 1
print("Loop Ends!")
Output5
4
3
2
1
Loop Ends!
Time complexity: O(n)
Auxiliary space: O(1)
Explanation:
Firstly a variable is initialized with the value 5. Then a loop is defined, which would iterate as long as the value of the aforementioned variable is greater than 0. Then inside the loop body, the variable's value is displayed, and the variable's value is decremented by 1. Hence upon each iteration, the value of variable n would approach closer to 0. Upon which the loop terminates, the Loop Ends! message is displayed.
Note: The decrement of a value may not be by 1 for each iteration. Depending upon the requirement, the value could be reduced by any value per iteration. Ex. The following is the code that displays the first 10 even numbers:
Python3
# Python code to display the 5 even numbers
# A variable that would be used in the loop control statement
n = 10
# The loop would execute until the value of n is greater than 0
while n > 0:
# Displaying the value of n for that iteration
print(n)
# Decrementing the value by 2
n = n - 2
print("Loop Ends!")
Output10
8
6
4
2
Loop Ends!
Time complexity: O(n)
Auxiliary space: O(1)
The code is the same as before, but this time the decrement of 2 per iteration is chosen as opposed to 1.
Similar Reads
Python Do While Loops In Python, there is no construct defined for do while loop. Python loops only include for loop and while loop but we can modify the while loop to work as do while as in any other languages such as C++ and Java.In Python, we can simulate the behavior of a do-while loop using a while loop with a condi
6 min read
Python While Loop Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.In this example, the condition for while will be True as long as the counter variable (count) i
5 min read
Difference between for loop and while loop in Python In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if w
4 min read
Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). In this article, we will look at Python loops and understand their working with the help of examples. While Loop in PythonIn Python, a while loop is us
9 min read
Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). In this article, we will look at Python loops and understand their working with the help of examples. While Loop in PythonIn Python, a while loop is us
9 min read