问题
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.
回答1:
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.
}
回答2:
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");
}
}
回答3:
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
- labeled break
回答4:
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;
}
}
}
回答5:
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.
回答6:
@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.
回答7:
A break
statement will take you out of the innermost loop enclosing that break
statement.
In the example the inner while loop.
回答8:
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;
}
}
回答9:
The java break statement won't take you out of multiple nested loops.
回答10:
Only the inner loop of course.
回答11:
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