Let's say I have this:
while(a){
while(b){
if(b == 10)
break;
}
}
Question: Will the break statement take me out of both loops or only from the inner one? Thank you.
In your example break statement will take you out of while(b) loop
while(a) {
while(b) {
if(b == 10) {
break;
}
}
// break will take you here.
}
It will break only the most immediate while loop. Using a label you can break out of both loops: take a look at this example taken from here
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
Only from the inner one. Use labeled break if you wish to break to specific loop
label1:
for(){
label2:
for(){
if(condition1)
break label1;//break outerloop
if(condition2)
break label2;//break innerloop
}
}
Also See
while (a) {
while (b) {
if (b == 10) {
break;
}
}
}
In the above code you will break the inner most loop where (ie. immediate loop
) where break
is used.
You can break both the loops at once using the break
with label
label1:
while (a) {
while (b) {
if (b == 10) {
break label1;
}
}
}
It will break out of the loop that immediately encloses it.
You can however, break to a label:
myLabel:
while(a) {
while(b) {
if(b == 10)
break myLabel;
}
}
I don't generally like to use this pattern because it easily leads to spaghetti code. Use an unlabeled break or a flag to terminate your loop.
@Abhishekkumar
Break keyword has it's derived root from C and Assembly, and Break it's sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.
Please refer these...
http://tigcc.ticalc.org/doc/keywords.html#break
http://www.functionx.com/cpp/keywords/break.htm
http://en.wikipedia.org/wiki/Break_statement#Early_exit_from_loops
So, if you want to get out of Two loops at same time then you've to use two Breaks, i.e. one in inner loop and one in outer loop.
But you want to stop both loop at same time then you must have to use exit or return.
A break
statement will take you out of the innermost loop enclosing that break
statement.
In the example the inner while loop.
As a curious note, in PHP the break statement accept a numeric parameter which tells how many outer loops you want to break, like this:
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
The java break statement won't take you out of multiple nested loops.
Only the inner loop of course.
You can raise a flag to pass the information to the outer while loop. In this case the information can be stored in a variable breakBothLoopsFlag and the outer while loop acts according to this information. See pseudo code below:
int breakBothLoopsFlag = 0;
while(a){
while(b){
if(b == 10) {
breakBothLoopsFlag = 1;
break;
}
}
if(breakBothLoopsFlag == 1) {
break;
}
}
来源:https://stackoverflow.com/questions/12393231/break-statement-inside-two-while-loops