While Loops With Python

By AIC Computer Education 06-Mar-2021

Python provides following types of loops to handle looping requirements. Python provides three types of loops. While all the ways provide similar basic functionality, they differ in their syntax and time to check condition.

While Loop: while loop executes a block of statements repeatedly until a given a condition is true. when the condition becomes false.the line immediately after the loop in program is executed.

Syntax

i = 1
while i < 6:
  print(i)
  i += 1

 

Use else statement with while loops: while loop executes the block until a condition is satisfied. When the condition becomes false, the statement right after the loop get executed.else clause is only executed when your while condition becomes false.If you break the loop, or if an exception is raised, block in else will not be executed.

Syntax 

while condition:
     # execute these statements
else:
     # execute these statements

Single statement while block: Just like the if block, if the while block consists of a single statement the we can declare the entire loop in a single line as shown below:

count = 0
while (count == 0): print("Hello World")