Scala – IF ELSE Statements




In Scala IF ELSE can be used in two ways, as statements and as expressions.

General form of an if-else statement:

When we use if as statements, we evaluate a Boolean expression and based on the result of the Boolean expression the code executes a statement. In this case the statement does not return anything by default.

General form of an if-else expressions:

When we use if as expression, we evaluate a Boolean expression and based on the result of the Boolean expression the code executes a statement. But in this case the if expression return the result of if expression.

Example:

Logical Operators

    • Scala provides logical operators.
      • The binary logical operators combine two boolean expressions into one.
      • The unary logical operator switches the value of a boolean expression.
      • Binary logical operators have lower precedence than relational operators (they will be evaluated after)
      • NOT has the same precedence as negation.

 

Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary

 

Logical Operator Practice

    • 2 > 3 && 4 < 5
      • false – first operand is false
    • 2 < 3 && 4 < 5
      • true
    • 2 > 3 || 4 < 5
      • true
    • 2 > 3 || 4 > 5
      • false – both operands are false
    • !(2 > 3)
      • true – operand is false

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com