Kotlin Do While Loop
Chapter:
Kotlin
Last Updated:
19-11-2017 14:02:49 UTC
Program:
/* ............... START ............... */
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")
}
/* ............... END ............... */
Output
Enter an integer: 3
Enter an integer: 5
Enter an integer: 7
Enter an integer: 8
Enter an integer: 0
Sum = 23
Notes:
-
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.