do..while loop in Kotlin guarantees at least one execution of block of statements because loop evaluates the boolean expression at the end of the loop’s body.
Here the above program user input are taking by using readline() function inside the do while loop.
While loop outside the body of do will check whether the condition is true or not and iterate accordingly.
It will exits the loop when the while statement statement condition is false.
Kotlin Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
fun main(args: Array<String>) { var sum: Int = 0 var input: String do { print("Enter an integer: ") input = readLine()!! sum += input.toInt() } while (input != "0") println("Sum = $sum") } |
Output:
1 2 3 4 5 6 7 8 |
Enter an integer: 3 Enter an integer: 5 Enter an integer: 7 Enter an integer: 8 Enter an integer: 0 Sum = 23 |