The use of the break and continue of the Python looping statement

Source: Internet
Author: User
Python Break Statement
The Python break statement, like in the C language, breaks the minimum enclosing for or while loop.
The break statement terminates the Loop statement, that is, the loop condition does not have a false condition, or the sequence is not fully recursive, and the loop statement is stopped.
The break statement is used in the while and for loops.
If you use a nested loop, the break statement stops executing the deepest loop and starts executing the next line of code.
Python language break statement syntax:

Break


Flow chart:

Instance:

#!/usr/bin/pythonfor "Python": # First   Example if letter  = = ' h ': Break  print ' Current letter: ' , Letter var = ten          # Second examplewhile var > 0:         print ' current variable value: ', var  var = var-1  If var = = 5:   breakprint "Good bye!"

The result of the above instance execution:

Current letter:pcurrent letter:ycurrent letter:tcurrent variable value:10current variable value:9current variabl e value:8current variable value:7current variable value:6good bye!

Python Continue statement
The Python continue statement jumps out of the loop, and break jumps out of the loop.
The continue statement is used to tell Python to skip the remaining statements of the current loop, and then proceed to the next round of loops.
The continue statement is used in the while and for loops.
The Python language continue statement syntax is formatted as follows:

Continue


Flow chart:

Instance:

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

The result of the above instance execution:

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!
  • 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.