10
$\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$

1 Answer 1

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$
2
  • $\begingroup$ Question says 'introduce', so I think you're saying 'no, but later'? $\endgroup$ Oct 4, 2017 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, 2017 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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