Shrinking a while loop in Python

Shrinking a while loop in Python

Shrinking a while loop in Python


 A loop is an iterative control structure capable of directing program flow based on the validity of a condition. Such structures are necessary for automating tasks. There are 2 types of loops that represent the Python programming language, namely:


for loop

while loop

This article will show you how to reduce the while loop in Python.


while loop:

The while loop is an iterative structure that continues to execute until the condition of the while loop is falsified. The condition must be met (in most cases); otherwise, the loop may become infinite. The while loop syntax is as follows:


while test_expression :

   Body of while

Where test_expression is usually a relation whose validity determines whether the loop will run or not. The condition is traditionally satisfied by continuously incrementing the variable. i.e., there must be a variable in the while loop (unless there is a break statement) whose value change will falsify the condition at one point. Unlike for loops, where an update statement can be defined in the loop header, while loops must contain at least one statement for this purpose.

Shrinking a while loop in Python

As stated earlier, inside the while loop, there must be a condition for it to end. In the following example, this will be demonstrated by a variable whose value is constantly decremented within a loop per iteration until the loop condition is falsified:


# 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 then 0

while n > :

 

    # Displaying the value of n for that iteration

    print(n)

 

    # Decrementing the value by 1

    n = n - 1

 

print("Loop Ends!")

Exit:


five

4

3

2

one

Loop Ends!

Explanation:

First, a variable with a value of 5 is initialized. Then a loop is defined that will run as long as the value of the above variable is greater than 0. Then, inside the loop body, the value of the variable is displayed, and the value of the variable is decremented by 1. Therefore, at each iteration, the value of the variable n will approach closer to 0. After the loop ends, the loop ends! a message is displayed.


Note. Decreasing value cannot be by 1 for each iteration. Depending on the requirement, the value can be reduced by any value per iteration. Former. Below is the code that displays the first 10 even numbers:


# 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 then 0

while n > :  

   

      # Displaying the value of n for that iteration

    print(n) 

     

    # Decrementing the value by 1

    n = n - 2

     

print("Loop Ends!")

output:

10

eight

6

4

2

Loop Ends!

The code is the same as before, but this time a decrease of 2 per iteration is chosen instead of 1.

Please leave your comment to encourage us

Previous Post Next Post