java基礎之 控制語句

java基礎之控制語句

在java中控制語句包括:java

  1. if-else語句
  2. swicth-case 語句
  3. while循環
  4. do-while循環
  5. for循環
  6. break語句
  7. continue語句

其中:if-else、swicth-case 屬於條件結構,while、do-while、for屬於循環結構,break和continue可用在swicth-case和循環結構中。數據庫

1、if-else語句

if-else主要用於判斷條件,根據不一樣的條件執行不一樣的代碼。express

在java中有各類類型的if-else語句,大體分爲:數組

  • if 語句
  • if-else 語句
  • 嵌套 if 語句
  • if-else-if 語句

語法併發

if (boolean-expression) {
    // boolean-expression 爲 true 時執行
} else {
    // boolean-expression 爲 false 時執行
}

舉個栗子高併發

package test;
public class IfElse {
    public static void main(String[] args) {
        IfElse ifElse = new IfElse();
        ifElse.isShow(true);
    }
    public void isShow(Boolean bool) {
        if (bool) {
            System.out.println("斯國一斯國一");
        } else {
            System.out.println("笨蛋笨蛋");
        }
    }
}
// 結果:斯國一斯國一

擴展測試

if 語句、嵌套 if 語句、if-else-if 語句,這些語句都是 if-else 語句 的變種,和它的使用方法一致,編碼

舉個栗子:code

package test;

public class IfElse {
    public static void main(String[] args) {
        IfElse ifElse = new IfElse();
        ifElse.ifelse(1000);
    }
    public void ifelse(int num) {
        if (num < 0) {
            System.out.print("這個是代把的數字");
        } else if (num == 0) {
            System.out.print("這個是圈");
        } else {
            System.out.print("這個數字好大啊");
            if (num > 999) {
                System.out.print("是我一個的存款【作夢ing】");
            }
        }
    }
}
// 結果:這個數字好大啊是我一個的存款【作夢ing】

2、swicth-case 語句

根據表達式的值,swicth語句從一系列case中選出一個去執行。對象

語法

switch (expression) {
    case value1 : statement1; break;
    case value2 : statement2; break;
    case value3 : statement3; break;
    default: statement;
}

規則:

  1. expression是選擇因子,類型能夠爲: byte、short、int 、char 、String(1.7+)。
  2. case 緊跟的value是值,其值不能是變量。
  3. 當expression和value匹配時,進入對應的statement語句塊。
  4. break是非必須的,若是不寫,將會執行下一個case,直到遇到break爲止。
  5. switch中能夠包含一個default語句,若是expression不符合任何的case條件,將會執行default語句後面的語句塊。

舉個栗子

package test;
public class SwitchCase {
    public static void main(String[] args) {
        SwitchCase switchCase = new SwitchCase();
        switchCase.nameToRgba("red");
    }
    public void nameToRgba(String colorName) {
        switch (colorName) {
            case "blue":
                System.out.println("胖次藍");
                break;
            case "red":
                System.out.println("姨媽紅");
                break;
            case "white":
                System.out.println("純白");
                break;
            default:
                System.out.println("未知顏色");
        }
    }
}
// 結果:姨媽紅

擴展

若是 case 代碼塊中沒有 break 語句時,並不會順序執行每個 case 對應的代碼塊,而是繼續匹配,匹配不成功則返回默認 case。匹配成功將執行代碼塊,若是匹配的case沒有break語句將會順序執行後面的case代碼塊,直到遇到break語句爲止。

舉個栗子

package test;
public class SwitchCase {
    public static void main(String[] args) {
        SwitchCase switchCase = new SwitchCase();
        switchCase.nameToRgba("red");
    }
    public void nameToRgba(String colorName) {
        switch (colorName) {
            case "blue":
                System.out.print("胖次藍 ");
            case "red":
                System.out.print("姨媽紅 ");
            case "white":
                System.out.print("純白 ");
                break;
            case "black":
                System.out.print("黑夜 ");
            default:
                System.out.print("未知顏色");
        }
    }
}
// 結果:姨媽紅 純白

3、while和do-while

while、do-while和for都是用來控制循環的。用這些語句的代碼塊會重複執行,至到起控制做用的布爾表達式爲「fasle」爲止。

其中while循環的格式以下:

while (expression) {
    statement
}

規則

在循環開始時會先計算一遍expression表達式的值,成立纔會進入statement循環體;在下一次循環開始前會再一次計算,直到計算出的值爲「假」爲止。

舉個栗子

package test;
public class While {
    public static void main(String[] args) {
        While w = new While();
        w.whileTest();
    }
    public void whileTest() {
        int i = 1;
        while (i <= 100) {
            System.out.println(i++);
        }
    }
}
// 結果:輸出 1 到 100

do-while 循環會先執行一次循環體的內容,而後第二次纔會進行條件判斷,do…while 循環和 while 循環類似,不一樣的是,do…while 循環至少會執行一次。語法規則以下:

do{
    statement
} while (expression);

舉個栗子

package test;
public class While {
    public static void main(String[] args) {
        While w = new While();
        w.doWhileTest();
    }
    public void doWhileTest() {
        int i = 0;
        do {
            System.out.println(++i);
        } while (i < 100);
    }
}
// 結果:輸出 1 到 100

