Differences between break and continue statements in Python (instance resolution)

Source: Internet
Author: User
Presumably everyone knows about the Python loop, and the countinue statement in Python is 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 Continue and break。 and continue and break two functions are similar to skip but there is no small difference, next I will take you to understand the two statements and then analyze the difference An example of break and continue

Both statements have the effect of terminating the loop statement, but the two statements are somewhat different.

The flowchart of the break statement is as follows:

(The break statement is used to terminate the loop statement, that is, the loop condition does not have a false condition or the sequence has not been fully recursive, but also stops executing the loop statement.) If you use a nested loop, the break statement stops executing the deepest loop and starts executing the next line of code. )

The syntax for break is as follows:

Break

Examples are as follows:

#!/usr/bin/python#-*-coding:utf-8-*-for letter in ' Python ':     # First Instance   if letter = = ' h ':      break   print ' current Letters: ', letter  var = ten                    # second instance while var > 0:                 print ' current variable value: ', var   var = var-1   If var = = 5:   # when Variable var equals 5 o'clock Exit loop break      print "Good bye!"

The results of the output are as follows:

Current Letter: P Current Letter: y current letter: T current variable value: 10 Current Variable Value: 9 Current variable Value: 8 Current Variable Value: 7 current variable value: 6Good bye!

the flowchart of the continue statement is as follows:

The syntax format for continue is as follows:

Continue

Examples are as follows:

#!/usr/bin/python#-*-coding:utf-8-*-for letter in ' Python ':     # First Instance   if letter = = ' h ':      continue   Print  ' Current letters: ', letter var = ten                    # second instance while var > 0:                 var = var-1   If var = = 5:      continue   print ' current variable value: ', Varprint "Good bye!"

The results of the output are as follows:

Current Letter: P Current Letter: y current letter: T current letter: o Current letter: N Current Variable Value: 9 Current variable Value: 8 Current Variable Value: 7 Current Variable value: 6 Current Variable Value: 4 Current variable values: 3 current Variable Value: 2 The current variable value: + 1 Current variable value: 0G Ood bye!

(The Continue statement tells Python to skip the remaining statements of the current loop and then proceed to the next cycle.) )

This article describes the differences between break and continue statements, and gives examples to aid in understanding the differences between the two, and hopefully this article will give you a little help in learning about Python.

For more information, please visit the PHP Chinese Web Python tutorial section.

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.