Pages

Monday, 28 March 2022

Scala IF expression ( All Possible ways to use IF condition )


IF expression in single line :

if (expression) println("something")

Example
scala> if ("print" == "print") print("True")
True
 
OR
scala> if ("print" == "print") { print("True") }
True 


IF expression in multiple lines (multiline statement)

if (expression) {
       println("something1")
       println("something1")
   }
 
Example
scala> if ("print" == "print") {
     | println("True")
     | println("True")
     | }
True
True 


Assign value to variable using IF condition (expression)

Example

scala> val day = 1

day: Int = 1

scala> val curr_day = if (day == 1) { "Monday" } else {"Not A Monday"}

curr_day: String = Monday



No comments: