NO.A.0008——day03——流程控制語句/if、if else、switch、for

流程控制語句:

            在一個程序執行過程中,各條件語句的執行順序的結果會有直接影響的。

一、順序結構
代碼庫:Demo01Sequence.java

// 順序結構
public class Demo01Sequence {
    public static void main(String[] args) {
        System.out.println("今天天氣不錯");
        System.out.println("挺風和日麗的");
        System.out.println("我們下午沒課");
        System.out.println("這的確挺爽的");
    }
}

流程圖
在這裏插入圖片描述

順序結構流程圖

二、判斷語句
2.1、if語句第一種格式:

/*
首先判斷關係表達式看其結果是true還是false
如果是true就執行語句體
如果是false就不執行語句體

if(關係表達式){
        語句體;
}

單if語句流程圖:
在這裏插入圖片描述

代碼庫:Demo02If.java

// 單if語句
public class Demo02If {
    public static void main(String[] args) {
        System.out.println("今天天氣不錯,正在壓馬路……突然發現一個快樂的地方:網吧");
        int age = 19;
        if (age >= 18) {
            System.out.println("進入網吧,開始high!");
            System.out.println("遇到了一羣豬隊友,開始罵街。");
            System.out.println("感覺不爽,結賬走人。");
        }
        System.out.println("回家喫飯");
    }
}

2.2、判斷if..else語句
if語句的第二種格式:if......else

/*
首先判斷關係表達式看其結果是true還是false
如果是true就執行與具體1
如果是false 就執行與具體2
*/

if (關係表達式);{
    語句體1;
}else{
    語句體2;
}

在這裏插入圖片描述

if....else語句流程圖

代碼庫:Demo03IfElse.java

// 標準的if-else語句
public class Demo03IfElse {
    public static void main(String[] args) {
        int num = 666;

        if (num % 2 == 0) { // 如果除以2能夠餘數爲0,說明是偶數
            System.out.println("偶數");
        } else {
            System.out.println("奇數");
        }
    }
}

2.3、判斷語句(複合擴展的if語句)
if語句第三種格式:if....else if.....else

/*
首先判斷關係表達式1看其結果是true還是false
如果是true就執行語句體1
如果是false就繼續判斷關係表達式2看其結果是true還是false
如果我是true就執行語句體2
如果是false就繼續判斷關係表達式....看其結果是true還是false
....
如果沒有任何表達式爲true,就執行語句體n+1.
*/
if (判斷條件1){
    執行語句1;
    }else if(判斷條件2){
    執行語句2;
}
...
}else if (判斷條件n){
          執行語句n;
          }else{
    執行語句n+1;
}

在這裏插入圖片描述

if...else擴展流程圖
代碼庫:Demo04IfElseExt.java

// x和y的關係滿足如下:
// 如果x >= 3,那麼y = 2x + 1;
// 如果-1 < x < 3,那麼y = 2x;
// 如果x <= -1,那麼y = 2x – 1;
public class Demo04IfElseExt {
    public static void main(String[] args) {
        int x = -10;
        int y;
        if (x >= 3) {
            y = 2 * x + 1;
        } else if (-1 < x && x < 3) {
            y = 2 * x;
        } else {
            y = 2 * x - 1;
        }
        System.out.println("結果是:" + y);
    }
}

案例一:
代碼庫:Demo05IfElsePractise.java

/*
制定考試成績,判斷成績的等級;
    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("數據錯誤");
        }
    }
}

2.4、if語句和三元運算符的互換:
代碼庫:Demo06Max.java

// 題目:使用三元運算符和標準的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);
    }
}

三、switch選擇語句:
3.1、選擇結構_標準的switch語句

// switch 語句格式
/*
首先計算出表達式的值
其次,和case依次比較,一旦有對應的值,就會執行相應的語句,在執行的過程中,入到break就會結束
最後,如果所有的case都和表達式的值不匹配,就會執行default語句體部分,然後程序結束掉。
*/

switch(表達式){
    case 常量值1:
        語句體1;
        break;
    case 常量值2:
        語句體2;
        break;
        ...
        default:
        語句體n+1;
        break;
}

在這裏插入圖片描述

代碼庫:Demo07Switch.java

public class Demo07Switch {
    public static void main(String[] args) {
        int num = 10;

        switch (num) {
            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語句可以省略,但是強烈推薦不要省略
        }
    }
}

3.2、case穿透性:
代碼庫:Demo08SwitchNotice.java

/*
switch語句使用的注意事項:

1. 多個case後面的數值不可以重複。

2. switch後面小括號當中只能是下列數據類型:
基本數據類型:byte/short/char/int
引用數據類型:String字符串、enum枚舉

3. switch語句格式可以很靈活:前後順序可以顛倒,而且break語句還可以省略。
“匹配哪一個case就從哪一個位置向下執行,直到遇到了break或者整體結束爲止。”
*/
public class Demo08SwitchNotice {
    public static void main(String[] args) {
        int num = 2;
        switch (num) {
            case 1:
                System.out.println("你好");
                break;
            case 2:
                System.out.println("我好");
                // break;
            case 3:
                System.out.println("大家好");
                break;
            default:
                System.out.println("他好,我也好。");
                break;
        } // switch
    }
}

四、循環語句、
4.1、for循環
代碼庫:Demo09For.java

