The difference between the Python skip loop statement continue and break, pythoncontinue

Source: Internet
Author: User

The difference between the Python skip loop statement continue and break, pythoncontinue

Although the for Loop in Python is not the same as that in other languages, the bounce loop is the same as that in most languages. You can use the keyword continue to jump out of this loop or break to jump out of the entire for loop.
Break
Copy codeThe Code is as follows:
# Encoding = UTF-8
 
For x in range (10 ):
If x = 5:
Break
 
Print x

The previous break loop is used, so the entire for loop exists when x = 5 is executed. Therefore, when the print x statement hits 4, the output result is terminated.

Continue
Copy codeThe Code is as follows:
# Encoding = UTF-8
 
For x in range (10 ):
If x = 5:
Continue
 
Print x

The above loop uses the continue to jump out of this loop, so it only jumps out of this loop when x = 5, and then continues next time, therefore, the print x statement is not executed only when x = 5, other values are executed, and the output result is


What is the difference between a continue statement and a break statement?

Break Statement (Force end loop)
Roles of the break statement: 1. It can be used to jump out of the loop body from the loop body, that is, to end the loop in advance, and then execute the following statements in the loop. 2. Jump the process out of the switch structure
Note: The break statement cannot be used in any statement other than the loop statement and switch statement.
The function of the continue statement is to end this loop, that is, to ignore the unexecuted statements under the continue statement in the loop body, and then determine whether to execute the next loop.
Note: The continue statement cannot be used in any statement other than the loop statement.
The difference between a continue statement and a break statement:
The continue statement only ends this loop, instead of terminating the execution of the entire loop.
The break statement ends the entire cycle and does not judge whether the execution cycle conditions are true. The break statement can be used in loop statements and switch statements. It is used to end an internal loop in a loop statement; it is used to jump out of a switch statement in a switch statement.
Note: During loop nesting, break and continue only affect the innermost loop containing them, and are irrelevant to the outer loop.

What is the difference between the break statement and the continue statement in the loop statement in VC ++?

After the break is executed, the loop exists. After the continue is executed, the loop exists.

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.