9
$\begingroup$

I found the following example in chapter two of Annotated Algorithms in Python (PDF)

>>> for i in [1, 2, 3]:
... print i
... continue
... print 'test'
1
2
3

This does demonstrate what continue does, but you would never write something like this in real code. You would just leave out continue and print "test" entirely. It seems like this example might be counter-productive.

Another type of example that I have seen elsewhere that I think might be counter-productive is indexing into string literals. For example, in some languages you can write the following:

"Hello World!"[0]

which would evaluate to "H" but if you actually wanted an expression that evaluated to "H", you would just use "H"!

Does it make sense to use contrived examples like this, or is it likely to introduce confusion in students?

$\endgroup$
7
$\begingroup$

This sort of thing is perfectly valid and valuable, provided that students already understand the underlying structure. So if they understand your first loop without continue (but with the print in place) the adding continue lets them see the effect dramatically.

The same is true of your second example. If they understand strings as arrays or lists (or ... whatever term is appropriate in your language) then introducing the [] "box" operator as selection makes sense.

But I wouldn't write a large program as an exemplar of good programming if it had these kinds of things.

But note also, that throwing in a continue in a loop is sometimes good for debugging, so it isn't entirely useless as a technique in "real" programming.

tl;dr teaching by showing modifications to a known base is fine.


By the way. In some languages asking students to decipher obfuscated code can be a useful exercise to make sure they know the basic rules. Asking them to clean it up is even better.

$\endgroup$
  • $\begingroup$ Question says 'introduce', so I think you're saying 'no, but later'? $\endgroup$ – Sean Houlihane Oct 4 '17 at 19:37
  • $\begingroup$ Actually no, I'm saying "yes", introduce is fine. Introduce "continue" into a known base. That was the point, actually. $\endgroup$ – Buffy Oct 4 '17 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.