Why doesn't my code work correctly? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why doesn't my code work correctly?

My code is: fun main(args: Array<String>) { var age = 17 while (age <= 17) { println("Hello") age++ } } Why does Hello appear only once and not 17 times?

16th Mar 2023, 3:31 PM
Artem Riabchikov
Artem Riabchikov - avatar
2 Answers
+ 4
In the beginning age = 17, so age =< 17 is true because 17 is smaller or equal to 17. The 2nd time age = 18, so age <= 17 is false because 18 is not smaller or equal to 17.
16th Mar 2023, 3:43 PM
Lisa
Lisa - avatar
+ 1
second time age =18 because of ”age++”
18th Mar 2023, 12:48 AM
Алексей Молодан
Алексей Молодан - avatar