directory

  • A break.
  • 2. Continue
  • Three. Key summary
  • Four. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

At the end of the Python while loop article, we left a bug that the program’s while loop gets stuck in an infinite loop when the condition is always True. How to fix it?

To circumvent this problem, today we introduce Python with two key words: break and continue;

A break.

If you use break in a loop, it means to immediately break out of the loop and go straight to code:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file: Python break/continue. Py @time :2021/3/20 00:05 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! If a == 100, if a == 100, if a == 100, if a == 100, if a == 100, if a == 100, if a == 100, if a == 100, if a == 100 Print ("a = %d" % a) print("a = %d" % a) print("a = 1 a = 2 a = 3 a = 4.... A = 96 a = 97 a = 98 a = 99 End of loop, exit programCopy the code

The code above is while in an infinite loop, which exits the loop only if the condition a == 100 is true. Remember the keyword break.

2. Continue

If continue is used in a loop, it means to end the loop and continue the next loop, directly demonstrating the code:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file: Python break/continue. Py @time :2021/3/20 00:05 @motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! If a == 100 while a == 0 if a == 100 while a == 1 Print ("a = %d" % a) print(" end of loop, exit program ")  a = 1 a = 2 a = 3 a = 4 ... a = 96 a = 97 a = 98 a = 99 a = 101 a = 102 a = 103 .... ' ' 'Copy the code

“Continue”, “continue”, “continue”, “continue”, “continue”, “continue”, “continue”, “continue”

Because a continue is executed when a is 100, the program code skips the continue and returns to the beginning of the while loop.

Both “break” and “continue” were introduced above. Sense the difference?

  1. Break is to end the loop; the current while stops sequentially;
  2. Continue is to end the current loop and continue the next loop, but the loop has not actually stopped;

Three. Key summary

The use of the keywords break and continue in the while loop is essential; note the difference between the two

  1. Break: to end a loop;
  2. Continue: Completes the current loop and continues the next loop;

For example, if you play basketball, “continue” is a halftime break. If break is used, the match ends.

Four. Guess you like it

  1. Introduction of Python
  2. Python Pycharm Anacanda difference
  3. Python2.x and python3. x, how to choose?
  4. Python Configuration Environment
  5. Introduction to Python Hello World
  6. Python code comments
  7. Python Chinese encoding
  8. Python variable
  9. What is Anaconda? Anconda download the installation tutorial
  10. Pycharm prompt: this license **** has been cancelled
  11. Pycharm Sets the development template/font size/background color

Python break/continue

This article is published by the blog – Ape Say Programming Ape Say programming!