Python while Loop
Table of Contents
while 循环
表格内容
- 什么是while循环
- while循环的语法
- while循环的流程图
- 例子:python while循环
- 带else的while循环
What is while loop in Python?
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
We generally use this loop when we don't know beforehand, the number of times to iterate.
什么是while循环
在python中,使用while循环遍历整个代码库,只有测试表达式(条件)是正确的。
当我们不知道遍历次数上,我们一般使用这个循环。
Syntax of while Loop in Python
while test_expression:
Body of while
In while loop, test expression is checked first. The body of the loop is entered only if thetest_expression
evaluates to True
. After one iteration, the test expression is checked again. This process continues until the test_expression
evaluates to False
.
In Python, the body of the while loop is determined through indentation.
Body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True
. None
and 0
are interpreted as False
.
while循环语句
>>> while test_expreesion:
... Body of while
在while循环,首先检查测试条件。 只有test_expression估算是正确的,循环的主体才能进入。
在一次遍历后,这个表达式被检查一遍。这个程序继续直到这个test_epression计算为错误。
在python,是while的循环主体通过缩进决定。
主体由缩进决定,首行不缩减标志着结束。
python解释器为非0的值为正确,解释器中为None和0是False
Flowchart of while Loop
while循环流程图
Example: Python while Loop
# Program to add natural
# numbers upto
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
When you run the program, the output will be:
Enter n: 10
The sum is 55
例子:while循环
程序运行,结果如下:
>>> n = 10
>>> sum = 0
>>> i = 1
>>> while i <= n:
... sum = sum + i
... i = i+1
...
>>> print("The sum is",sum)
The sum is 55
In the above program, the test expression will be True
as long as our counter variable i is less than or equal to n (10 in our program).
We need to increase the value of counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never ending loop).
Finally the result is displayed.
上面程序,这个测试表达式是True的,只要我们计算的变量是小于或等于n(10 在我们程序里)。
我们需要增加计算变量的值在循环的主体里。这是很重要的(基本被人们遗忘了)。忘了这么做,因此会导致我们的结果无限循环(决没有结束的结尾)。
最后显示结果。
while loop with else
Same as that of for loop, we can have an optional else
block with while loop as well.
The else
part is executed if the condition in the while loop evaluates to False
.
The while loop can be terminated with a break statement. In such case, the else
part is ignored. Hence, a while loop's else
part runs if no break occurs and the condition is false.
Here is an example to illustrate this.
# Example to illustrate
# the use of else statement
# with the while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
Output
Inside loop
Inside loop
Inside loop
Inside else
带else的while循环
和for循环一样,我们可以带有while循环的可选else模块。
如果在while循环的条件估算是False的,这个else部分被执行。
这个while循环带有break语句的可以被终止。这种情况下,else部分是被忽略的。因此,如果没有break发生,条件是False的,这while循环的else部分会跑起来。
这里有一个例子去说明。输出:
>>> counter = 0
>>> while counter < 3:
... print("Inside loop")
... counter = counter + 1
... else:
... print("Inside else")
...
Inside loop
Inside loop
Inside loop
Inside else
Here, we use a counter variable to print the string Inside loop
three times.
On the forth iteration, the condition in while becomes False
. Hence, the else
part is executed.
因为,我们使用一个counter变量去打印字符串Inside loop 3次。
在第4次循环中,这条件变成了False在while循环中。因此,这else部分被执行。
Check out these examples to learn more:
- PREVIOUS
PYTHON FOR LOOP - NEXT
PYTHON BREAK AND CONTINUE STATEMENT