• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with loop label problem

 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just can't seem to get any of these right:

So a plain break in the switch statement causes the switch statement ONLY to break out, right? Then how am I to think of the labeling?
When you apply a label, is it like you are giving the loop an identifier and saying "break label" means break out of that loop, and "continue label" means move to conditional check for that loop?
[ June 10, 2003: Message edited by: Brian Joseph ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest explanation I can give is that a break or continue statement will cause the enclosing context to terminate abruptly. That means that, if the break or continue statement is found within a for loop, it causes that for loop to terminate, if it's found within a switch statement, the switch statement terminates.
Of course, there is a slight difference between the two. Break causes the enclosing context to terminate abruptly and execution continues after that context. Continue, on the other hand, causes the enclosing context to terminate the current loop and continues in the next iteration of the enclosing context. (Note that continue statements are generally only used in loops, switch statements will almost always use a break statement.)
Ok, so what about labelled breaks and continues. Let's take a look at this example code:

What would be the output from uncommenting lines 1 through 6? Let's look at a couple of them.
If we use line 1, we have an unlabelled break statement. That means that the immediately enclosing context will terminate abruptly. What's the immediate closing context? That would be the inner loop that uses j as a counter. In order to find the immediately enclosing context, just trace back until you find a loop or switch statement in the code. Therefore, usign this line produces output like this:

What happens if we use line 2 instead of line 1? Well, now we terminate the context with the matching label. In this case, the context with the matching label is the inner loop, which is also the immediate enclosing context. Therefore, line 2 produces the same output as line 1.
How about line 3? Now, we abruptly halt the matching context, but that context is the outer loop, rather than the inner loop. Therefore, we get this output:

Try out the continue statements on your own and, if you have more questions, please let me know.
I hope that helps,
Corey
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey!
The only thing left in terms of labels is where can they go? Is it true that they can only be directly above the loop constructor or on the same line as the loop construct?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. If you tried to put them elsewhere, you'd get a compiler error indicating an unknown label or something to that effect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic