0

I have a code like:

while(condition)
{
    foreach(string s in list)
    {
        switch(j)
        {
            case 1:
            //do something
            continue;//can I break out of foreach here?
            case 2:
            //do another
            continue;//break 
            ..other cases;
        }
        //do some stuff
    }
    //do some stuff
}

I am considering goto but I've heard many bad things about it.

rtuner
  • 2,362
  • 3
  • 25
  • 37
  • 3
    Please add the relevant language tag to your question... – Oliver Charlesworth May 26 '13 at 16:57
  • you can set `flag` and break if the `flag` is set. and thus avoid goto....but `"a wholesale ban (on goto) is just silly"` -- @Jon Purdy http://stackoverflow.com/a/16555996/436084 – Bill May 26 '13 at 16:58
  • You shouldn't break out of a for each. Loop with a while and an iterator, and with a flag as second condition. – Noctua May 26 '13 at 17:00
  • 1
    if you put the whole thing in a function, you can just put a return statement whereever you want to break. – Kevin May 26 '13 at 17:00
  • What Oli said. The answers will be very different in, for example, C++ and Perl. – aschepler May 26 '13 at 17:06
  • Ooops, very sorry. I didn't have Internet connection on weekend. Thanks – rtuner May 27 '13 at 04:32

4 Answers4

1

Simply use a Boolean variable:

while(condition)
{
    foreach(string s in list)
    {
        var breakout = false;
        switch(j)
        {
            case 1:
                //do something
                breakout = true;
                break;
            // ...
        }

        if(breakout)
        {
            break;
        }
    }
    //do some stuff
}
Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
1

I assume what you are trying to do is break from inside the switch, and have it exit the for loop but not the while loop. My suggestion is to put the whole thing into a function and put a return statement wherever you want to break. For example:

void f () {
        foreach(string s in list) {
            switch(j) {
                case 1:
                //do something
                return; 
                case 2:
                //do another
                continue;//break 
                ..other cases;
             }
         //do some stuff
        }
}

// ... later somewhere
while (condition) {
    f();
}
Kevin
  • 24,871
  • 19
  • 102
  • 158
  • 3
    in modern times there is a definite preference for readable code over micro-optimization. Function calls are fast enough in 99.9% of cases. – Kevin May 26 '13 at 17:09
  • yes, but too many stack unwinding can be expensive in some circumstances. But your solution def works and nothing wrong :) – Bill May 26 '13 at 17:11
  • 1
    In some cases like this, the C# compiler can decide to make the function inline, which would render the performance question essentially moot. You are far better off writing the cleanest, most readable code possible and letting the compiler take care of micro, micro optimisations. – Levi Botelho May 27 '13 at 06:56
1

boolean state variable should do the trick:

bool isBadInput = false;
bool isRunning = true;

while(isRunning && !isBadInput){
  for(int j = 0; j < list.size() && !isBadInput; ++j){
     switch(j){
       case 0: 
         int res = handleCase0();
         if(res == -1){ 
            isBadInput = true;
            isRunning = false;
         }
         break;
      //similar for other cases
  }
}
dchhetri
  • 6,926
  • 4
  • 43
  • 56
  • Yeah, I thought of a variable but I wanted to know if there was any better structural featured for this. – rtuner May 27 '13 at 04:38
  • If structure is what you want then consider making those loops into function if possible. That way its easier to read and reuse – dchhetri May 27 '13 at 04:52
0

I am considering goto but I've heard many bad things about it.

Then you should ensure you are fully-informed before making a decision. This subject is covered in depth by the articles linked from GOTO still considered harmful?.

Write the code that you believe succinctly expresses your intention, is idiomatic to your programming language and ultimately that will be easiest to maintain.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • I think ny question is reacting based on [this answer](http://stackoverflow.com/a/46789/1261657). – rtuner May 27 '13 at 04:40
  • ...and my answer is (partially) based on [this comment](http://stackoverflow.com/q/46586/78845#comment19670998_46789) to that answer. `goto` statements have their place (although I cannot recall the last time I actually had to use one). In this case I would probably just `return` from the function, but that might not be appropriate in all cases. – johnsyweb May 27 '13 at 11:48