【從零開始學 Java 程式設計】if 和 switch 條件控制差異比較

【從零開始學 Java 程式設計】 線上教學課程目錄,使用 Java 程式語言,開發應用程式。

if…else… 和 switch 多條件判斷差異

if 可以處理範圍資料的判斷,例如:
if(intVal >= 0 && intVal <= 100){
  //todo something ...
}
而 switch 只能處理 case 常數值判斷。另外 switch 根據資料值,選擇程式分支,只做一次運算,但 if…else… 並需每個都判斷,也因為這樣 switch 處理效率較高。

補充資料:運行時間

通常我們在比較誰的處理較快,我們都會使用時間差(=結束時間-開始時間),來計算誰處理的時間較短,最短的人,處理速度較快,反之時間差越大,處理較慢。而更嚴僅的情況下,可以連續執行 100、1000 或10,000次,平均下來的時間進行比較。

時間差

//記錄開始時間
long startTime = System.nanoTime();

/*
...
程式運行區塊 
...
 */

//記錄結束時間
long endTime = System.nanoTime();

//印出時間差= 結束時間 - 開始時間
System.out.println(endTime - startTime);

if…else…

 //記錄開始時間
long startTime = System.nanoTime();

String strVal = "999";
if ("1".equals(strVal)) {
    System.out.println(strVal);
} else if ("2".equals(strVal)) {
    System.out.println(strVal);
} else if ("3".equals(strVal)) {
    System.out.println(strVal);
} else if ("4".equals(strVal)) {
    System.out.println(strVal);
} else if ("5".equals(strVal)) {
    System.out.println(strVal);
} else if ("6".equals(strVal)) {
    System.out.println(strVal);
} else if ("7".equals(strVal)) {
    System.out.println(strVal);
} else if ("8".equals(strVal)) {
    System.out.println(strVal);
} else if ("9".equals(strVal)) {
    System.out.println(strVal);
} else if ("10".equals(strVal)) {
    System.out.println(strVal);
} else if ("11".equals(strVal)) {
    System.out.println(strVal);
} else if ("12".equals(strVal)) {
    System.out.println(strVal);
} else if ("13".equals(strVal)) {
    System.out.println(strVal);
} else if ("14".equals(strVal)) {
    System.out.println(strVal);
} else if ("15".equals(strVal)) {
    System.out.println(strVal);
} else {
    System.out.println(strVal);
}
//記錄結束時間
long endTime = System.nanoTime();

//印出時間差= 結束時間 - 開始時間
System.out.println(endTime - startTime);

switch

 long startTime = System.nanoTime();
String strVal="999";
switch (strVal) {
    case "1":
        System.out.println(strVal);
        break;
    case "2":
        System.out.println(strVal);
        break;
    case "3":
        System.out.println(strVal);
        break;
    case "4":
        System.out.println(strVal);
        break;
    case "5":
        System.out.println(strVal);
        break;
    case "6":
        System.out.println(strVal);
        break;
    case "7":
        System.out.println(strVal);
        break;
    case "8":
        System.out.println(strVal);
        break;
    case "9":
        System.out.println(strVal);
        break;
    case "10":
        System.out.println(strVal);
        break;
    case "11":
        System.out.println(strVal);
        break;
    case "12":
        System.out.println(strVal);
        break;
    case "13":
        System.out.println(strVal);
        break;
    case "14":
        System.out.println(strVal);
        break;
    default:
        System.out.println(strVal);
        break;
}
//記錄結束時間
long endTime = System.nanoTime();

//印出時間差= 結束時間 - 開始時間
System.out.println(endTime - startTime);

補充資料:逐步偵錯

我們可以在程式碼左側點擊,下程式碼中斷點
選擇 Debug 除錯模式運行,當程式走到此處則會被中斷點,中斷而停止,這時你可以選擇「逐步偵錯」,即可以看到程式運作邏輯。
可以試著練習寫一段 if…else… 和 switch 程式碼,即可以知道 if…else… 是每個判斷都要進行判斷,而 switch 只計算一次。

那這次的課程就介紹到這邊囉~
順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這套課程分享給更多人喔。
我們下次再見囉!!!掰掰~

這個網誌中的熱門文章

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

nano 文字編輯器

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)

16天記下7000單字

最新入門零基礎 Java 教學【從零開始學 Java 程式設計】Java教學課程目錄 (IntelliJ IDEA 開發教學)