Electron microscopy
 
break and Functions to Exit a Loop
- Integrated Circuits -
- An Online Book -
Integrated Circuits                                                                                   http://www.globalsino.com/ICs/        


=================================================================================

 

==================================================

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:
      i) Break statement. This exits the current loop only. Normally do not use it, but a break statement is needed if it’s possible to end a loop w/o the condition being false. See Examples 2 - 6 below.
      ii) Return statement. This only exits the loop if the loop is inside of a function. On the other hand, the return statement will not only exit the loop, but will also exit the entire function.
      iii) raise Exception. Example 1 below.

==================================================

Example 1. Break function enabled with exception: code:
          continue in Python
Output:         
         continue in Python

==================================================

watchdog with conditioning break

==================================================

Example 2. "break" exits the loop immediately, and "continue" continues the loop regardless what and how many statements remain: code:
          continue in Python
Output:         
          continue in Python

==================================================

Example 3. break: code:
          continue in Python
Output:         
          continue in Python

==================================================

Example 4. break with a condition: code:
          continue in Python
Output:         
         continue in Python

==================================================

Example 5. break with a condition: check if a letter is in a string: code:
         continue in Python
Output:         
         continue in Python

==================================================

Example 6. break with a condition: check if a letter is in a string: code:
         continue in Python
Output:         
         continue in Python

 

=================================================================================