Difference between continue and break in a while loop

Source: Internet
Author: User

In addition to the while condition, there are two methods to terminate the loop: Break and continue. The only difference between them is that break jumps out of the entire loop and executes the following code directly. Continue terminates the current loop and directly enters the next loop without executing the following code, the difference between continue and pass is that although pass does not do anything, it continues to execute the following code. The following code illustrates the difference between break and continue.

Break:

 

Count =0WhileCount <= 100:Print('Loop', Count)IfCount = 5:BreakCount+ = 1Print("Out of loop ----")"""Loop 0 loop 1 loop 2 loop 3 Loop 4 loop 5out of Loop"""

After the break statement is executed, the loop is terminated directly.

 

Continue:

 
Count =0WhileCount <= 100:Print('Loop', Count)IfCount = 5:ContinueCount+ = 1Print("Out of loop ----")#Infinite Loop 5

 

When Count = 5, the program starts to continue. Instead, the program enters the next loop. Because count does not add 1, so in the next loop, count is equal to 5, and so is next time. Next time ......

Difference between continue and break in a while loop

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.