有人能告诉我break语句和continue语句的区别吗?
当前回答
Break完全离开循环,并在循环之后执行语句。 而Continue则离开当前迭代并执行循环中的下一个值。
这段代码解释了一切:
public static void main(String[] args) {
for(int i=0;i<10;i++)
{
if (i==4)
{
break;
}
System.out.print(i+"\t");
}
System.out.println();
for(int i=0;i<10;i++)
{
if (i==4)
{
continue;
}
System.out.print(i+"\t");
}
}
输出:
0 1 2 3
0 1 2 3 5 6 7 8 9
其他回答
Break完全离开循环,并在循环之后执行语句。 而Continue则离开当前迭代并执行循环中的下一个值。
这段代码解释了一切:
public static void main(String[] args) {
for(int i=0;i<10;i++)
{
if (i==4)
{
break;
}
System.out.print(i+"\t");
}
System.out.println();
for(int i=0;i<10;i++)
{
if (i==4)
{
continue;
}
System.out.print(i+"\t");
}
}
输出:
0 1 2 3
0 1 2 3 5 6 7 8 9
有关更多细节和代码示例,请参阅分支语句:
打破
break语句有两种形式:有标记的和无标记的。你们看到了 switch语句中未标记的形式。你 还可以使用未标记的中断来终止for, while或do-while吗 循环[…] 未标记的break语句终止最里面的开关,如下: While或do-while语句,但是标记的break终止了外层语句 声明。
继续
continue语句跳过for语句的当前迭代, 或者do-while循环。未标记的表单跳转到最内层的末尾 对象的布尔表达式求值 循环。[…] 带标签的continue语句跳过使用给定标签标记的外部循环的当前迭代。
Break只留下一个循环,继续跳转到下一个迭代。
首先,我认为你应该知道在Java中有两种类型的中断和继续,即有标记的中断,无标记的中断,有标记的继续和无标记的继续。现在,我将谈谈它们之间的区别。
class BreakDemo {
public static void main(String[] args) {
int[] arrayOfInts =
{ 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchfor = 12;
int i;
boolean foundIt = false;
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;//this is an unlabeled break,an unlabeled break statement terminates the innermost switch,for,while,do-while statement.
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}
}
未标记的break语句终止最里面的开关、for、while、do-while语句。
public class BreakWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
break search;//this is an labeled break.To notice the lab which is search.
}
}
}
带标记的break终止外部语句。如果你javac和Java这个演示,你会得到:
0 - 0
0 - 1
0 - 2
0 - 3
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;//this is an unlabeled continue.
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
未标记的continue语句将跳过for、while、do-while语句的当前迭代。
public class ContinueWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
continue search;//this is an labeled continue.Notice the lab which is search
}
}
}
一个带标签的continue语句将跳过当前使用给定标签标记的外部循环的迭代,如果你javac和java演示,你将得到:
0 - 0
0 - 1
0 - 2
0 - 3
1 - 0
1 - 1
1 - 2
1 - 3
2 - 0
2 - 1
2 - 2
2 - 3
如果你有任何问题,你可以看看这个Java教程:在这里输入链接描述
简单的例子:
Break离开了循环。
int m = 0;
for(int n = 0; n < 5; ++n){
if(n == 2){
break;
}
m++;
}
System.out.printl("m:"+m); // m:2
Continue将返回开始循环。
int m = 0;
for(int n = 0; n < 5; ++n){
if(n == 2){
continue; // Go back to start and dont execute m++
}
m++;
}
System.out.printl("m:"+m); // m:4
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件