Python while Loop
while is a conditional loop statement in Python; as long as the condition is True, the loop body keeps executing.
The difference between for in Vue 2,while The loop is better suited to scenarios where the number of iterations is uncertain and the exit depends on a condition.
Word meaning: while means "while...", indicating continuous execution while the condition is true.
Basic syntax and parameters
Syntax format
while 条件:
# Loop body (must be indented)
语句1
语句2
...
else:
# Optional, executes when the loop "normally finishes"
语句1
语句2
...
Syntax Description
- Conditional: the conditional expression evaluated before each iteration.
- Execution logic: when True, execute the loop body and then evaluate the condition again.
- Exit condition: exit the loop when the condition is False.
else clause
- Optional: like for, while can also have an else.
- Execution Timing: executed when the condition becomes False (normal exit).

Example
Example 1: basic usage
Example
A basic while loop
count
=
0
while
count
<
5
:
(
count
)
count +
=
1
(
"Loop ended"
)
Expected output:
0
1
2
3
4
循环结束
Code analysis:
- When count < is less than 5, keep looping.
- Each iteration increases count by 1.
- When count = 5, the condition is False and the loop exits.
Example 2: user input validation
Example
Simulate user input validation
Note: when actually run, input() waits for user input
Simple example: guessing a number
secret
=
7
guess
=
0
while
guess
!=
secret:
guess
=
int
(
input
(
"Please enter a number (7): "
)
)
if
guess
<
secret:
(
"Too small"
)
elif
guess
secret:
(
"Too large"
)
(
"Congratulations, you guessed it!"
)
Expected output:
请输入数字 (7): 5
太小了
请输入数字 (7): 9
太大了
请输入数字 (7): 7
恭喜,猜对了!
while loops are often used in scenarios that need to repeat until a condition is met.
Example 3: calculating a factorial
Example
Calculate the factorial
n
=
5
result
=
1
while
n
0
:
result *
=
n
n -
=
1
(
f
"5! = {result}"
)
Output: 5! = 120
Accumulated sum
total
=
0
i
=
1
while
i
<=
100
:
total +
=
i
i +
=
1
(
f
"1+2+...+100 = {total}"
)
Output: 5050
Expected output:
5! = 120
1+2+...+100 = 5050
When using while loops for numerical computation, be careful to avoid infinite loops.
Example 4: the while-else structure
Example
while-else
count
=
0
while
count
<
3
:
(
count
)
count +
=
1
else
:
(
"The loop ended normally"
)
(
"---"
)
else is not executed when break is used
count
=
0
while
count
<
5
:
if
count
==
3
:
break
(
count
)
count +
=
1
else
:
(
"This line will not be printed"
)
Expected output:
0
1
2
循环正常结束
---
0
1
2
In while-else, the else is not executed when terminated by break.
Example 5: infinite loops and exiting
Example
Infinite loops need to be used with care
The following example uses break to exit
Simulate a menu system
while
True
:
(
"=== Menu ==="
)
(
"1. Check balance"
)
(
"2. Deposit"
)
(
"3. Withdraw"
)
(
"4. Exit"
)
choice
=
input
(
"Please choose: "
)
if
choice
==
"1"
:
(
"Balance: 1000 yuan"
)
elif
choice
==
"2"
:
(
"Deposit function"
)
elif
choice
==
"3"
:
(
"Withdrawal function"
)
elif
choice
==
"4"
:
(
"Thank you for using"
)
break
Exit the loop
else
:
(
"Invalid choice"
)
(
"Program ends"
)
Combining infinite loops with break enables complex control flows.
Expected output:
=== 菜单 ===
1. 查看余额
2. 存款
3. 取款
4. 退出
请选择: 1
余额: 1000元
=== 菜单 ===
1. 查看余额
2. 存款
3. 取款
4. 退出
Please select:
Python3 loop statements