1

I was asked to do maintenance work on some C# code which, I'm told, was originally converted from Visual Basic 6. (I mention this only because I don't know VB6 so I don't know if it would have made more sense in that language. . .)

It's got a for loop which parses some text for a proprietary scripting language by using a switch inside the for loop . . .

for ( t = 0; t < upperBound(tokens); t++)
{
    String mystring = tokens[t];
    switch (mystring)  
    {
        case "GOTO":
            if (firstGoto == -1)
            {
                firstGoto = t;
            } 
            else
            {
                // compute number of tokens in GOTO
                pointLength = t - firstGoto - 1;
                break;  // exit for
            }
            break;
       case "ACTUATE"
 . . . 

Notice the comment

   // exit for

The programmer expects the break will exit the for loop but I think it will only exit the switch statement because the documentation for break says

The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.

So am I correct that this will only exit the switch, but still be in the for, and if so, what is the correct way to do what the original programmer intended?

chridam
  • 100,957
  • 23
  • 236
  • 235
user316117
  • 7,971
  • 20
  • 83
  • 158
  • 6
    Surely you could figure this out by either running the code or writing a simple test harness to see what happens? – Daniel Kelley Jun 19 '14 at 15:22
  • Is your question "Am I correct", or is it "How can I..."? For the latter, see [Break out of a while loop that contains a switch statement](http://stackoverflow.com/questions/1987379/break-out-of-a-while-loop-that-contains-a-switch-statement). – CodeCaster Jun 19 '14 at 15:24
  • The correct way (IMO) is to encapsulate this for loop in a method and return from the method – Sayse Jun 19 '14 at 15:25

3 Answers3

8

Yes, break will break out of the closest loop or switch. The easiest way is to use a goto. (No, goto is not evil)

for {
switch(...) {
        ....
        goto MyLabel;
    }
}
MyLabel: 
Dennis_E
  • 8,751
  • 23
  • 29
  • 4
    +1 for bravery. Not to mention that switch is really just a structured set of goto's. – Smeegs Jun 19 '14 at 15:27
  • 2
    @Smeegs Many statements are goto's in disguise (return, break, continue, if-else) I don't understand the anti-goto movement. It's like racism: everyone is allowed to jump unless your name is goto. :) – Dennis_E Jun 19 '14 at 15:47
2

Yes, you are correct... break will only exit the innermost block of code.

One easy way to do this is to also have a boolean flag which is set within the switch statement, and at the end of the loop just do this:

var flag = false;
for (...) {

    switch (...) {
        case "x":
            flag = true;
    }        

    if (flag) break;
}
BG100
  • 4,481
  • 2
  • 37
  • 64
1

Yes break will only terminate the switch in this scenario. How about using a boolean to exit the for as well..?

for ( t = 0; t < upperBound(tokens); t++)
{
String mystring = tokens[t];
bool exitFor = false;
switch (mystring)  
{
    case "GOTO":
        if (firstGoto == -1)
        {
            firstGoto = t;
        } 
        else
        {
            // compute number of tokens in GOTO
            pointLength = t - firstGoto - 1;
            exitFor = true;
            break;  // exit for
        }
        break;
   case "ACTUATE"

. . .

if(exitFor)
    break;
jmelhus
  • 1,130
  • 2
  • 12
  • 28