While Loop in Python
Definition
A while loop in Python is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The code inside the loop will continue to execute as long as the condition is true. The block of code is defined by its indentation level relative to the while statement.
Overview
The while loop in Python executes a block of code as long as a specified condition is True. It can include break and continue statements to control the flow of the loop. The loop can optionally include an else block that executes if the loop completes without encountering a break statement. The break statement can terminate the loop prematurely, while the continue statement skips the rest of the loop body and reevaluates the condition.
Structure
The full while loop looks like this:
while condition:
body
else:
post-code
conditionis a Boolean expression that evaluates toTrueorFalse.bodyis executed repeatedly as long as theconditionisTrue.- When the
conditionevaluates toFalse, thewhileloop executes thepost-codesection and then terminates. - If the
conditionstarts out by beingFalse, thebodywon’t be executed at all—just thepost-codesection. - The
elsepart of thewhileloop is optional and not often used. It is executed only if the loop completes without abreak.
Examples
Basic While Loop
Here is a simple example of a while loop:
u, v, x, y = 0, 0, 100, 30
while x > y:
u = u + y
x = x - y
if x < y + 2:
v = v + x
x = 0
else:
v = v + y + 2
x = x - y - 2
print(u, v)
In this example, the output would be 60 40.
Factorial Calculation
Another example using a while loop to calculate the factorial of a number:
n = 9
r = 1
while n > 0:
r = r * n
n = n - 1
This code calculates the factorial of 9, leaving the result in the variable r.
User Input Loop
A while loop that repeats until the user enters a “Q”:
response = ""
while response != "Q":
response = input("Q to quit, B to break")
if response == "B":
break
else:
print("no break")
In this example, the break will force the while loop to end and skip the else block. The else block will only be executed if there is no break.
Comparison with Other Languages
Python differs from most other programming languages because it uses whitespace and indentation to determine block structure. Most languages use braces to do this. For example, here is C code that calculates the factorial of 9:
/* This is C code */
int n, r;
n = 9;
r = 1;
while (n > 0) {
r *= n;
n--;
}
In C, the braces delimit the body of the while loop, whereas in Python, indentation is used to achieve the same effect.
FAQ (Frequently asked questions)
What is a 'while' loop in Python?
When does the code inside a 'while' loop execute?
How does a 'while' loop function in Python?
What statements can be used to control the flow of a 'while' loop?
What is an example of a 'while' loop in Python?
What is the output of the provided 'while' loop example?
How is a block of code defined in a 'while' loop in Python?
What does the provided 'while' loop code do?
What shorthand operations does Python support in a 'while' loop?
What happens if a 'while' loop completes without a 'break' statement?
What is the purpose of the 'break' statement in a 'while' loop?
What does the 'continue' statement do in a 'while' loop?
What is the structure of a full 'while' loop in Python?