“比如说 if-else、switch、for、while、do-while、return、break、continue 等等,接下来,我们一个个来了解下。”

    1)if 语句

    if 语句的格式如下:

    画个流程图表示一下:

    流程控制语句有哪些?图解版 - 图2

    来写个示例:

    1. public static void main(String[] args) {
    2. int age = 20;
    3. if (age < 30) {
    4. System.out.println("青春年华");
    5. }
    6. }
    7. }

    输出:

    1. 青春年华

    2)if-else 语句

    if-else 语句的格式如下:

    1. if(布尔表达式){
    2. // 条件为 true 时执行的代码块
    3. }else{
    4. // 条件为 false 时执行的代码块
    5. }

    画个流程图表示一下:

    来写个示例:

    1. public class IfElseExample {
    2. public static void main(String[] args) {
    3. int age = 31;
    4. if (age < 30) {
    5. System.out.println("青春年华");
    6. } else {
    7. System.out.println("而立之年");
    8. }
    9. }
    10. }

    输出:

    1. 而立之年

    除了这个例子之外,还有一个判断闰年(被 4 整除但不能被 100 整除或者被 400 整除)的例子:

    1. public class LeapYear {
    2. public static void main(String[] args) {
    3. int year = 2020;
    4. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
    5. System.out.println("闰年");
    6. } else {
    7. System.out.println("普通年份");
    8. }
    9. }
    10. }

    输出:

    1. 闰年

    如果执行语句比较简单的话,可以使用三元运算符来代替 if-else 语句,如果条件为 true,返回 ? 后面 : 前面的值;如果条件为 false,返回 : 后面的值。

    1. public class IfElseTernaryExample {
    2. public static void main(String[] args) {
    3. int num = 13;
    4. String result = (num % 2 == 0) ? "偶数" : "奇数";
    5. System.out.println(result);
    6. }
    7. }

    输出:

    1. 奇数

    3)if-else-if 语句

    if-else-if 语句的格式如下:

    1. if(条件1){
    2. // 条件1 为 true 时执行的代码
    3. }else if(条件2){
    4. // 条件2 为 true 时执行的代码
    5. }
    6. else if(条件3){
    7. // 条件3 为 true 时执行的代码
    8. }
    9. ...
    10. else{
    11. // 以上条件均为 false 时执行的代码
    12. }

    画个流程图表示一下:

    流程控制语句有哪些?图解版 - 图4

    来写个示例:

    1. public class IfElseIfExample {
    2. public static void main(String[] args) {
    3. int age = 31;
    4. if (age < 30) {
    5. System.out.println("青春年华");
    6. } else if (age >= 30 && age < 40 ) {
    7. System.out.println("而立之年");
    8. } else if (age >= 40 && age < 50 ) {
    9. System.out.println("不惑之年");
    10. } else {
    11. System.out.println("知天命");
    12. }
    13. }
    14. }

    输出:

    1. 而立之年

    4)if 嵌套语句

    if 嵌套语句的格式如下:

    1. if(外侧条件){
    2. // 外侧条件为 true 时执行的代码
    3. if(内侧条件){
    4. // 内侧条件为 true 时执行的代码
    5. }
    6. }

    画个流程图表示一下:

    来写个示例:

    1. public class NestedIfExample {
    2. public static void main(String[] args) {
    3. int age = 20;
    4. boolean isGirl = true;
    5. if (age >= 20) {
    6. if (isGirl) {
    7. System.out.println("女生法定结婚年龄");
    8. }
    9. }
    10. }
    11. }

    输出:

    1. 女生法定结婚年龄

    02、switch 语句

    switch 语句用来判断变量与多个值之间的相等性。变量的类型可以是 byte、short、int、long,或者对应的包装器类型 Byte、Short、Integer、Long,以及字符串和枚举。

    来看一下 switch 语句的格式:

    1. switch(变量) {
    2. case 可选值1:
    3. // 可选值1匹配后执行的代码;
    4. break; // 该关键字是可选项
    5. case 可选值2:
    6. // 可选值2匹配后执行的代码;
    7. break; // 该关键字是可选项
    8. ......
    9. default: // 该关键字是可选项
    10. // 所有可选值都不匹配后执行的代码
    11. }
    • 变量可以有 1 个或者 N 个值。

    • 值类型必须和变量类型是一致的,并且值是确定的。

    • 值必须是唯一的,不能重复,否则编译会出错。

    • break 关键字是可选的,如果没有,则执行下一个 case,如果有,则跳出 switch 语句。

    • default 关键字也是可选的。

    画个流程图:

    流程控制语句有哪些?图解版 - 图6

    来个示例:

    1. public class Switch1 {
    2. public static void main(String[] args) {
    3. int age = 20;
    4. switch (age) {
    5. case 20 :
    6. System.out.println("上学");
    7. break;
    8. case 24 :
    9. System.out.println("苏州工作");
    10. break;
    11. case 30 :
    12. System.out.println("洛阳工作");
    13. break;
    14. default:
    15. System.out.println("未知");
    16. break; // 可省略
    17. }
    18. }
    19. }

    输出:

    1. public class Switch2 {
    2. public static void main(String[] args) {
    3. String name = "沉默王二";
    4. switch (name) {
    5. System.out.println("篮球运动员");
    6. break;
    7. case "穆里尼奥":
    8. System.out.println("足球教练");
    9. break;
    10. case "沉默王二":
    11. case "沉默王三":
    12. System.out.println("乒乓球爱好者");
    13. default:
    14. throw new IllegalArgumentException(
    15. "名字没有匹配项");
    16. }
    17. }
    18. }

    输出:

    1. 乒乓球爱好者

    枚举作为 switch 语句的变量也很常见,来看例子:

    1. public class SwitchEnumDemo {
    2. public enum PlayerTypes {
    3. TENNIS,
    4. FOOTBALL,
    5. BASKETBALL,
    6. UNKNOWN
    7. }
    8. public static void main(String[] args) {
    9. System.out.println(createPlayer(PlayerTypes.BASKETBALL));
    10. }
    11. private static String createPlayer(PlayerTypes playerType) {
    12. switch (playerType) {
    13. case TENNIS:
    14. return "网球运动员费德勒";
    15. case FOOTBALL:
    16. return "足球运动员C罗";
    17. case BASKETBALL:
    18. return "篮球运动员詹姆斯";
    19. case UNKNOWN:
    20. throw new IllegalArgumentException("未知");
    21. default:
    22. throw new IllegalArgumentException(
    23. "运动员类型: " + playerType);
    24. }
    25. }
    26. }

    输出:

    1. 篮球运动员詹姆斯

    1)普通 for 循环

    普通的 for 循环可以分为 4 个部分:

    1)初始变量:循环开始执行时的初始条件。

    2)条件:循环每次执行时要判断的条件,如果为 true,就执行循环体;如果为 false,就跳出循环。当然了,条件是可选的,如果没有条件,则会一直循环。

    3)循环体:循环每次要执行的代码块,直到条件变为 false。

    4)自增/自减:初识变量变化的方式。

    来看一下普通 for 循环的格式:

    1. for(初识变量;条件;自增/自减){
    2. // 循环体
    3. }

    画个流程图:

    流程控制语句有哪些?图解版 - 图8

    来个示例:

    1. public class ForExample {
    2. public static void main(String[] args) {
    3. for (int i = 0; i < 5; i++) {
    4. System.out.println("沉默王三好美啊");
    5. }
    6. }
    7. }

    输出:

    1. 沉默王三好美啊
    2. 沉默王三好美啊
    3. 沉默王三好美啊
    4. 沉默王三好美啊
    5. 沉默王三好美啊

    “哎呀,二哥,你真的是变着法夸我啊。”

    “非也非也,三妹,你看不出我其实在夸我自己吗?循环语句还可以嵌套呢,这样就可以打印出更好玩的呢,你要不要看看?”

    “好呀好呀!”

    “看好了啊。”

    1. public class PyramidForExample {
    2. public static void main(String[] args) {
    3. for (int i = 0; i < 5; i++) {
    4. for (int j = 0;j<= i;j++) {
    5. System.out.print("❤");
    6. }
    7. System.out.println();
    8. }
    9. }
    10. }

    打印出什么玩意呢?

    1. ❤❤
    2. ❤❤❤
    3. ❤❤❤❤
    4. ❤❤❤❤❤

    “哇,太不可思议了,二哥。”

    “嘿嘿。”

    2)for-each

    for-each 循环通常用于遍历数组和集合,它的使用规则比普通的 for 循环还要简单,不需要初始变量,不需要条件,不需要下标来自增或者自减。来看一下语法:

    1. for(元素类型 元素 : 数组或集合){
    2. // 要执行的代码
    3. }

    来看一下示例:

    1. public class ForEachExample {
    2. public static void main(String[] args) {
    3. String[] strs = {"沉默王二", "一枚有趣的程序员"};
    4. for (String str : strs) {
    5. System.out.println(str);
    6. }
    7. }
    8. }

    输出:

    1. 沉默王二
    2. 一枚有趣的程序员

    “呀,二哥,你开始王哥卖瓜了啊。”

    “嘿嘿,三妹,你这样说哥会脸红的。”

    3)无限 for 循环

    “三妹,你想不想体验一下无限 for 循环的威力,也就是死循环。”

    “二哥,那会有什么样的后果啊?”

    “来,看看就知道了。”

    1. public class InfinitiveForExample {
    2. public static void main(String[] args) {
    3. for(;;){
    4. System.out.println("停不下来。。。。");
    5. }
    6. }
    7. }

    输出:

    1. 停不下来。。。。
    2. 停不下来。。。。
    3. 停不下来。。。。
    4. 停不下来。。。。

    一旦运行起来,就停不下来了,除非强制停止。

    04、while 循环

    来看一下 while 循环的格式:

    1. while(条件){
    2. //循环体
    3. }

    画个流程图:

    来个示例:

    1. public class WhileExample {
    2. public static void main(String[] args) {
    3. int i = 0;
    4. while (true) {
    5. System.out.println("沉默王三");
    6. i++;
    7. if (i == 5) {
    8. break;
    9. }
    10. }
    11. }
    12. }

    “三妹,你猜猜会输出几次?”

    “五次吗?”

    “对了,你可真聪明。”

    1. 沉默王三
    2. 沉默王三
    3. 沉默王三
    4. 沉默王三
    5. 沉默王三

    “二哥,那会有什么样的后果啊?”

    “来,看看就知道了。”

    输出:

    1. 停不下来。。。。
    2. 停不下来。。。。
    3. 停不下来。。。。
    4. 停不下来。。。。

    把 while 的条件设置为 true,并且循环体中没有 break 关键字的话,程序一旦运行起来,就根本停不下来了,除非强制停止。

    来看一下 do-while 循环的格式:

    1. do{
    2. // 循环体
    3. }while(提交);

    画个流程图:

    流程控制语句有哪些?图解版 - 图10

    来个示例:

    1. public class DoWhileExample {
    2. public static void main(String[] args) {
    3. int i = 0;
    4. do {
    5. System.out.println("沉默王三");
    6. i++;
    7. if (i == 5) {
    8. }
    9. } while (true);
    10. }
    11. }

    “三妹,你猜猜会输出几次?”

    “五次吗?”

    “对了,你可真聪明。”

    1. 沉默王三
    2. 沉默王三
    3. 沉默王三
    4. 沉默王三

    “三妹,你想不想体验一下无限 do-while 循环的威力……”

    “二哥,又来啊,我都腻了。”

    “来吧,例行公事,就假装看看嘛。”

    1. public class InfinitiveDoWhileExample {
    2. public static void main(String[] args) {
    3. do {
    4. System.out.println("停不下来。。。。");
    5. } while (true);
    6. }
    7. }

    输出:

    1. 停不下来。。。。
    2. 停不下来。。。。
    3. 停不下来。。。。
    4. 停不下来。。。。

    把 do-while 的条件设置为 true,并且循环体中没有 break 关键字的话,程序一旦运行起来,就根本停不下来了,除非强制停止。

    06、break

    break 关键字通常用于中断循环或 switch 语句,它在指定条件下中断程序的当前流程。如果是内部循环,则仅中断内部循环。

    可以将 break 关键字用于所有类型循环语句中,比如说 for 循环、while 循环,以及 do-while 循环。

    来画个流程图感受一下:

    流程控制语句有哪些?图解版 - 图12

    用在 for 循环中的示例:

    1. for (int i = 1; i <= 10; i++) {
    2. if (i == 5) {
    3. break;
    4. }
    5. System.out.println(i);
    6. }

    用在嵌套 for 循环中的示例:

    1. for (int i = 1; i <= 3; i++) {
    2. for (int j = 1; j <= 3; j++) {
    3. if (i == 2 && j == 2) {
    4. break;
    5. }
    6. System.out.println(i + " " + j);
    7. }
    8. }

    用在 while 循环中的示例:

    1. int i = 1;
    2. while (i <= 10) {
    3. if (i == 5) {
    4. i++;
    5. break;
    6. }
    7. System.out.println(i);
    8. i++;
    9. }

    用在 do-while 循环中的示例:

    1. int j = 1;
    2. do {
    3. if (j == 5) {
    4. j++;
    5. break;
    6. }
    7. System.out.println(j);
    8. j++;
    9. } while (j <= 10);

    用在 switch 语句中的示例:

    1. switch (age) {
    2. case 20 :
    3. System.out.println("上学");
    4. break;
    5. case 24 :
    6. System.out.println("苏州工作");
    7. break;
    8. case 30 :
    9. System.out.println("洛阳工作");
    10. break;
    11. default:
    12. System.out.println("未知");
    13. break; // 可省略
    14. }

    当我们需要在 for 循环或者 (do)while 循环中立即跳转到下一个循环时,就可以使用 continue 关键字,通常用于跳过指定条件下的循环体,如果循环是嵌套的,仅跳过当前循环。

    来个示例:

    1. public class ContinueDemo {
    2. public static void main(String[] args) {
    3. for (int i = 1; i <= 10; i++) {
    4. if (i == 5) {
    5. // 使用 continue 关键字
    6. continue;// 5 将会被跳过
    7. }
    8. System.out.println(i);
    9. }
    10. }
    11. }

    输出:

    1. 1
    2. 2
    3. 3
    4. 4
    5. 6
    6. 7
    7. 8
    8. 9
    9. 10

    “二哥,5 真的被跳过了呀。”

    “那必须滴。不然就是 bug。”

    再来个循环嵌套的例子。

    1. public class ContinueInnerDemo {
    2. public static void main(String[] args) {
    3. for (int i = 1; i <= 3; i++) {
    4. for (int j = 1; j <= 3; j++) {
    5. if (i == 2 && j == 2) {
    6. // 当i=2,j=2时跳过
    7. continue;
    8. }
    9. System.out.println(i + " " + j);
    10. }
    11. }
    12. }
    13. }

    打印出什么玩意呢?

    1. 1 1
    2. 1 2
    3. 1 3
    4. 2 1
    5. 2 3
    6. 3 1
    7. 3 2
    8. 3 3

    “2 2” 没有输出,被跳过了。

    再来看一下 while 循环时 continue 的使用示例:

    1. public class ContinueWhileDemo {
    2. public static void main(String[] args) {
    3. int i = 1;
    4. while (i <= 10) {
    5. if (i == 5) {
    6. i++;
    7. continue;
    8. }
    9. System.out.println(i);
    10. i++;
    11. }
    12. }
    13. }

    输出:

    1. 1
    2. 2
    3. 3
    4. 4
    5. 6
    6. 7
    7. 8
    8. 9
    9. 10

    注意:如果把 if 条件中的“i++”省略掉的话,程序就会进入死循环,一直在 continue。

    最后,再来看一下 do-while 循环时 continue 的使用示例:

    输出:

    1. 1
    2. 2
    3. 3
    4. 4
    5. 6
    6. 7
    7. 8

    注意:同样的,如果把 if 条件中的“i++”省略掉的话,程序就会进入死循环,一直在 continue。


    Java 程序员进阶之路》预计一个月左右会有一次内容更新和完善,大家在我的公众号 沉默王二 后台回复“03” 即可获取最新版!如果觉得内容不错的话,欢迎转发分享!