Python While Loop (original) (raw)

Last Updated : 10 Dec, 2024

Try it on GfG Practice redirect icon

**Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.

In this example, the condition for while will be True as long as the counter variable (count) is less than 3.

Python `

Python example for while loop

count = 0 while (count < 3): count = count + 1 print("Hello Geek")

`

Output

Hello Geek Hello Geek Hello Geek

Let’s take a look at Python While Loop in detail:

Table of Content

**while loop Syntax

**while expression:
statement(s)

**While Loop Flowchart

Python While Loop

While Loop

The while loop will continue running the code block as long as the condition evaluates to True. Each time the loop executes, the condition is checked again. If it is True, the loop continues; if it is False, the loop terminates, and the program moves to the next statement after the loop.

Infinite while Loop in Python

Here, the value of the condition is always True. Therefore, the body of the loop is run infinite times until the memory is full.

Python `

age = 28

the test condition is always True

while age > 19: print('Infinite Loop')

`

Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

**while loop with continue statement

Python Continue Statement returns the control to the beginning of the loop.

Python `

Prints all letters except 'e' and 's'

i = 0 a = 'geeksforgeeks'

while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 continue

print(a[i])
i += 1

`

**while loop with break statement

Python Break Statement brings control out of the loop.

Python `

break the loop as soon it sees 'e'

or 's'

i = 0 a = 'geeksforgeeks'

while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 break

print(a[i])
i += 1

`

**while loop with pass statement

The Python pass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.

Python `

An empty loop

a = 'geeksforgeeks' i = 0

while i < len(a): i += 1 pass

print('Value of i :', i)

`

while loop with else

As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause 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.

**Note: The else block just after for/while is executed only when the loop is NOT terminated by a break statement.

Python `

Python program to demonstrate

while-else loop

i = 0 while i < 4: i += 1 print(i) else: # Executed because no break in for print("No Break\n")

i = 0 while i < 4: i += 1 print(i) break else: # Not executed as there is a break print("No Break")

`