Loops are used to run the identical block of code repeatedly in programming languages. In Python, the whereas loop runs the identical block of code till the take a look at situation is true. If the take a look at situation is fake, the loop ends, and the speedy line after the whereas loop is executed.
Inside the whereas loop, you’ll be able to have a single assertion or a number of statements, that are executed till the take a look at situation is true. This text describes the Python whereas loop and supplies some easy examples utilizing this perform.First, we’ll speak concerning the syntax of the whereas loop.

Syntax of whereas Loop

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

whereas test_condition:
assertion(s)

When the whereas loop is executed, the test_condition is evaluated first. When the test_condition is true, the loop physique or statements are executed inside the loop.

If the test_condition is true, then the physique of the loop or assertion(s) contained in the loop are executed. Executing the physique of the whereas loop solely as soon as is denoted as one iteration. The take a look at situation is checked after each iteration, and the whereas loop runs till the test_condition is fake.

Move of the whereas Loop

The determine given under depicts the circulation of the whereas loop.
Python while Loop – Linux Hint

Examples of whereas Loop

Now, allow us to have a look at some examples of the whereas loop and to know the way it works.

On this instance, first, we declare a sum variable whose worth is zero. The whereas loop is then began utilizing the whereas key phrase. The test_condition is sum<20. Which means the loop will run till the sum variable worth is lower than 20.

When the take a look at situation is fake and the sum worth is 20, the loop will terminate and this system will print “Finish of whereas loop.”

# declaring a variable
sum = 0
#beginning whereas loop
whereas (sum < 20):
#printing the worth of the sum
print (“The worth of sum is”, sum)
#incrementing 1 within the sum in each iteration
sum = sum + 1
print (“Finish of whereas loop”)

Output
Python while Loop – Linux Hint

You too can declare the else assertion with the whereas loop. The else block is functionalized when the whereas test_condition is fake. On this instance, we’ll add the else assertion to the sum instance given above.

# declaring a variable
sum = 0
#beginning whereas loop
whereas (sum < 20):
#printing the worth of the sum
print (“The worth of sum is”, sum)
#incrementing 1 within the sum in each iteration
sum = sum + 1
#writing the else assertion
else:
print(“Executing the else assertion”)
print (“Finish of whereas loop”)

Output
Python while Loop – Linux Hint

Within the subsequent sections, we’ll focus on management statements, together with the proceed and break statements, inside the whereas loop.

Management Statements

You too can management the execution or habits of the whereas loop utilizing the management statements. The next embody the management statements that can be utilized with the whereas loop:

  1. proceed
  2. break

The proceed Assertion

The proceed assertion is used to skip a particular iteration and carry out the subsequent iteration as a substitute.

On this instance, when the sum worth is the same as 5, the present iteration is skipped, and the subsequent iteration will begin.

#declaring the sum variable
sum = 0
#beginning whereas loop
whereas sum < 20:
#incrementing 1 within the sum in each iteration
sum=sum + 1
#declaring the if situation
if sum == 5:
#the proceed assertion
proceed
#printing the worth of the sum
print(“The worth of sum is: “,sum)

Output
On this output, it may be seen that this system doesn’t print the worth of sum when the sum worth is the same as 5. The present iteration is omitted and the next iteration is executed.
Python while Loop – Linux Hint

The break Assertion

The break assertion is used with the whereas loop to terminate the loop until the test_condition is true. On this instance, when the sum worth is the same as 5, the loop is terminated.

#declaring the sum variable
sum = 0
#beginning whereas loop
whereas sum < 20:
#incrementing 1 within the sum in each iteration
sum=sum + 1
#declaring the if situation
if sum == 5:
#the break assertion
break
#printing the worth of the sum
print(“The worth of sum is: “,sum)

Output
Within the output, it may be seen that this system terminates when the sum is the same as 5 until the test_condition is true.
Python while Loop – Linux Hint

The infinite whereas Loop

Some time loop that by no means terminates known as an infinite whereas loop. In an infinite whereas loop, the test_condition by no means turns into false. As an alternative, the test_condition all the time stays true, and the whereas loop by no means terminates.

On this instance, the worth of the sum variable is 10, and the test_condition is asserted as sum=10. On this case, the worth of the sum variable all the time stays 10, and the test_condition all the time stays true because the loop begins executing.

#declaring the sum variable
sum = 10
#beginning whereas loop
whereas sum ==10:
#printing the worth of the sum
print(“The worth of sum is: “,sum)

Output
Python while Loop – Linux Hint

Conclusion

The Python whereas loop is used to repeatedly execute the identical code block until sure situations are true, although infinite loops proceed to execute no matter present situations. On this article, the whereas loop in Python was defined by exhibiting just a few easy examples.

bash for each line in variable,bash for each line in file do command,bash script loop over file,zsh for loop file content,for loop with files in unix,fish loop over lines in file,linux system programming youtube,bash scripting full course 3 hours,ubuntu course,how to install django in ubuntu,how to install django in linux,linuxhint,infinite while loop in shell script,while loop in shell script to read file,do while loop in shell script,shell while true,until loop in shell script,while loop inside while loop in unix,while loop practice problems python,python while loop exercises pdf,python while loop multiple conditions,python while loop user input,print out the numbers 1 through 7 python,python while not true

Share: