Net-informations.com

Python while loop Statements

Loops are one of the most important features in computer programming languages . As the name suggests is the process that get repeated again and again . It offer a quick and easy way to do something repeated until a certain condition is reached. Every loop has 3 parts:

  1. Initialization
  2. Condition
  3. Updation

How to python while loop

Python while loop

Syntax

In Python, while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. That means, while loop tells the computer to do something as long as the condition is met. It consists of condition/expression and a block of code. The condition/expression is evaluated, and if the condition/expression is true, the code within the block is executed. This repeats until the condition/expression becomes false.

For example, if I initialize the value of a variable x as 0 and set the condition x < =5 then the condition will be held true. But if I set the condition x>=5 the condition will become false. After checking the condition in while clause, if it holds true, the body of the loop is executed. While executing the body of loop it can update the statement inside while loop . After updating, the condition is checked again. This process is repeated as long as the condition is true and once the condition becomes false the program breaks out of the loop.

example

output

Here the conditional of x < =5 (while(x < =5):) and x was previously declared and set equal to 0 (x=0). So, the first item printed out was 0 (print(x)), which makes sense. In the next line x+=1 means x = x+1, now the value of x = 1. After updating x , the condition is checked again. This process is repeated as long as the condition is true and once the condition becomes false the program breaks out of the loop . Of course, once a becomes equal to 5, we will no longer run through the loop.

Python while loop: break and continue

Python provides two keywords that terminate a loop iteration prematurely: break and continue.

  1. break leaves a loop.
  2. continue jumps to the next iteration.

break statement in Python while loop


break statement in python while loop

Sometimes it's necessary to exit from a Python while loop before the loop has finished fully iterating over all the step values. This is typically achieved by a "break" statement.

example

output

In the above example, when the condition x>20, the break statement executed and immediately terminated the while loop and the program control resumes at the next statement.

continue statement in Python while loop


continue statement in python while loop

The continue statement in Python while loop is used when we want to skip one or more statements in loop's body and to transfer the control to the next iteration.

example

output

In the above example, we can see in the output the 30 is missing. It is because when the condition x==30 the loop encounter the continue statement and control go back to start of the loop.

Else clause on Python while statement

Syntax

This is a unique feature of Python and not found in most other programming languages. The else clause in Python while loop is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed.

example

output

In the above example, you can see the condition is (x < =10). Up to the condition, the while block executed and the final value of x=11. Then the condition is false and the control goes to else clause of while loop and print the statement.

example

output

Here the initial value of x=11 and condition is (x < =10). So there is no way to enter inside the while loop . So, the control directly goes to else block of while loop and print the statement.

Nested while Loops


python nested while loop

A nested while loop is a loop within a while loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer while loop triggers the inner while loop , which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.

example

output

One-Line while Loops

As with an if statement, a Python while loop can be specified on one line. If there are multiple statements in the loop code block that makes up the loop body , they can be separated by semicolons (;):

example

output

is same as:

output

Python Infinite while Loop


python infinite while loop

As the name suggests, an infinite loop runs forever. In the context of a computer programming, this means that the loops runs until the program terminates . An infinite loop can be useful in a program that is always looking for some kind of input. While it is waiting for the input, it can be sitting idle. We can program an infinite loop using Python while statement . If the condition of while loop is always True, we get an infinite loop.

example

How to Emulate a do-while loop in Python?

Python doesn't have do-while loop . The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluated at the bottom of the loop body.

output










net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About