4、for循環

for循環多是最經常使用的迭代數據的形式,在第一次迭代時進行初始化,而後進行條件測試,在每次條件結束時進行某種形式的「進步」。for循環的格式以下:

for (init; expression; setp) {
    statement;
}

規則

for循環經常使用執行「計數」任務,在執行前就肯定執行次數。

init、expression、setp均可覺得空,每次迭代都會判斷expression是否爲false,若是是則執行statement代碼,在執行完成時會執行一次setp,若是判斷expression是否爲true時會結束迭代。

舉個栗子

package test;
public class ForTest {
    public static void main(String[] args) {
        ForTest forTest = new ForTest();
        forTest.print99();
    }
    //打印 9*9 乘法表
    public void print99() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}
// 結果:
// 1x1=1    
// 1x2=2    2x2=4   
// 1x3=3    2x3=6   3x3=9   
// 1x4=4    2x4=8   3x4=12  4x4=16  
// 1x5=5    2x5=10  3x5=15  4x5=20  5x5=25  
// 1x6=6    2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
// 1x7=7    2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
// 1x8=8    2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
// 1x9=9    2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81

擴展

在for循環的init和setp可使用由逗號隔開的語句,這些語句會獨立執行。經過使用逗號操做符,能夠在for循環內定義多個變量,但它們必須是同一種類型。

舉個栗子

package test;
public class ForTest {
    public static void main(String[] args) {
        ForTest forTest = new ForTest();
        forTest.print66();
    }
    /**
     * 99乘法表向上對其
     */
    public void print66() {
        for (int i = 9; i > 0; i--) {
            for (int j = 1, k = 10 - i; j <= i; j++, k++) {
                System.out.print(j + "x" + k + "=" + (j * k) + "\t");
            }
            System.out.println();
        }
    }
}
// 結果:
// 1x1=1    2x2=4   3x3=9   4x4=16  5x5=25  6x6=36  7x7=49  8x8=64  9x9=81  
// 1x2=2    2x3=6   3x4=12  4x5=20  5x6=30  6x7=42  7x8=56  8x9=72  
// 1x3=3    2x4=8   3x5=15  4x6=24  5x7=35  6x8=48  7x9=63  
// 1x4=4    2x5=10  3x6=18  4x7=28  5x8=40  6x9=54  
// 1x5=5    2x6=12  3x7=21  4x8=32  5x9=45  
// 1x6=6    2x7=14  3x8=24  4x9=36  
// 1x7=7    2x8=16  3x9=27  
// 1x8=8    2x9=18  
// 1x9=9

加強for循環

加強for循環用於快速的迭代數組和容器,其格式以下:

for (variable : expression) {
    statement;
}

規則

variable循環內的局部變量,expression爲數組、容器或者返回數組容器的表達式,在每次迭代時,可用variable這個局部變量來取出expression迭代內的項。

舉個栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.specialPrint();
    }
    /**
     * 每隔500毫秒打印一個字符
     */
    public void specialPrint() throws InterruptedException {
        String content = "每隔500毫秒打印一個字符。";
        for (Character str : content.toCharArray()) {
            Thread.sleep(500);
            System.out.print(str + "\t");
        }
    }
}
// 結果:每 隔   5   0   0   毫   秒   打   印   一   個   字   符   。

5、break 語句

在任何迭代語句的主體部分,均可以使用break語句來進行控制循環的流程。break語句用於強制退出循環,不執行循環中剩餘的語句。語法格式以下:

break;

舉個栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.printFor();
    }
    /**
     * 在無窮for循環中退出
     */
    public void printFor() {
        int i = 0;
        for (; ; ) {
            i++;
            System.out.print(i + " ");
            // 循環10次後退出
            if (i >= 10) {
                break;
            }
        }
    }
}
// 結果:1 2 3 4 5 6 7 8 9 10

6、continue語句

break語句用於強制結束本次循環,開始下一次循環。語法格式以下:

continue;

舉個栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.print10();
    }
    public void print10() {
        for (int i = 0; i < 10; i++) {
            // 被2整除的數不打印
            if (i % 2 == 0) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}
// 結果:1 3 5 7 9

編碼規範

根據阿里巴巴java開發手冊說明:

【強制】在 if / esle / for / while / do 語句中,應該使用大括號 「{}」,避免使用單行編碼。

【強制】在高併發場景中,避免使用「等於」做爲中斷或退出條件,防止條件被穿刺。

【強制】在使用 if else 時,爲防止之後維護困難,不要超過3層。

【強制】在一個switch塊內 ,每個case要麼經過break/return 等來終止,要麼說明程序將繼續執行到那個case爲止;在一個switch塊內,必須包含一個default語句而且放在最後,即便它是空的。

【強制】在循環體內避免進行定義變量、對象、獲取數據庫鏈接、進行try-catch等操做。

【推薦】不要在條件判斷中執行復雜的判斷語句,可將複雜的邏輯判斷的結果賦予一個有意義的布爾變量,以提升可讀性。

【推薦】避免使用反邏輯運算符,不利於快速理解。

相關文章
相關標籤/搜索