break和continue

来源:互联网 发布:淘宝美工全攻略pdf 编辑:程序博客网 时间:2024/06/02 18:55

下列语句可被用在更深层次的控制循环语句中:

break [标注];

continue[标注];

label: 语句; // 语句必须是有效的

break语句被用来从switch语句、loop语句和预先给定了label的块中退出。

continue语句被用来略过并跳到循环体的结尾。

label可标识控制需要转换到的任何有效语句,它被用来标识循环构造的复合语句。

例如:

loop: while (true){

for (into I = 0; I < 100; I++) { 

switch (c = System.in.read()) { 

case -1: 

case ' \n ':   

break loop; 

.... 

} // end for

} // end while

test:  for (...) { 

.... 

while (...) { 

if (j > 10) { 

// jumps to the increment portion of

// for-loop at line #13

continue test; 

} // end while

} // end for

计算1100的合计值。

public class Number {

public into sum(){

into totle = 0;

for (into I = 1; I <= 100; I++){

totle += I;

}

return totle;

}

public static void main(String[] args){

Number obj = new Number();

into temp = obj.sum();

System.out.println(temp);

}

}

输出结果