Python Break and Continue statements

Source: Internet
Author: User

Briefly

In Python, the break and continue statements are used to change the flow of the normal loop.

Typically, a loop iterates through a piece of code until the condition is judged to be False. Sometimes, however, you may want to terminate the current iteration, or even the entire loop, without detecting the condition. In this case, you need to use the break and continue statements.

    • Briefly
    • Break statement
    • Continue statements

All rights reserved: A go, two or three Li, reprint please indicate source: http://blog.csdn.net/liang19890820

Break statement

Break is used to terminate the loop statement. Even if the loop condition is not False or the sequence has not been fully recursive, it will terminate.

Note: If the break statement is within a nested loop, break terminates the inner loop.

Syntax format:

break

Flow chart:

When we are intoxicated in the world of single cycle, suddenly a voice: The teacher came, to the swift and sudden close the song ^_^ !

i0whilei3:    ifi1:        print(‘老师来啦‘)        print(‘关闭歌曲‘)        break    print(‘正在播放:双节棍‘)    i1

Run the program with the output as follows:

Playing now: double-jointed sticks
Here comes the teacher.
Close Song

Well, how about a single loop 3 times? Just 1 times in circulation, the teacher came. No song to listen to is small, not good mp3 are to be confiscated ... Say more is a tear!

Continue statements

The Continue is used to skip the remaining code in the loop and is used only for the current iteration. The loop will not terminate, and the next iteration will continue.

Syntax format:

continue

Flow chart:

When the list plays, you will often choose the next song when you encounter songs you don't like:

songs = [‘安静‘‘蜗牛‘‘稻香‘]# 通过索引遍历列表forin range(len(songs)):    if1:        print(‘不想听‘, songs[i])        print(‘快进,下一曲‘)        continue    print("正在播放:", songs[i])

Run the program with the output as follows:

Playing: Quiet
Don't want to hear the snail
Fast Forward, next song
Playing now: Tao Heung

Imagine that looping will play through all the songs in the list in order. When play to "snail", found this song is too sensational, directly into the next song ...

the fundamental difference between the two: break is used to terminate the entire loop; Continue is used to jump out of this loop and continue the next cycle.

Python Break and Continue statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.