Friday, June 28, 2019

Writing Loops With Break and Continue

The simplest form of a loop in python is the while True: infinite loop but you can pass other expressions to the while loop that will make it infinite.

i = 0
while 1:
 i += 1
 print("Hello "+ str(i))

The above will do the same thing as passing while True.

You can write other conditional expressions in a while loop to manipulate the number of repetitions.

i = 0
while i <= 10:
 i += 1
 print("Hello "+ str(i))

The above will print hello n 10 times instead of printing forever.

The break Statement

You can also write infinite loops and break out of them using the break statement in python. This is very common and replaces the goto statement that is often demonized in modern programming.  When you break out of a while loop or any other loop your code stops execution where break is encountered and continue execution after the while loop.

Recall that python works with indentation so that the while loop ends when the indentation is no longer there.


i = 0
while 1:
 i += 1
 if i <= 10:
  break
 print("Hello "+ str(i))
a = i

In the case above we do the same loop but a little less compact. When the break statement is hit execution continues at a= i. 

The continue Statement

Similar to the break statement the continue statement is used to control the flow of the while loop. Instead of exiting the while or other looping mechanism it is used to skip statements below it and go to the beginning of the loop. It will re-evaluate the condition. Because we are doing an infinite while loop it will always be true. It is usually combined with an if statement like above but the if is not required in either one.


i = 0
while 1:
 i += 1
 if i <= 10:
  break
 print("Hello "+ str(i))
a = i

The above program prints hello n exactly like the other two examples except it skips the print command when i has the value 5. If i = 5 all statements under continue are skipped. Often times you will see code that always evaluates to true or simply has a continue statement without a conditional statement ( if statement ). This is sometimes used to skip code that should not be ran. It is equivalent to commenting code. However, it is bad practice and code that never runs is referred to as DEAD code.

Dead code is frowned upon and is often the target of re-factoring or re-writing of a program.


while 1: 
 print("Hello")
 continue
 print("World") # <---- DEAD CODE

The above program will print Hello forever / infinite but never reaches print("World").

More about loops in python can be found here:

https://www.w3schools.com/python/python_while_loops.asp




I must confess that I have sinned in this way many times. Often times I write "dead code" like the above example because I am troubleshooting the loop to see what will happen if I skip to the end or to see what happens under certain conditions if I continue or break out of the loop. Unfortunately, "The road to hell is paved with good intentions" and I sometimes forget to take those statements out before committing or saving my work.

No comments:

Post a Comment

nonlocal keyword

The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keywo...