首頁  >  文章  >  Java  >  控制流程:掌握條件語句和循環

控制流程:掌握條件語句和循環

王林
王林原創
2024-08-21 06:07:321107瀏覽

Control Flow: Mastering Conditional Statements and Loops

本指南涵蓋 if-else、for 循環、while 循環等。

在程式設計中,控制執行流程對於在程式碼中做出決策和重複操作至關重要。 Java 提供了強大的工具來管理控制流,包括條件語句循環。在這篇文章中,我們將深入探討這些基本概念,探索它們的工作原理以及如何使用它們來建立動態和響應式程式。

1.條件語句

1.1 If-Else 語句

if-else 語句可讓您根據條件是 true 或 false 來執行程式碼區塊。這就像在程式中設定一個檢查點,其中某些程式碼僅在滿足特定條件時才運行。

文法:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

範例:

int marks = 75;

if (marks >= 60) {
    System.out.println("Passed with distinction!");
} else if (marks >= 40) {
    System.out.println("Passed!");
} else {
    System.out.println("Failed.");
}

在此範例中:

  • 如果分數為 60 或更高,「以優異成績通過!」已列印。
  • 如果分數在 40 到 59 之間,則「通過!」已列印。
  • 如果分數低於 40,則「失敗」。已列印。

挑戰1:

寫一個 Java 程序,使用 if-else 語句檢查數字是正數、負數還是零。為每種情況列印一條適當的訊息。

1.2 Switch 語句

switch 語句是另一種基於變數值執行程式碼的方法。當您需要將單一變數與多個可能值進行比較時,它特別有用。

文法:

switch (variable) {
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    // more cases...
    default:
        // Code to execute if none of the cases match
}

範例:

int dayOfWeek = 3;
String day;

switch (dayOfWeek) {
    case 1:
        day = "Sunday";
        break;
    case 2:
        day = "Monday";
        break;
    case 3:
        day = "Tuesday";
        break;
    // more cases...
    default:
        day = "Invalid day";
        break;
}

System.out.println("Today is: " + day);

2.循環

循環是程式設計中強大的工具,可讓您多次重複一段程式碼。 Java 支援多種類型的循環,每種類型適合不同的場景。

2.1 For 迴圈

for 迴圈通常在您預先知道需要迭代多少次時使用。它由三個部分組成:初始化、條件和迭代。

文法:

for (initialization; condition; iteration) {
    // Code to execute in each loop iteration
}

範例:

for (int i = 1; i <= 5; i++) {
    System.out.println("Iteration: " + i);
}

在此循環中:

  • 整數 i = 1;初始化迴圈計數器 i。
  • i++ 在每次迭代後將 i 加 1。

挑戰2:

建立一個 for 迴圈來列印前 10 個偶數。

2.2 While 迴圈

只要指定條件為真,while 迴圈就會繼續執行。當事先不知道迭代次數時,通常會使用它。

文法:

while (condition) {
    // Code to execute while the condition is true
}

範例:

int count = 0;

while (count < 3) {
    System.out.println("Count: " + count);
    count++;
}

在此範例中,循環列印 count 的值並遞增它,直到 count 不再小於 3。

2.3 Do-While 迴圈

do-while 迴圈與 while 迴圈類似,但它保證迴圈體至少執行一次,即使條件從一開始就是 false。

文法:

do {
    // Code to execute at least once
} while (condition);

範例:

int count = 0;

do {
    System.out.println("Count: " + count);
    count++;
} while (count < 3);

在這種情況下,循環會列印 count 的值並遞增它,就像 while 循環一樣,但它確保程式碼至少運行一次,即使 count 從 3 或更高開始。

2.4 Break 和Continue 語句

  • break: 立即退出循環,跳過任何剩餘的迭代。
  • 繼續: 跳過當前迭代並跳到下一個迭代。

使用中斷的範例:

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is 5
    }
    System.out.println("Value of i: " + i);
}

使用繼續的範例:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // Skip the iteration when i is 3
    }
    System.out.println("Value of i: " + i);
}

挑戰3:

寫一個循環,列印從 1 到 10 的數字,但跳過數字 5。

總結

在本節中,我們介紹了使用條件語句和循環控制 Java 程式流程的要點。我們探索了 if-else、switch、for、while 和 do-while 循環,以及 break 和 continue 語句。

透過掌握這些控制流程工具,您可以建立更動態、更有效率的Java程式。嘗試挑戰來鞏固您所學到的知識!

在下一篇文章中,我們將探討 Java 中的陣列和集合,它們是有效管理資料組的關鍵。請繼續關注!

以上是控制流程:掌握條件語句和循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn