您的位置:首页 > 其它

双路选择: if-else 语句

2016-03-24 13:47 661 查看

说明:

  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;

      }


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


对应流程图:



代码片段举例:

/ ① 判别数字的奇偶性
// 方案:能为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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: