6. [Exercise] Flow Control in Scala
Activity
// Flow control // If / else syntax if (1 > 3) println("Impossible!") else println("The world makes sense.") //> The world makes sense. if (1 > 3) { println("Impossible!") } else { println("The world makes sense.") }// Matching - like switch in other languages: val number = 3 //> number : Int = 3 number match { case 1 => println("One") case 2 => println("Two") case 3 => println("Three") case _ => println("Something else") } //> Three// For loops for (x <- 1 to 4) { val squared = x * x println(squared) } //> 1 //| 4 //| 9 //| 16// While loops var x = 10 //> x : Int = 10 while (x >= 0) { println(x) x -= 1 } //> 10 //| 9 //| 8 //| 7 //| 6 //| 5 //| 4 //| 3 //| 2 //| 1 //| 0// Do While Loop x = 0 //> 0 do { println(x); x+=1 } while (x <= 10) //| 1 //| 2 //| 3 //| 4 //| 5 //| 6 //| 7 //| 8 //| 9 //| 10// Expressions // "Returns" the final value in a block automatically {val x = 10; x + 20} //> res0: Int = 30 println({val x = 10; x + 20}) //> 30
Exercise
Last updated