@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】【码云】