Home  >  Article  >  Backend Development  >  The difference between break and continue in python

The difference between break and continue in python

silencement
silencementOriginal
2019-06-20 15:36:209665browse

The difference between break and continue in python

Most people always confuse break and continue. Although they both end the cycle, they end in different ways.
break is used to end the entire loop.
continue is used to end the current loop.

break Sometimes when we write code, we want it to end the entire loop. In addition to the condition ending when it reaches False, we can set a condition. When it reaches this condition, the entire loop ends. break is used to completely jump out of the loop and execute the statements following the loop body.

while True:
    s = input('随便输入点什么:')

    if s =='quit':
        break

    print('你输入的字符串长度是{}'.format(len(s)))

print('完')

It can be seen from this code that when s == ‘quit’, this loop will end, otherwise it will run until the condition s is satisfied.

**2.**continue means to continue in English, but its function in the code is to end a cycle, but its original intention is to continue, so we can know its function in the code The function is to jump out of the current loop and then continue the subsequent loop, that is, it only ends the loop once.

while True:
    s = input('随便输入点什么:')

    if len(s) < 3:
        print(&#39;太短了,请输入三个字以上字符的内容。&#39;)
        continue


    print(&#39;你输入的内容是:{},长度是{}&#39;.format(s,len(s)))

After this code is executed, when the length of the condition s you enter is less than 3, the current loop will jump out, and then continue to execute the loop code. The entire loop will not be affected by this, and will still follow Works the same as before.

The above is the detailed content of The difference between break and continue in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn