Kotlin 15.2 Practice Range | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Kotlin 15.2 Practice Range

I get an error "for is not an expression, and only expressions are allowed here". Looked at the solution, tried it again but without effect. Stuck. Help me please :) The code I have used: fun main(args: Array<String>) { var num1 = readLine()!!.toInt() var num2 = readLine()!!.toInt() var sum = 0 for(i in num1..num2){ sum += i } println(sum) }

13th Nov 2022, 11:48 AM
Elias Joostens
Elias Joostens - avatar
4 Answers
+ 5
Put a line break after the for block. println should start in new line. Then it will work. In Kotlin the cost of not using semicolons, is that we have to structure the code more smartly. I'd say it's win-win (for the coder and the compiler).
13th Nov 2022, 2:06 PM
Tibor Santa
Tibor Santa - avatar
+ 3
1. Review the lesson before the test. Make sure you use the correct syntax. 2. Copy or link the code you have written. It would be easier to review, than blindly guessing what you did wrong. Rest assured, that the tests work correctly.
13th Nov 2022, 11:56 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Hi Tibor, off course. Sorry, not used to this. I have updated my post. Can you please check?
13th Nov 2022, 1:39 PM
Elias Joostens
Elias Joostens - avatar
+ 1
@Tibor, thank you very much! :D Did not now this mattered so much, but now do understand the logic behind it. Also this is why no semi-columns are used I suppose. Thanks once again!
14th Nov 2022, 8:22 PM
Elias Joostens
Elias Joostens - avatar