开发者学堂课程【【名师课堂】Java 零基础入门:程序逻辑控制(循环控制)】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/370/detail/4417
程序逻辑控制(循环控制)
内容简介:
一、continue
二、break
在进行循环处理时,有两个关键词:continue、break,一般此类语句都会结合 if 判断使用。
一、continue
continue 为执行到此语句时将跳过循环体的剩余部分。
public class TestDemo {
public static void main (String args [ ]) {
for (int x = 0 ; x < 10 ; x ++) {
if (x == 3) {
continue ;
执行此语句之后循环体后面的代码不再执行
}
System.out.println (“x = ” + x) ;
}
}
}
二、break
break 指退出整个循环
public class TestDemo {
public static void main (String args [ ]) {
for (int x = 0 ; x < 10 ; x ++) {
if (x == 3) {
break ;
执行此语句之后循环体后面的代码不再执行
}
System.out.println (“x = ” + x) ;
}
}
}
其他语言中有一种 goto 功能,这种功能一般不会出现在 Java 中,也没有关键字,但是可以利用 continue 实现与之一样的功能(但一般不用)
public class TestDemo {
public static void main (String args [ ]) {
mp: for (int x = 0 ; x < 2 ; x ++) {
for (int y = 0 ; y < 3 ; y ++) {
if (x > 2) {
continue mp ;
}
System.out.println (“x = “ + x + ”, y = ” + y) ;
}
}
}
}
break、continue 语句操作很少使用。