Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun main() {
val score = 95
when {
score >= 50 -> println("pass")
score >= 90 -> println("great")
else -> println("fail")
}
}pass
- Expecting
greatfor a score of95:whendoes not look for the best match; it runs the first branch whose condition is true, so the order of branches matters. - Expecting every true branch to run, one after another:
whenruns exactly one branch, sogreatis never printed here.
- How do you reorder the branches so that a score of
95printsgreat? - What does the program print when
scoreis30?