双路选择: if-else 语句

来源:互联网 发布:php面试题大全 编辑:程序博客网 时间:2024/06/13 12:37

说明:

   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.

语法格式:

     if(boolean-expression) {
        statement(s)-for-the-true-case;
      }
     else{
        statement(s)-for-the-false-case;
      }

     //通常情况下,如果语句块中仅包含一个语句,则相应花括号可以省略

对应流程图:

flowchart of if-else statement

代码片段举例:

// ① 判别数字的奇偶性
// 方案:能为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.

0 0
原创粉丝点击