Kotlin While Loop

The while and do-while loop concept is easy to understand in Kotlin. For the understanding, a while loop executes a statement while a certain condition is true. The check of the condition is checked at the beginning of the while loop. The do-while loop in contrast checks the condition at the end of the loop body. This means, that the loop is passed at least one time.

Endless loops

While loops can be quite dangerous in programming and should be avoided when possible. Often for loops can be used instead of while loops. The dangerous part of a while loop is the risk to create an endless loop. This happens when the condition is always evaluated to true. In some circumstances this may be desired behavior (e.g. GameLoop, Microprocessor MainLoop), but usually it is not.

    fun checkData() : Boolean
        return true

        println("it does't stop")