Does the break statement break out of loops or only out of if statements?

前端 未结 9 1585
孤街浪徒
孤街浪徒 2020-12-16 15:12

In the following code, does the break statement break out of the if statement only or out of the for loop too?

I need it to b

相关标签:
9条回答
  • 2020-12-16 16:09

    break is to break out of any loop.

    0 讨论(0)
  • 2020-12-16 16:11

    It also goes out of the loop.

    You can also use labeled breaks that can break out of outer loops (and arbitrary code blocks).

    looplbl: for(int i=;i<;i++){
    
        if (i == temp)
            // do something
        else {
            temp = i;
            break looplbl;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 16:14

    An unlabelled break only breaks out of the enclosing switch, for, while or do-while construct. It does not take if statements into account.

    See http://download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html for more details.

    0 讨论(0)
提交回复
热议问题