Use of break and continue in Python loop statements, pythoncontinue

Source: Internet
Author: User

Use of break and continue in Python loop statements, pythoncontinue

Python break statement
The Python break statement breaks the minimum closed for or while loop in the C language.
The break statement is used to terminate a loop statement. That is, if the loop condition does not have the False condition or the sequence is not completely recursive, the execution of the loop statement is also stopped.
The break statement is used in the while and for loops.
If you use nested loops, the break statement stops executing the deepest loop and starts executing the next line of code.
Break statement syntax in Python:

break


Flowchart:

Instance:

#!/usr/bin/pythonfor letter in 'Python':   # First Example  if letter == 'h':   break  print 'Current Letter :', letter var = 10          # Second Examplewhile var > 0:         print 'Current variable value :', var  var = var -1  if var == 5:   breakprint "Good bye!"

Execution result of the above instance:

Current Letter : PCurrent Letter : yCurrent Letter : tCurrent variable value : 10Current variable value : 9Current variable value : 8Current variable value : 7Current variable value : 6Good bye!

Python continue statement
The Python continue statement jumps out of this loop, while the break jumps out of the entire loop.
The continue statement is used to tell Python to skip the remaining statement of the current loop and then continue the next loop.
The continue statement is used in the while and for loops.
The syntax format of the Python continue statement is as follows:

continue


Flowchart:

Instance:

#! /Usr/bin/python #-*-coding: UTF-8-*-for letter in 'python': # first instance if letter = 'H ': continue print 'current letter: ', lettervar = 10 # The Second Instance while var> 0: var = var-1 if var = 5: continue print 'current variable value :', varprint "Good bye! "

Execution result of the above instance:

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 value: 3. Current variable value: 2. Current variable value: 1. Current variable value: 0 Good 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.