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.

Tags

Do While Loop, Kotlin, Example Program

Similar Programs Chapter Last Updated
Kotlin Continue Kotlin 02-03-2018
Kotlin Break Statement Kotlin 27-01-2018
Kotlin For Loop Kotlin 30-11-2017
Kotlin While Loop Kotlin 19-11-2017
Kotlin When Statement Kotlin 18-11-2017
Kotlin Nested If Kotlin 18-11-2017
Kotlin If Else If Statement Kotlin 22-09-2018
Kotlin If Statement Kotlin 17-11-2017
Kotlin Print And Println Kotlin 22-09-2018
Kotlin Type Conversion Kotlin 17-11-2017
Kotlin Logical Operators Kotlin 17-11-2017
Kotlin Assignment Operators Kotlin 15-11-2017
Kotlin Arithmetic Operators Kotlin 15-11-2017
Kotlin Data Types Kotlin 12-11-2017
Kotlin Variables Example Kotlin 11-11-2017
Kotlin Comments Example Kotlin 11-11-2017
Kotlin Hello World Kotlin 11-11-2017

1