25 C
Jaipur
Thursday, December 3, 2020

Python while Loop – Linux Hint

Must read

Twitter Now Supports Hardware Security Keys on Android and iOS

2020 was certainly not a great year for Twitter’s security team, case in point: the infamous high-profile Twitter Bitcoin scam. To recover from this...

FAU-G Records Over 1 Million Pre-Registrations in Just 24 Hours on Android

While the ministry is giving PUBG officials a cold shoulder, the game’s Atmanirbhar rival, FAU-G, is gearing up for a mammoth launch later this...

Apple to Release Mini LED MacBook Pro in 2021, MacBook Air in 2022: Kuo

We have heard about Apple working towards using Mini LED displays in its devices, including the MacBook Pro and MacBook Air. While earlier reports...

Top 9 Best Open Source Forum Software to Self Host [2020]

Looking to have a community forum or customer support portal? Here are some of the best open source forum software you can deploy on...

Loops are used to run the same block of code repeatedly in programming languages. In Python, the while loop runs the same block of code until the test condition is true. If the test condition is false, the loop ends, and the immediate line after the while loop is executed.
Inside of the while loop, you can have a single statement or multiple statements, which are executed until the test condition is true. This article describes the Python while loop and provides some simple examples using this function.First, we will talk about the syntax of the while loop.

Syntax of while Loop

The syntax of the while loop in Pythion is as follows:

while test_condition:
  statement(s)

When the while loop is executed, the test_condition is evaluated first. When the test_condition is true, the loop body or statements are executed within the loop.

If the test_condition is true, then the body of the loop or statement(s) inside the loop are executed. Executing the body of the while loop only once is denoted as one iteration. The test condition is checked after every iteration, and the while loop runs until the test_condition is false.

Flow of the while Loop

The figure given below depicts the flow of the while loop.

Examples of while Loop

Now, let us look at some examples of the while loop and to understand how it works.

In this example, first, we declare a sum variable whose value is zero. The while loop is then started using the while keyword. The test_condition is sum<20. This means that the loop will run until the sum variable value is less than 20.

When the test condition is false and the sum value is 20, the loop will terminate and the program will print “End of while loop.”

# declaring a variable
sum = 0
#starting while loop
while (sum < 20):
    #printing the value of the sum
    print (“The value of sum is”, sum)
    #incrementing 1 in the sum in every iteration
    sum = sum + 1
print (“End of while loop”)

Output


You can also declare the else statement with the while loop. The else block is functionalized when the while test_condition is false. In this example, we will add the else statement to the sum example given above.

# declaring a variable
sum = 0
#starting while loop
while (sum < 20):
    #printing the value of the sum
    print (“The value of sum is”, sum)
    #incrementing 1 in the sum in every iteration
    sum = sum + 1
#writing the else statement
else:
    print(“Executing the else statement”)
    print (“End of while loop”)

Output


In the next sections, we will discuss control statements, including the continue and break statements, within the while loop.

Control Statements

You can also control the execution or behavior of the while loop using the control statements. The following include the control statements that can be used with the while loop:

  1. continue
  2. break

The continue Statement

The continue statement is used to skip a specific iteration and perform the next iteration instead.

In this example, when the sum value is equal to 5, the current iteration is skipped, and the next iteration will start.

#declaring the sum variable
sum = 0
#starting while loop
while sum < 20:
    #incrementing 1 in the sum in every iteration
    sum=sum + 1
    #declaring the if condition
    if sum == 5:
        #the continue statement
        continue
    #printing the value of the sum
    print(“The value of sum is: “,sum)

Output
In this output, it can be seen that the program does not print the value of sum when the sum value is equal to 5. The current iteration is omitted and the following iteration is executed.

The break Statement

The break statement is used with the while loop to terminate the loop unless the test_condition is true. In this example, when the sum value is equal to 5, the loop is terminated.

#declaring the sum variable
sum = 0
#starting while loop
while sum < 20:
    #incrementing 1 in the sum in every iteration
    sum=sum + 1
    #declaring the if condition
    if sum == 5:
        #the break statement
        break
    #printing the value of the sum
    print(“The value of sum is: “,sum)

Output
In the output, it can be seen that the program terminates when the sum is equal to 5 unless the test_condition is true.

The infinite while Loop

A while loop that never terminates is called an infinite while loop. In an infinite while loop, the test_condition never becomes false. Instead, the test_condition always remains true, and the while loop never terminates.

In this example, the value of the sum variable is 10, and the test_condition is declared as sum=10. In this case, the value of the sum variable always remains 10, and the test_condition always remains true as the loop begins executing.

#declaring the sum variable
sum = 10
#starting while loop
while sum ==10:
    #printing the value of the sum
    print(“The value of sum is: “,sum)

Output

Conclusion

The Python while loop is used to repeatedly execute the same code block unless certain conditions are true, though infinite loops continue to execute regardless of existing conditions. In this article, the while loop in Python was explained by showing a few simple examples.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Twitter Now Supports Hardware Security Keys on Android and iOS

2020 was certainly not a great year for Twitter’s security team, case in point: the infamous high-profile Twitter Bitcoin scam. To recover from this...

FAU-G Records Over 1 Million Pre-Registrations in Just 24 Hours on Android

While the ministry is giving PUBG officials a cold shoulder, the game’s Atmanirbhar rival, FAU-G, is gearing up for a mammoth launch later this...

Apple to Release Mini LED MacBook Pro in 2021, MacBook Air in 2022: Kuo

We have heard about Apple working towards using Mini LED displays in its devices, including the MacBook Pro and MacBook Air. While earlier reports...

Top 9 Best Open Source Forum Software to Self Host [2020]

Looking to have a community forum or customer support portal? Here are some of the best open source forum software you can deploy on...