python中break outside loop_Python 2.5 While Loops

while Loops#while循环

An if statement is run once if its condition evaluates to True, and never if it evaluates to False.

A while statement is similar, except that it can be run more than once. The statements inside it are repeatedly executed, as long as the condition holds. Once it evaluates to False, the next section of code is executed.

Below is a while loop containing a variable that counts up from 1 to 5, at which point the loop terminates.

i = 1

while i <=5:

print(i)

i = i + 1

print("Finished!")

如果一个if语句的条件计算结果为True,那么它将运行一次,如果计算结果为False,则永不运行。

while语句类似,只是它可以运行不止一次。只要条件成立,其中的语句就会重复执行。一旦计算结果为False,就执行下一段代码。

下面是一个while循环,其中包含一个从1到5的变量,在这个值处循环终止。

i = 1

while i <=5:

print(i)

i = i + 1

print("Finished!")

The code in the body of a while loop is executed repeatedly. This is called iteration.

while循环体中的代码被重复执行。这叫做迭代。

The infinite loop is a special kind of while loop; it never stops running. Its condition always remains True.

An example of an infinite loop:

while 1==1:

print("In the loop")

This program would indefinitely print "In the loop".

You can stop the program's execution by using the Ctrl-C shortcut or by closing the program.

无限循环是一种特殊的while循环;它从不停止运行。它的条件总是正确的。

一个无限循环的例子:

while 1==1:

print("In the loop")

这个程序将无限期地打印“In the loop”。

您可以使用Ctrl-C快捷方式或关闭程序来停止程序的执行。

4aa545dccf7de8d4a93c2b2b8e3265ac0a26d216.png

break#中断

To end a while loop prematurely, the break statement can be used.

When encountered inside a loop, the break statement causes the loop to finish immediately.

i = 0

while 1==1:

print(i)

i = i + 1

if i >= 5:

print("Breaking")

break

print("Finished")

Result:

>>>

0

1

2

3

4

Breaking

Finished

>>>

要提前结束while循环,可以使用break语句。

当在循环中遇到break语句时,它会立即结束循环。

i = 0

while 1==1:

print(i)

i = i + 1

if i >= 5:

print("Breaking")

break

print("Finished")

Result:

>>>

0

1

2

3

4

Breaking

Finished

>>>

Using the break statement outside of a loop causes an error.

在循环外使用break语句会导致错误。

4aa545dccf7de8d4a93c2b2b8e3265ac0a26d216.png

continue

Another statement that can be used within loops is continue.

Unlike break, continue jumps back to the top of the loop, rather than stopping it.

i = 0

while True:

i = i +1

if i == 2:

print("Skipping 2")

continue

if i == 5:

print("Breaking")

break

print(i)

print("Finished")

Result:

>>>

1

Skipping 2

3

4

Breaking

Finished

>>>

可以在循环中使用的另一个语句是continue。

与break不同,continue跳转回循环的顶部,而不是停止它。

i = 0

while True:

i = i +1

if i == 2:

print("Skipping 2")

continue

if i == 5:

print("Breaking")

break

print(i)

print("Finished")

Result:

>>>

1

Skipping 2

3

4

Breaking

Finished

>>>

Basically, the continue statement stops the current iteration and continues with the next one.

基本上,continue语句停止当前迭代并继续下一个迭代。

Using the continue statement outside of a loop causes an error.

在循环外使用continue语句会导致错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值