有人能告诉我break语句和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:;
}
其他回答
简单的程序,了解继续和中断之间的区别
当使用continue时
public static void main(String[] args) {
System.out.println("HelloWorld");
for (int i = 0; i < 5; i++){
System.out.println("Start For loop i = " + i);
if(i==2){
System.out.println("Inside if Statement for i = "+i);
continue;
}
System.out.println("End For loop i = " + i);
}
System.out.println("Completely out of For loop");
}
OutPut:
HelloWorld
Start For loop i = 0
End For loop i = 0
Start For loop i = 1
End For loop i = 1
Start For loop i = 2
Inside if Statement for i = 2
Start For loop i = 3
End For loop i = 3
Start For loop i = 4
End For loop i = 4
Completely out of For loop
什么时候使用break
public static void main(String[] args) {
System.out.println("HelloWorld");
for (int i = 0; i < 5; i++){
System.out.println("Start For loop i = " + i);
if(i==2){
System.out.println("Inside if Statement for i = "+i);
break;
}
System.out.println("End For loop i = " + i);
}
System.out.println("Completely out of For loop");
}
Output:
HelloWorld
Start For loop i = 0
End For loop i = 0
Start For loop i = 1
End For loop i = 1
Start For loop i = 2
Inside if Statement for i = 2
Completely out of For loop
考虑以下几点:
int n;
for(n = 0; n < 10; ++n) {
break;
}
System.out.println(n);
Break导致循环终止,n的值为0。
int n;
for(n = 0; n < 10; ++n) {
continue;
}
System.out.println(n);
Continue使程序计数器返回到循环的第一行(检查条件,n的值为增量),n的最终值为10。
还应该注意的是,break只终止它所在循环的执行:
int m;
for(m = 0; m < 5; ++m)
{
int n;
for(n = 0; n < 5; ++n) {
break;
}
System.out.println(n);
}
System.out.println(m);
会输出什么效果
0
0
0
0
0
5
答得好,简单准确。
我会添加一个代码示例。
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
有关更多细节和代码示例,请参阅分支语句:
打破
break语句有两种形式:有标记的和无标记的。你们看到了 switch语句中未标记的形式。你 还可以使用未标记的中断来终止for, while或do-while吗 循环[…] 未标记的break语句终止最里面的开关,如下: While或do-while语句,但是标记的break终止了外层语句 声明。
继续
continue语句跳过for语句的当前迭代, 或者do-while循环。未标记的表单跳转到最内层的末尾 对象的布尔表达式求值 循环。[…] 带标签的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
推荐文章
- 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不是主类在其清单文件