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!
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
2