+ 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