Search code examples
while-loopkotlininfinite-loop

While loops in kotlin


fun main(args: Array<String>) {
    var x = 9
    while (x>=0){
        println(x)
        x++
    }
}

If i compile this code, it will print out an infinite number of iteration of the loop.

Any way to set a limit of iteration there ?


Solution

  • Change

    while (x>=0){
    

    to

    while (x<100){
    

    It will give you all number from 9 to 99. Don't hesitate to change the condition based on your requirement.

    Just put a valid one, which stops.