/*
循環結構的基本組成部分,一般可以分成四部分:

1. 初始化語句:在循環開始最初執行,而且只做唯一一次。
2. 條件判斷:如果成立,則循環繼續;如果不成立,則循環退出。
3. 循環體:重複要做的事情內容,若干行語句。
4. 步進語句:每次循環之後都要進行的掃尾工作,每次循環結束之後都要執行一次。
*/
public class Demo09For {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            System.out.println("我錯啦!原諒我吧!" + i);
        }
        System.out.println("程序停止");
    }
}
/* for循環語句格式:
for (初始化表達式1;布爾表表達式2,步進表達式3){
    循環體4
}

執行流程:
        執行順序:1234>234>234....2不滿足位置。
        1負責完成循環變量初始化
        2負責判斷滿足循環條件,不滿足則跳出循環
        3具體執行的語句
        4循環後,循環條件所涉及變量的變化情況
*/

在這裏插入圖片描述

for循環初始圖
4.2、while循環
代碼庫:Demo10While.java

/*
while循環有一個標準格式,還有一個擴展格式。

標準格式:
while (條件判斷) {
    循環體
}

擴展格式:

初始化語句;
while (條件判斷) {
    循環體;
    步進語句;
}
*/
public class Demo10While {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println("我錯啦!" + i);
        }
        System.out.println("=================");

        int i = 1; // 1. 初始化語句
        while (i <= 10) { // 2. 條件判斷
            System.out.println("我錯啦!" + i); // 3. 循環體
            i++; // 4. 步進語句
        }
    }
}

在這裏插入圖片描述

while循環標準格式
4.3:do while循環語句:
代碼庫:Demo11DoWhile.java

/*
do-while循環的標準格式:

do {
    循環體
} while (條件判斷);

擴展格式:

初始化語句
do {
    循環體
    步進語句
} while (條件判斷);
*/
public class Demo11DoWhile {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println("原諒你啦!起來吧!地上怪涼!" + i);
        }
        System.out.println("===============");

        int i = 1; // 1. 初始化語句
        do {
            System.out.println("原諒你啦!起來吧!地上怪涼!" + i); // 3. 循環體
            i++; // 4. 步進語句
        } while (i <= 10); // 2. 條件判斷
    }
}

在這裏插入圖片描述

do while循環結構圖

實例一:
代碼庫:Demo12HundredSum.java

/*
題目:求出1-100之間的偶數和。

思路:
1. 既然範圍已經確定了是1到100之間,那麼我就從1、2、3……一直到100這麼多數字一個一個進行檢查。
2. 總共有100個數字,並非所有數字都能用。必須要是偶數才能用,判斷(if語句)偶數:num % 2 == 0
3. 需要一個變量,用來進行累加操作。也就好比是一個存錢罐。
*/
public class Demo12HundredSum {
    public static void main(String[] args) {
        int sum = 0; // 用來累加的存錢罐

        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) { // 如果是偶數
                sum += i;
            }
        }
        System.out.println("結果是:" + sum);
    }
}       //結果是:2550

4.4、do、for、while三種循環的區別
代碼庫:Demo13LoopDifference.java

/*
三種循環的區別。

1. 如果條件判斷從來沒有滿足過,那麼for循環和while循環將會執行0次,但是do-while循環會執行至少一次。
2. for循環的變量在小括號當中定義,只有循環內部纔可以使用。while循環和do-while循環初始化語句本來就
在外面,所以出來循環之後還可以繼續使用。
*/
public class Demo13LoopDifference {
    public static void main(String[] args) {
        for (int i = 1; i < 0; i++) {
            System.out.println("Hello");
        }
        // System.out.println(i); // 這一行是錯誤寫法!因爲變量i定義在for循環小括號內,只有for
        循環自己才能用。
        System.out.println("================");

        int i = 1;
        do {
            System.out.println("World");
            i++;
        } while (i < 0);
        // 現在已經超出了do-while循環的範圍,我們仍然可以使用變量i
        System.out.println(i); // 2
    }
}

4.5、條件控制語句:跳出語句:break continue
break
代碼庫:Demo14Break.java

/*
break關鍵字的用法有常見的兩種:

1. 可以用在switch語句當中,一旦執行,整個switch語句立刻結束。
2. 還可以用在循環語句當中,一旦執行,整個循環語句立刻結束。打斷循環。

關於循環的選擇,有一個小建議:
凡是次數確定的場景多用for循環;否則多用while循環。
*/
public class Demo14Break {
    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一旦執行,立刻跳過當前次循環,馬上開始下一次循環。
代碼庫:Demo15Continue.java

/*
另一種循環控制語句是continue關鍵字。
一旦執行,立刻跳過當前次循環剩餘內容,馬上開始下一次循環。
*/
public class Demo15Continue {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 4) { // 如果當前是第4層
                continue; // 那麼跳過當前次循環,馬上開始下一次(第5層)
            }
            System.out.println(i + "層到了。");
        }
    }
}

五、擴展知識:
5.1、死循環
代碼庫:Demo16DeadLoop.java

/*
永遠停不下來的循環,叫做死循環。

死循環的標準格式:
while (true) {
    循環體
}
*/
public class Demo16DeadLoop {
    public static void main(String[] args) {
        while (true) {
            System.out.println("I Love Java!");
        }

        // System.out.println("Hello");     //Ctrl+c    停止下來
    }
}

5.2、循環的嵌套
代碼庫:Demo17LoopHourAndMinute.java

public class Demo17LoopHourAndMinute {
    public static void main(String[] args) {
        for (int hour = 0; hour < 24; hour++) { // 外層控制小時

            for (int minute = 0; minute < 60; minute++) { // 內層控制小時之內的分鐘
                System.out.println(hour + "點" + minute + "分");
            }

        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章