Control Flow
Ordered Guards
Check ordered conditions from general to specific.
Ordered Guards
NestedGuards.scala
object Main {
def main(args: Array[String]): Unit = {
val score =
var result = "retry"
if (score >= 60) {
result = "pass"
}
if (score >= 90) {
result = "honor"
}
println("score=" + score)
println("result=" + result)
}
}
object Main {
def main(args: Array[String]): Unit = {
val score =
var result = "retry"
if (score >= 60) {
result = "pass"
}
if (score >= 90) {
result = "honor"
}
println("score=" + score)
println("result=" + result)
}
}
object Main {
def main(args: Array[String]): Unit = {
val score =
var result = "retry"
if (score >= 60) {
result = "pass"
}
if (score >= 90) {
result = "honor"
}
println("score=" + score)
println("result=" + result)
}
}
ordered-guards
Ordered guards let later checks refine an earlier result when the conditions are simple and explicit.