Home » Scala Conditional Expressions

Scala Conditional Expressions

by Online Tutorials Library

Scala Conditional Expressions

Scala provides if statement to test the conditional expressions. It tests boolean conditional expression which can be either true or false. Scala use various types of if else statements.

  • If statement
  • If-else statement
  • Nested if-else statement
  • If-else-if ladder statement

Scala if statement

The scala if statement is used to test condition in scala. If block executes only when condition is true otherwise execution of if block is skipped.

Syntax

Flowchart

Scala If statement 1


Scala Example: If Statement

Output:

Age is greate than 18 

Scala If-Else Statement

The scala if-else statement tests the condition. If the condition is true, if block executes otherwise else block executes.

Syntax

Flowchart

Scala If statement 2


Scala if-else example

Output:

Odd number 

Scala If-Else-If Ladder Statement

The scala if-else-if ladder executes one condition among the multiple conditional statements.

Syntax

Flowchart

Scala If statement 3


Scala If-Else-If Ladder Example

Output:

A Grade 

Scala If Statement as better alternative of Ternary Operators

In scala, you can assign if statement result to a function. Scala does not have ternary operator concept like C/C++ but provides more powerful if which can return value. Let’s see an example

Example

Output:

-1 

You may also like