liveBook · Manning

Stopping criterion

As explained in Get Programming: Learn to code with Python

A Boolean-valued test that determines which execution path a conditional construct should take.

A stopping criterion is a condition that tells a program when to stop repeating instructions or choose a different execution path. In a loop, the program checks the condition again after each pass; repetition ends when the condition becomes false. section 14.0 section 18.1

Guessing until the answer is correct

secret = "code" guess = input("Guess a word: ") while guess != secret: print("Try again") guess = input("Guess again: ") print("You got it!") The test guess != secret is the stopping criterion. A wrong guess keeps the loop running; the correct guess makes the condition false, so execution continues after the loop. section 18.1

A useful stopping criterion should make the intended endpoint clear:

  • Check whether repetition is still needed, rather than instructing the computer to repeat forever. section 16.1
  • Change something related to the condition inside the loop; otherwise, the program can enter an infinite loop. section 18.1
  • Use several conditions when a decision depends on more than one requirement, such as price range and phone color. section 14.0

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains repeated condition checks, condition updates, and infinite-loop risks, while stopping short of formal debugging methods.
Examples
Uses phone selection, lather-rinse repetition, and a secret-word guessing game to demonstrate conditions and termination.
What you can do after
Build it from scratch by writing conditions that select paths, repeat work, and stop loops at the intended point.
How it connects
Connects conditional statements, while loops, infinite loops, and Boolean-style true-or-false decisions.

Also explained in

Compare how 11 books explain stopping criterion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.