Python jumps out of the loop statement the difference between continue and break

Source: Internet
Author: User

Although the for loop in Python is not the same as other languages, jumping out of a loop is like most languages, you can use the keyword continue to jump out of this loop or break out of the entire for loop.
Break

For x in range: if X==5:break print X

The break loop used above, so the entire for loop jumps out when it executes to x==5, so the print X statement terminates only when it hits 4, so the output


The continue code is as follows:


For x in range: if x==5:continue print X

Execution Result: 0
1
2
3
4
6
7
8
9

The above loop uses the continue to jump out of this cycle, so only in the x==5 when the time out of the loop, and then continue the next time, so the print x statement is not executed at the time of x==5, the other values are executed, the output


Problem:

For I in Rangge (Ten): if i = = 2:continue elif i = = 5:break Print I


Python jumps out of the loop statement the difference between continue and break

Related Article

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.