Back to questions
JuniorTheoryCommon
What does the program print?
fun main() {
var sum = 0
for (i in 1 until 4) {
sum += i
}
for (i in 1..4) {
sum += i
}
println(sum)
}16
- Expecting
20: readinguntilas "up to and including".1 until 4stops before4, so the first loop adds1 + 2 + 3, which is6. - Expecting
12: thinking..leaves out its end too.1..4includes4, so the second loop adds10and the total is16.
- What does the program print if both loops go over
1..<4? - How many times does the body of
for (i in 4 until 4)run?