Java中关于break的功能


public class Example16 {
public static void main(String [] args)
{
  int x=1;
  while(x <= 4){

    System.out.println("x = " + x);

    if(3==x){
    break;

    }

    x++;
  }
    System.out.println("x = " + x);
  }
}

/*
* the result
x = 1
x = 2
x = 3
x = 3

*/

 

以上代码是java中关于break功能的举例,当break语句出现在嵌套循环中的内层循环时,如果想使用break语句跳出外层循环,则需对外层循环添加标记。此例中为什么运行结果不是x=1,x=2,x=3,x=4,因为if()不是循环语句,break直接跳出了while()循环。所以运行结果是x=1,x=2,x=3,x=3.

posted @ 2017-06-15 20:04  reedom1991  阅读(350)  评论(0编辑  收藏  举报