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!" ) |
5 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!" ) |
10 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.