Python中断并继续

本文介绍了Python编程中的break和continue语句,它们用于在循环中控制流程。break语句用于立即退出循环,而continue语句则跳过当前循环的剩余部分,继续下一次迭代。通过示例代码,展示了如何在for和while循环中使用这两个语句来实现特定的循环行为。
摘要由CSDN通过智能技术生成

Today we will learn about Python break and continue statements. In previous post, we learnt about python while loop.

今天,我们将学习Python break和continue语句。 在上一篇文章中,我们了解了python while loop

Python中断并继续 (Python Break and Continue)

Python break and continue statements are used only in loop. It helps to modify loop behaviour. Suppose you’re executing a loop, at one phase you need to terminate the loop or skip some statements then you need these statements.

Python break和continue语句仅在循环中使用。 它有助于修改循环行为。 假设您正在执行循环,在一个阶段中,您需要终止循环或跳过某些语句,然后需要这些语句。

Python中断 (Python break)

This statement is used to break the loop. Suppose you’re printing odd numbers using while loop. But you don’t need to print numbers greater than 10. Then you can write the following python code.

该语句用于中断循环。 假设您正在使用while循环打印奇数。 但是您不需要打印大于10的数字。那么您可以编写以下python代码。

number = 1 #Number is initially 1
 
while True : #This means the loop will continue infinite time
        print (number) #print the number
        number+=2 #calculate next odd number
 
        # Now give the breaking condition
        if number > 10:
                break;
                #Breaks the loop if number is greater than ten
                print (number) #This statement won't be executed

Python break example output

python break example

Python Break示例输出

In the given example you will see that the statement(s) that are written after break, won’t be executed. So here, 11 will not be printed.

在给定的示例中,您将看到中断后编写的语句将不会执行。 因此,此处将不会打印11。

Python break statement can be used in for loop also. Suppose you are printing words from a list. If any words that matches with “exit” won’t be printed and the loop will terminate. The following python code illustrates the idea.

Python break语句也可以在for循环中使用。 假设您要从列表中打印单词。 如果任何与“ exit”匹配的单词都不会被打印,则循环将终止。 以下python代码说明了这一想法。

words = ["rain", "sun", "moon", "exit", "weather"]
 
for word in words:
        #checking for the breaking condition
        if word == "exit" :
                #if the condition is true, then break the loop
                break;
 
        #Otherwise, print the word
        print (word)

Python break for example

python break statement example with for loop

以Python中断为例

Python继续 (Python continue)

Python continue statement is used to skip the statement of the loop and iterate to the next step. For example you’re printing number 1 to 10. You need to skip all statements at step 7. The following python code illustrates the scenario.

Python的continue语句用于跳过循环语句并迭代到下一步。 例如,您要打印数字1到10。您需要在步骤7跳过所有语句。以下python代码说明了这种情况。

numbers = range(1,11)
'''
the range(a,b) function creates a list of number 1 to (b-1)
So, in this case it would generate
numbers from 1 to 10
'''
for number in numbers:
        #check the skipping condition
        if number == 7:
                #this statement will be executed
                print("7 is skipped")
                continue
                #this statement won't be executed
                print ("This won't be printed")
 
        #print the values
        #for example:
        #2 is double of 1
        print (number*2),
        print ("is double of"),
        print (number)

Python continue example output

python continue example code

Python继续示例输出

Same thing you can do for while loop. Suppose, you are trying to print all the odd values from 1 to 10, then the python code which solves the problem would be:

您可以为while循环做同样的事情。 假设您尝试打印从1到10的所有奇数值,那么解决该问题的python代码将是:

numbers = [ 1, 2, 4, 3, 6, 5, 7, 10, 9 ]
pos = 0 #initial position is one
while pos < len(numbers):
        #checking skipping condition if number is divisible by two, it is even
        if numbers[pos] % 2 == 0 :
                #increment the position by one
                pos = pos + 1
                continue
        #print the odd number
        print (numbers[pos])
        #increment the position by one
        pos = pos + 1

Python continue example while loop

python continue while loop example

Python在循环时继续示例

That’s all about python break and continue statement. Hope that you have learnt well. If you have any query about this topic, feel free to ask that in comment section.
#HappyCoding

这就是关于python break and Continue语句的全部内容。 希望你学得很好。 如果您对此主题有任何疑问,请随时在评论部分提出。
#HappyCoding

翻译自: https://www.journaldev.com/14161/python-break-python-continue

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值