关于java:java基础04java流程控制语句

8次阅读

共计 2522 个字符,预计需要花费 7 分钟才能阅读完成。

@TOC

流程管制次要分为程序构造,分支构造和循环构造。在整个程序当中,流程构造起到十分大的用途。

1. if 语句

语法 1:

if (关系表达式) {语句体;}

判断关系表达式,为 true 执行语句体否则持续向下执行。

示例:

public class IfTest {public static void main(String[] args) {
        int a = 1, b = 2;
        if (a < b) {System.out.println("条件为 true");
        }
    }
}

语法 2:

if (关系表达式) {语句体 1;} else {语句体 2;}

示例:

public class IfTest2 {public static void main(String[] args) {
        int a = 1, b = 2;
        if (a > b) {System.out.println("条件为 true");
        } else {System.out.println("条件为 false");
        }
    }
}

语法 3:

if (关系表达式 1) {语句体 1;} else if (关系表达式 2) {语句体 2;} else {语句体 3;}

示例:

public class IfTest3 {public static void main(String[] args) {
        int a = 1, b = 2;
        if (a > b) {System.out.println("条件 1 为 true");
        } else if (b == 2) {System.out.println("条件 2 为 true");
        } else {System.out.println("条件都不成立");
        }
    }
}

语法 4:

if (关系表达式 1)
    语句体 1;    

这种语法不举荐应用,在大批量的程序中呈现很难保护,举荐应用三元运算符,看我上一篇文章。
示例:

public class IfTest4 {public static void main(String[] args) {
        int a = 1, b = 2;
        if (a < b)
            System.out.println("条件为 true");
    }
}

2. switch 语句

格局:

switch (表达式) {
    case 1:
        语句体 1;
    case 2:
        语句体 2;
    default:
        语句体 3;
}

执行程序:
先执行表达式计算出值
与 1 进行比照看是否成立,成立执行语句体 1
而后与 2 进行比照,成立执行语句体 2

都不成立执行语句体 3

示例:

public class SwitchTest {public static void main(String[] args) {
        int a = 1;
        switch (a) {
            case 1:
                System.out.println("1 选项");
            case 2:
                System.out.println("2 选项");
            default:
                System.out.println("默认选项");
        }
    }
}

以上不是长应用的,会在每个语句体后加 break;,否则前面语句体都会被执行。
失常示例会是这样,示例:

public class SwitchTest {public static void main(String[] args) {
        int a = 2;
        switch (a) {
            case 1:
                System.out.println("1 选项");
                break;
            case 2:
                System.out.println("2 选项");
                break;
            default:
                System.out.println("默认选项");
        }
    }
}

3. for 循环语句

语法:

for (初始化语句; 条件判断语句; 条件管制语句) {语句体;}

执行程序:

  1. 执行初始化语句
  2. 执行条件判断语句,条件满足执行 3,不满足循环完结
  3. 执行语句体
  4. 执行条件管制语句
  5. 执行地 2 步

示例:

public class ForTest {public static void main(String[] args) {for (int i = 0; i < 10; i++) {System.out.println(i + 1);
        }
    }
}

4. while 和 do-while 循环语句

4.1 while 循环语句

语法:

while (关系表达式) {语句体;}

执行程序:

  1. 关系表达式为 true 执行 2,否则跳出 while 循环体向下执行
  2. 执行语句体,而后再执行 1

示例:

public class WhileTest {public static void main(String[] args) {
        int x = 1;
        while (x <= 10) {System.out.println("x:" + x);
            x++;
        }
    }
}

4.2 do-while 循环语句

语法:

do {语句体;} while (关系表达式);

执行程序:

  1. 执行语句体
  2. 执行关系表达式,为 true 执行语句体,否则跳出循环体

示例:

public class DoWhileTest {public static void main(String[] args) {
        int x = 1;
        do {System.out.println("x:" + x);
            x++;
        } while (x < 10);
    }
}

5. 关键字 break 和 continue

循环语句实际上是一个危险语句,如果不加以控制就会导致机器呈现死机奔溃问题。所以跳出循环是十分重要的,除了关系表达式 break 和 continue 也能够跳出循环。

一旦执行 break 不管关系表达式是否满足条件,都会间接跳出循环,执行循环前面的语句。
break 示例:

public class BreakTest {public static void main(String[] args) {
        int x = 1;
        do {if (x == 6) {System.out.println("循环");
                break;
            }
            System.out.println("x:" + x);
            x++;
        } while (x < 10);
    }
}

一旦执行 continue,continue 之后的语句体不再执行,而是取执行关系表达式,true 继续执行语句体,false 跳出循环体。

continue 示例:

public class ContinueTest {public static void main(String[] args) {
        int x = 1;
        while (x < 10) {switch (x) {
                case 4:
                case 5:
                case 6:
                    x++;
                    continue;
                default:
            }
            System.out.println("x:" + x);
            x++;
        }
    }
}

本章完结,用于集体学习和小白入门,大佬勿喷!

源码【GitHub】【码云】

正文完
 0