有人能告诉我break语句和continue语句的区别吗?
当前回答
首先,我认为你应该知道在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语句会导致它所应用的语句(switch、for、do或while)的终止。
continue语句用于结束当前循环迭代并将控制权返回给循环语句。
下面是break的语义:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// find 9
for(int i = 0; i < a.Length; i++)
{
if (a[i] == 9)
goto goBreak;
Console.WriteLine(a[i].ToString());
}
goBreak:;
下面是continue的语义:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// skip all odds
for(int i = 0; i < a.Length; i++)
{
if (a[i] % 2 == 1)
goto goContinue;
Console.WriteLine(a[i].ToString());
goContinue:;
}
简单的例子:
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
break语句跳出循环(要执行的下一个语句是右大括号之后的第一个语句),而continue语句在下一个迭代中开始循环。
答得好,简单准确。
我会添加一个代码示例。
C:\oreyes\samples\java\breakcontinue>type BreakContinue.java
class BreakContinue {
public static void main( String [] args ) {
for( int i = 0 ; i < 10 ; i++ ) {
if( i % 2 == 0) { // if pair, will jump
continue; // don't go to "System.out.print" below.
}
System.out.println("The number is " + i );
if( i == 7 ) {
break; // will end the execution, 8,9 wont be processed
}
}
}
}
C:\oreyes\samples\java\breakcontinue>java BreakContinue
The number is 1
The number is 3
The number is 5
The number is 7
推荐文章
- 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不是主类在其清单文件