Update 8th June: I’ve also noticed that the MIT’s Introduction to Computer Science and Programming uses Break extensively in their 1st week. So the argument that universities don’t use break is pretty weak.
There has been a very interesting discussion in SEACSTA about the use of breaks in Python. It has been quite a learning journey for me and so I wanted to share MY opinions on what I’ve read and what I now think. If you disagree, please feel free to do so in the comments.
The argument against not using breaks at all is neatly summarised as “In structured programming, the integrity of the loop is that there is only one point of control… the boolean expression. Break creates multiple alternatives.” (Simon Carter)
The difficulty in Python is that there are no repeat loops, which test a condition at the end of the loop. such as the repeat…while in Swift. So I quite often let students use code like this:
For most while loops it doesn’t matter if the condition is met without running, but for validation the code has to be run at least once!
Alternative: Using a boolean Variable
One suggestion made is to use an additional variable, which ends the loop once True. See this example:
The downsides to this approach are that, firstly this code is longer to write out and secondly the minute a student tries to add a second validation then they will inevitably use the variable validated, which has now been set to True by the first loop!
Nested Loops
Carl’s main concern for beginner programmers were nested loops. “The moment they start nesting loops they won’t be able to stop the outer loop very easily”
This is a concern and once students get to a stage that they can create functions, this would enable them to validate an input in a function, but in iGCSE this is going to be challenging for them.
The case for break statements
In Python there are quite a number of alternative methods, but many come with downsides such as additional lines of code, additional complexity or in extreme situations performance hits. (Particularly if you dream up recursive solutions.) Adam takes us through a few of these scenarios:
The Community View
Dr. Charles Severance (the University of Michigan) stated quite strongly, ” But any teacher who says break is verboten never has coded in the real world.”
Raymond Hettinger, Silicon valley consultant and youtuber for Python “Use “break” is a perfectly reasonable way to break out of a for-loop or while-loop. In fact, that is its intended purpose 🙂 An alternative is to move the loop into a function and use “return” for a possible early exit.”
The vast majority of the community on the Twitter chat said that breaks were ok. (currently at 94%, but let’s see how that changes)
Need help! I use While True…Break to make a #repeat style loop in #Python. But a lot of teachers seem to think break is a really bad thing! Please could you settle this for me by voting and commenting! See example at https://t.co/pzjBqGd0hE @brainysmurf @drchuck #caschat
— James Abela (@eslweb) May 11, 2019
Conclusion (My Takeaways)
I’m much more aware of the pros and cons of this break issue and will certainly be encouraging my students to use functions as soon as they are ready for it.
However all of the alternatives in Python for validating input seem more clunky and just as error prone, so unless you can persuade me in the comments I will continue to teach it that way…