说明:
An if-else statement decides the execution path based on whether the condition is
true or false.
A one-way if statement performs an action if the specified condition is true. If the condition
is false, nothing is done. But what if you want to take alternative actions when the condition
is false? You can use a two-way if-else statement. The actions that a two-way if-else
statement specifies differ based on whether the condition is true or false.
true or false.
A one-way if statement performs an action if the specified condition is true. If the condition
is false, nothing is done. But what if you want to take alternative actions when the condition
is false? You can use a two-way if-else statement. The actions that a two-way if-else
statement specifies differ based on whether the condition is true or false.
语法格式:
if(boolean-expression) {
statement(s)-for-the-true-case;
}
else{
statement(s)-for-the-false-case;
}
//通常情况下,如果语句块中仅包含一个语句,则相应花括号可以省略
对应流程图:
代码片段举例:
/ ① 判别数字的奇偶性
// 方案:能为2整除的是偶数,反之则为奇数
if (number % 2 == 0)
System.out.println(number + " is even.");
else
System.out.println(number + " is odd.");
A WORD :Death or Alive,is not a must,but a choice.