Scala If Else Expression with Example Code

Scala If Else control structure has used syntax and usage as Java if else. If Else in Scala is used as an expression rather than a control structure because if..else in Scala returns a value. Here I have tried to give you more details and code examples of Scala If Else Expression.

Scala If Else Expression
Scala If Else Expression

Expression Syntax

In above syntax,

  • We can use another if..else or any Scala expression in place of “expression”
  • Curly brackets can be omitted in if..else expression

Scala REPL Example: Finding Maximum number

Scala REPL Example: Finding absolute number

If else part in if..else expression is not specified than this expression will return “Any” as a return type.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *