Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix
| Relevant functions: return, return, limit, quit/exit. ================================================== In Python, the main way to exit a loop is by using the break statement. When the break statement runs in a loop, it will terminate that loop. However, the break statements will only terminate the innermost loop that it is run inside. Therefore, if you have a nested loop, the outer loop will continue to run. One things which need to keep in mind are that exiting loops is very tricky and there are many ways to exit a loop: ================================================== Example 1. Break function enabled with exception: code: ================================================== watchdog with conditioning break ================================================== Example 2. "break" exits the loop immediately, and "continue" continues the loop regardless what and how many statements remain: code: ================================================== Example 3. break: code: ================================================== Example 4. break with a condition: code: ================================================== Example 5. break with a condition: check if a letter is in a string: code: ================================================== Example 6. break with a condition: check if a letter is in a string: code:
|