I don't believe that your question is entirely valid; some languages require jumping. The first principle, therefore, is to follow the norms of your language.
However, I suspect that you are asking about languages that discourage (but do not ban) jumping, such as Java, or C++. In these cases, I agree with Peter that the solution is to give them the negative learning experience of having to parse code with bad jumping usage.
I would only add two things to what he said:
Do not teach about break
statements and jump
statements if you can avoid it until they really have have absorbed the norms of looping. This can take some time. If kids start using them earlier than you would like, simply tell the students that, while such statements have a time and place, please avoid using them for now.
As an idea for an exercise, just make some code that has for loops and replace them with awful while(true)
loops. For instance, consider this bit of foul excrement, which will print out the first 10 Fibonacci numbers:
int daffyDuck = 10;
int[] bugs = new int[daffyDuck];
bugs[0] = 0;
bugs[1] = 1;
int whynot = -1;
while(true){
whynot = whynot+1;
int whistlersmother = 0;
if (whynot == 10) break;
int animeRocks = 0;
while(true){
if (whynot-animeRocks <= 2)
whistlersmother += bugs[animeRocks];
animeRocks++;
if(animeRocks>whynot)
break;
}
bugs[whynot] = whistlersmother;
System.out.print(bugs[whynot]+",");
}
Give it to your students, but don't tell them what it does. Give them a chance to attempt to trace it. Tell them that they only have 4 minutes (240 seconds!)1 to figure out what it does! Presumably, they will struggle mightily to figure it out, but mention that, if anyone thinks that they've solved it (and can back that up), not to call anything out, but to instead raise their hand. If anyone does call you over, ask the to whisper the answer into your ear, and if they get it right, ask them to silently try to re-write the code segment in a way that would be easier to follow.
Now, give the class a new version, with another 240 seconds. Do not mention that it is exactly the same code as before, but with nicely-defined loops (and modified variable names). Once again, tell them not to call out the answers. The process here should be the same.
int n = 10;
int[] f = new int[n];
f[0] = 0;
f[1] = 1;
for(int c = 0; c < f.length; c++){
int s = 0;
for(int j = 0; j <= c; j++){
if (c-j <= 2)
s += f[j];
}
f[c] = s;
System.out.print(f[c]+",");
}
And finally, give them this version, same 240 seconds, same procedure:
int fib_nums_to_print = 10;
int[] fibonacciNums = new int[fib_nums_to_print];
fibonacciNums[0] = 0;
fibonacciNums[1] = 1;
for(int currentNum = 0; currentNum < fibonacciNums.length; currentNum++){
int sum_so_far = 0;
for(int j = 0; j <= currentNum; j++){
if (currentNum-j <= 2)
sum_so_far += fibonacciNums[j];
}
fibonacciNums[currentNum] = sum_so_far;
System.out.print(fibonacciNums[currentNum]+",");
}
At this point, you can reveal, for those that hadn't figured it out yet, that all 3 of these bits of code were the same function. Ask them what made the later versions easier. They should be able to pinpoint the two problems pretty easily.
Point out that loops can grow to be very, very large - hundreds, or even thousands of lines of code long. Ask them why the while(true)
version of the loop would be especially problematic in longer code examples. Ask them where, in their opinion, would it be best in such long code for the coder to make clear when the loop will actually end.
All of which brings us to the final principle: we want to be able to figure out, at least in 99.9% of the cases, the basic logic of what will cause a loop to terminate at the moment that the loop is declared, because otherwise, we are left hunting and guessing.
1 - Any time you mention the number of seconds along with the number of minutes, the kids suddenly feel time pressure. It's a very consistent trick to get the kids to focus on a short task.
break
statements can be good practice. What language is this instruction in? $\endgroup$break
,continue
, etc. all have a place and a time. The trick is to learning when and why. I recommend showing the power as well as the danger in using them. I'm always leery when someone says "All X are bad" - like the guy who said he never used loops, only LINQ. Time and place... $\endgroup$