Java Notes (2) - Conditionals and Control Flow

转载请注明出处:http://blog.csdn.net/cxsydjn/article/details/71513567

The note introduces how to use control flow and conditional statements in Java.

Java notes of open courses @Codecademy.

Boolean Operators

  • &&: and operator

    • It returns a boolean value of true only when the expressions on both sides of && are true.
  • ||: or operator

    • It returns a Boolean value of true when at least one expression on either side of || is true.
  • !: not operator

    • It will return the opposite of the expression immediately after it. It will return false if the expression is true, and true if the expression is false.
  • Boolean Operator Precedence

    • ! > && > ||
    • Every expression within parentheses is evaluated first.
    • Expressions are also read from left to right.

Conditional Expressions

  • if statement

    • In Java, the keyword if is the first part of a conditional expression.
    • It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to true, the block of code that follows will be run.
    • The if statement is not followed by a semicolon (;). Instead it uses curly braces ({ and }) to surround the code block.
  • if/else statement

    • The if/else conditional will run the block of code associated with the if statement if its Boolean expression evaluates to true.
    • Otherwise, if the Boolean expression evaluates to false, it will run the block of code after the else keyword.
  • if/elseif/else statement

    • If the Boolean expression after the if statement evaluates to true, it will run the code block that directly follows.
    • Otherwise, if the Boolean expression after the else if statement evaluates to true, the code block that directly follow will run.
    • Finally, if all previous Boolean expressions evaluate to false, the code within the else block will run.
  • Ternary Conditional Statement

    • It can write if/else statements in a single line of code.
    • It from a Latin word that means “composed of three parts”:
      • A Boolean expression
      • A single statement that gets executed if the Boolean expression is true
      • A single statement that gets executed if the Boolean expression is false
      • E.g., (Boolean expression) ? 'T' : 'F';
  • Switch Statement

    • Java also provides a way to execute code blocks based on whether a block is equal to a specific value.

      int restaurantRating = 3;
      
      switch (restaurantRating) {
      
          case 1: System.out.println("This restaurant is not my favorite.");
            break;
      
          case 2: System.out.println("This restaurant is good.");
            break;
      
          case 3: System.out.println("This restaurant is fantastic!");
            break;
      
          default: System.out.println("I've never dined at this restaurant.");
            break;
      }
    • The break statement will exit the switch statement after a condition is met. Without the break statement, Java will continue to check whether the value of restaurantRating matches any other cases.

    • The default case is printed only if restaurantRating is not equal to an int with the value of 1, 2, or 3.

Review

  • Boolean Operators: &&, ||, and ! are used to build Boolean expressions and have a defined order of operations
  • Statements: if, if/else, and if/else if/else statements are used to conditionally execute blocks of code
  • Ternary Conditional: a shortened version of an if/else statement that returns a value based on the value of a Boolean expression
  • Switch: allows us to check equality of a variable or expression with a value that does not need to be a Boolean

External Resources

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值