day03_ flow control statements

Process Control

Outline

During a program execution, the order of execution of the statement of the program is the result of a direct impact. That is, the flow of the program have a direct impact on operating results. So, we have to understand the flow of execution for each statement. And, many times we want to achieve our functions to be performed by the execution order control statements.

Sequence Structure

According to the order written from top to bottom, in sequence.

Code demonstrates

// sequence structure 
public  class Demo01Sequence {
     public  static  void main (String [] args) {
         // execution order, prepared according to the order, from top to bottom run 
        System.out.println ( "Moonlight" );
        System.out.println ( "suspected ground frost" );
        System.out.println ( "lift eyes to the moon" );
        System.out.println ( "bow their heads and think of home" );
    }
}

Judgment statement

The first form of the if statement: if

format:

Implementation process
  • First, determine the relationship between the expression to see the result is true or false
  • If the statement is true on the implementation of the body
  • If false statement is not executed body

Execution Flow

Single-use code demonstrates the if statement

public class DemoIf {
    //判断2个变量是否相等
    public static void main(String[] args) {
        // 定义两个变量
        int a = 10;
        int b = 20;
        //a == b 是关系表达式
        if (a == b) {
            //语句体
            System.out.println("a等于b");
        }
        int c = 10;
        //a == c 是关系表达式
        if (a == c) {
            //语句体
            System.out.println("a等于c");//a等于c
        }
        System.out.println("其他代码");//其他代码

    }
}
  • if语句第二种形式: if...else
格式:
执行流程
  • 首先判断关系表达式看其结果是true还是false
  • 如果是true就执行语句体1
  • 如果是false就执行语句体2

执行流程图

代码演示if.. else语句的使用

// 标准的if-else语句, 判断给定的数据是奇数还是偶数
public class Demo03IfElse {
    public static void main(String[] args) {
        int num = 666;
        // 如果除以2能够余数为0,说明是偶数
        if (num % 2 == 0) {
            //语句体1
            System.out.println("偶数");//偶数
        } else {
            //语句体2
            System.out.println("奇数");
        }
        System.out.println("其他代码");//其他代码
    }
}
  • if语句第三种形式: if...else if ...else 
格式:
执行流程
  • 首先判断关系表达式1看其结果是true还是false
  • 如果是true就执行语句体1
  • 如果是false就继续判断关系表达式2看其结果是true还是false
  • 如果是true就执行语句体2
  • 如果是false就继续判断关系表达式…看其结果是true还是false
  • 如果没有任何关系表达式为true,就执行语句体n+1。

执行流程图

代码演示其基本使用

/*
指定考试成绩,判断学生等级
90-100 优秀
80-89 好70-79 良
60-69 及格
60以下 不及格

 */
public class Demo05IfElsePractise {
    public static void main(String[] args) {
        int score = 120;
        if (score >= 90 && score <= 100) {
            System.out.println("优秀");
        } else if (score >= 80 && score < 90) {
            System.out.println("好");
        } else if (score >= 70 && score < 80) {
            System.out.println("良");
        } else if (score >= 60 && score < 70) {
            System.out.println("及格");
        } else if (score >= 0 && score < 60) {
            System.out.println("不及格");
        } else { // 单独处理边界之外的不合理情况
            System.out.println("数据错误");//数据错误
        }
    }
}

if语句和三元运算符的互换

在某些简单的应用中,if语句是可以和三元运算符互换使用的。 
// 题目:使用三元运算符和标准的if-else语句分别实现:取两个数字当中的最大值
public class Demo06Max {
    public static void main(String[] args) {
        int a = 105;
        int b = 20;

        /*
        首先使用三元运算符
        int max = a > b ? a : b;
         */
        // 使用今天的if语句
        int max;
        if (a > b) {
            max = a;
        } else {
            max = b;
        }

        System.out.println("最大值:" + max);//最大值:105

    }
}

选择语句 

选择语句switch 

格式: 

执行流程
  • 首先计算出表达式的值
  • 其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束。
  • 最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。 

执行流程图

代码演示,基本使用

public class Demo07Switch {
    public static void main(String[] args) {
        //定义变量,判断是星期几
        int weekday = 1;
        //switch语句实现选择
        switch (weekday) {
            case 1:
                System.out.println("星期一");//星期一
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("数据不合理");
                break; // 最后一个break语句可以省略,但是强烈推荐不要省略
        }
    }
}

switch语句使用的注意事项:

  • 多个case后面的数值不可以重复。
  • switch后面小括号当中只能是下列数据类型:基本数据类型:byte/short/char/int 引用数据类型:String字符串、enum枚举
  •  switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句还可以省略。“匹配哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”case的穿透性 :在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。 

循环语句

循环概述

循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将一直执行下去,形成死循环。

循环语句for

格式:

执行流程
  • 执行顺序:①②③④>②③④>②③④…②不满足为止。
  • ①负责完成循环变量初始化,在循环开始最初执行,而且只做唯一一次。
  • ②负责判断是否满足循环条件,不满足则跳出循环
  • ③具体执行的语句
  • ④循环后,循环条件所涉及变量的变化情况 ,每次循环之后都要进行的扫尾工作,每次循环结束之后都要执行一次。

 执行流程图

代码演示,基本使用

//循环练习:使用for循环,计算1-100之间的偶数和
public class DemoFor {
    public static void main(String[] args) {
        //1.定义一个初始化变量,记录累加求和,初始值为0
        int sum = 0;
        //2.利用for循环获取1‐100之间的数字
        for (int i = 1; i <= 100; i++) {
            //3.判断获取的数组是奇数还是偶数
            if (i % 2 == 0) {
                //4.如果是偶数就累加求和
                sum += i;
            }
        }
        //5.循环结束之后,打印累加结果
        System.out.println("sum:" + sum);//sum:2550
    }
}

while循环

格式

执行流程
  • 执行顺序:①②③④>②③④>②③④…②不满足为止。
  • ①负责完成循环变量初始化。
  • ②负责判断是否满足循环条件,不满足则跳出循环。
  • ③具体执行的语句。
  • ④循环后,循环变量的变化情况。

执行流程图

代码演示,基本使用

//循环练习:使用while循环,计算1-100之间的偶数和
public class DemoWhile {
    public static void main(String[] args) {
        //定义一个初始化变量,记录累加求和,初始值为0
        int sum = 0;
        //定义初始化表达式
        int i = 1;
        //使用while循环让初始化表达式的值变化
        while (i < 101) {
            if (i % 2 == 0) {
                //累加求和
                sum += i;
            }
            //步进表达式改变变量的值
            i++;
        }
        //打印求和的变量
        System.out.println("1‐100的偶数和是:" + sum);//1‐100的偶数和是:2550
    }
}

do...while循环

格式 
执行流程
  • 执行顺序:①③④>②③④>②③④…②不满足为止。
  • ①负责完成循环变量初始化。
  • ②负责判断是否满足循环条件,不满足则跳出循环。
  • ③具体执行的语句
  • ④循环后,循环变量的变化情况 

执行流程图

 代码演示,基本使用

/*
do-while循环的标准格式:

do {
    循环体
} while (条件判断);

扩展格式:

初始化语句
do {
    循环体
    步进语句
} while (条件判断);
*/
//循环练习:使用do..while循环,计算1-100之间的偶数和
public class Demo11DoWhile {
    public static void main(String[] args) {
        //定义一个初始化变量,记录累加求和,初始值为0
        int sum = 0;
        // 初始化语句
        int i = 0;
        do {
            //判断获取的数组是奇数还是偶数
            if (i % 2 == 0) {
                //如果是偶数就累加求和
                sum += i;
            }
            i++; // 4. 步进语句
        } while (i <= 100);  // 2. 条件判断
        //打印求和的变量
        System.out.println("1‐100的偶数和是:" + sum);//1‐100的偶数和是:2550
    }
}

三种循环的区别

  • 如果条件判断从来没有满足过,那么for循环和while循环将会执行0次,但是do-while循环会执行至少一次。
  • for循环的变量在小括号当中定义,只有循环内部才可以使用。while循环和do-while循环初始化语句本来就在外面,所以出来循环之后还可以继续使用。
public class LoopDifference {
    public static void main(String[] args) {
        for (int i = 1; i < 0; i++) {
            System.out.println("Hello");
        }
        // System.out.println(i);  这一行是错误写法!因为变量i定义在for循环小括号内,只有for循环自己才能用。
    
    
        int i = 1;
        do {
            System.out.println("World");
            i++;
        } while (i < 0);
        // 现在已经超出了do-while循环的范围,我们仍然可以使用变量i
        System.out.println(i); // 2
    }
}

break关键字

用法有常见的两种:

  • 用在switch语句当中,一旦执行,整个switch语句立刻结束。
  • 用在循环语句当中,一旦执行,整个循环语句立刻结束。打断循环。

关于循环的选择,有一个小建议:

  • 凡是次数确定的场景多用for循环;否则多用while循环。

代码演示

public class DemoBreak {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            // 如果希望从第4次开始,后续全都不要了,就要打断循环
            if (i == 4) { // 如果当前是第4次
                break; // 那么就打断整个循环
            }
            System.out.println("Hello" + i);
        }
    }
}

continue关键字

常见用法

  • 用在循环语句当中一旦执行,立刻跳过当前次循环剩余内容,马上开始下一次循环。

代码演示

public class DemoContinue {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 4) { // 如果当前是第4层
                continue; // 那么跳过当前次循环,马上开始下一次(第5层)
            }
            System.out.println(i + "层到了。");
        }
    }
}

死循环

死循环:也就是循环中的条件永远为true,死循环的是永不结束的循环在后期的开发中,会出现使用死循环的场景,例如:我们需要读取用户输入的输入,但是用户输入多少数据我们并不清楚,也只能使用死循环,当用户不想输入数据了,就可以结束循环了,如何去结束一个死循环呢,就需要使用到跳出语句了。 

代码演示

/*
永远停不下来的循环,叫做死循环。

死循环的标准格式:
while (true) {
    循环体
}
*/
public class DemoDeadLoop {
    public static void main(String[] args) {
        while (true) {
            System.out.println("I Love Java!");
        }
        
        // System.out.println("Hello"); 错误
    }
}

嵌套循环

所谓嵌套循环,是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。总共的循环次数=外循环次数*内循环次数

嵌套循环格式: 

嵌套循环执行流程:
  • 执行顺序:①②③④⑤⑥>④⑤⑥>⑦②③④⑤⑥>④⑤⑥
  • 外循环一次,内循环多次。
  • 比如跳绳:一共跳5组,每组跳10个。5组就是外循环,10个就是内循环。
练习:使用嵌套循环,打印5*8的矩形 

代码实现

public class Test {
    public static void main(String[] args) {
        //5*8的矩形,打印5行*号,每行8个
        // 外循环5次,内循环8次
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 8; j++) {
                //不换行打印星号
                System.out.print("*");
            }//内循环打印8个星号后,需要一次换行
            System.out.println();
        }
    }
}

 

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/12156923.html