user1016403
user1016403

Reputation: 12621

How to exit the loop?

I have below code. I wrote a for loop and inside that I have switch statement. The switch has two cases and if i==someOtherValue is true then entire loop should exit.

for (//iterate over elements){
    int i = someValueTakenFromLoop;
    if(i==someOtherValue){

        switch(i){
            case 5:
                //some logic
                break;
            case 6:
                //some logic
                break;
         }
     }
 }

While iterating if i==someOtherValue is true then it should exit the loop. Do i need to keep break statement out side switch?.

    for(//iterate over elements){
        int i = someValueTakenFromLoop;
        if(i==someOtherValue){
            switch(i){
                case 5:
                    //some logic
                    break;
                case 6:
                    //some logic
                    break;
            }
            break;
         }
     }

Thanks!

Upvotes: 0

Views: 214

Answers (6)

mprabhat
mprabhat

Reputation: 20323

Yes,

When you break inside switch control just comes out of switch but not from the outside loop, so you will have to break outside switch again to break out of the loop, or else use labeled loop, but in case you use labelled code please make them capitalized for better readability, so that they stand out and can be read clearly, though Java convention suggests to use camel casing.

1

for(...) {
      switch(...) {
         break; // this will break from switch
      }
      if(condition) {
         break; // this will break from for loop
      }
   }

2

OUTERLOOP:
for(...){
   switch(...){
       break OUTERLOOP;       
   }
}

Upvotes: 1

Score_Under
Score_Under

Reputation: 1216

edalorzo's solution is perfect, but I'd like to add one more thing (and don't have access to comments):

You can almost always restructure that kind of control structure using methods --

for ( /* ... */ ) {
    boolean finished = doSomething(i);
    if (finished) {
        break;
    }
}

//Later in the same class
private boolean doSomething(int val) {
    switch (val) {
        case 0:
            // do something;
            return true; // Break out of outer loop
        case 5:
            // do something absolutely terrifying
            break;
    }
    return false;
}

Edit: Just noticed you meant to break independent of the switch condition - rather simple:

for ( /* ... */ ) {
    if ( /* ... */ ) {
        switch ( /* ... */ ) { /* ... */ }
        break;
    }
}

Upvotes: 0

Ovilia
Ovilia

Reputation: 7256

Former break only makes it out of switch statement. The following one will work.

for(/*iterate over elements*/){
    int i = someValueTakenFromLoop;
    if(i==someOtherValue){
        switch(i){
        case 5:
            //some logic
            break;
        case 6:
            //some logic
            break;
        }
        break;
    }
}

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

You should put a break as specified below:

if  (i == someOtherValue ) {  
  break; // breaks for loop
} // for some other value
// switch( to continue here
...

Upvotes: 0

javatutorial
javatutorial

Reputation: 1944

If you have only 2 cases, why not using a simpler if-else if statement instead of switch?

Upvotes: 6

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78589

mylabel:
for(...){
   switch(...){
      case x: break mylabel;
   }
}

Upvotes: 7

Related Questions