BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin do-while Loop with examples

Last Updated: September 7, 2020 by Chaitanya Singh | Filed Under: Kotlin Tutorial

A do-while loop is similar to while loop except that it checks the condition at the end of iteration. A do-while loop will at least run once even if the given condition is false.

Kotlin do-while loop Example

/**
 * created by Chaitanya for Beginnersbook.com
 */

package beginnersbook

fun main(args : Array<String>){

    var num = 100
    do {
        println("Loop: $num")
        num++
    }
    while (num<=105)
}

Output:
Kotlin do while loop

A do-while loop at least run once

As I mentioned in the beginning of this guide, a do-while loop will at least run once even if the given condition returns false. This happens because the do-while loop checks the condition after execution of the loop body.

/**
 * created by Chaitanya for Beginnersbook.com
 */

package beginnersbook

fun main(args : Array<String>){

    var num = 100
    do {
        println("Loop: $num")
        num++
    }
    while (false)
}

Output:
do while loop with false condition

Infinite do while loop in Kotlin

A do while loop that runs infinitely and never stops is called infinite do while loop. Lets look at the few examples of infinite do while loop.

Example 1:

var num = 100
do {
    println("Loop: $num")
    num++   
}
while (true)

Example 2:

var num = 100
do {
    println("Loop: $num")
    num--  
}
while (num<=105)

Example 3:

var num = 105
do {
    println("Loop: $num")
    num++
}
while (num>=100)
❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Keywords, Soft Keywords and Identifiers
  2. Kotlin for Loop with examples
  3. Kotlin Abstract Class with examples
  4. Kotlin Constructors with examples
  5. Kotlin String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap