W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
我們將看到的下一個決策語句是 if / else 語句。這種說法的一般形式是 -
if(condition) { statement #1 statement #2 ... } else{ statement #3 statement #4 }
這個語句的一般工作是首先在 if 語句中評估一個條件。如果條件為真,則其后執(zhí)行語句,并在else條件之前停止并退出循環(huán)。如果條件為假,則執(zhí)行else語句塊中的語句,然后退出循環(huán)。下圖顯示了 if 語句的流程。
下面是一個if / else語句的例子 -
class Example { static void main(String[] args) { // Initializing a local variable int a = 2 //Check for the boolean condition if (a<100) { //If the condition is true print the following statement println("The value is less than 100"); } else { //If the condition is false print the following statement println("The value is greater than 100"); } } }
在上面的例子中,我們首先將一個變量初始化為值2.然后我們評估變量的值,然后決定應該執(zhí)行哪個 println 語句。上面的代碼的輸出將是
The value is less than 100.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: