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 while Loop with examples

Last Updated: February 23, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

While loop is used to iterate a block of code repeatedly as long as the given condition returns true. In this guide, we will learn how to use while loop with the help of examples.

A simple while loop in Kotlin

In the following example we are displaying the value from 10 to 5 using while loop. The important point to note here is the counter, which is variable num in the following example, for ascending loop the counter value should increase to meet the given condition and for the descending loop the counter value should decrease in every iteration just like we did in the following example.

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

package beginnersbook

fun main(args : Array<String>){

    var num = 10

    while(num>=5){
        println("Loop: $num")
        num--
    }
}

Output:
Kotlin while loop

Infinite While loop

If the condition specified in the while loop never returns false then the loop iterates infinitely and never stops such while loops are called infinite while loops. We should always avoid such situation while writing code. Lets see few examples of infinite while loop.

1. Since the condition is always true this will run infinitely.

while (true){  
   println("loop")  
}

2. In this while loop we are incrementing the counter num, the counter initial value is 10 and we are increasing it on every iteration, which means the specified condition num>=5 will always remain true and the loop will never stop.

var num = 10

while(num>=5){
   println("Loop: $num")
   num++
}

3. The following loop will is also an infinite loop because the condition will always remain true as we are decreasing the value of num which means the condition num<=10 will always be satisfied.

var num = 5

while(num<=10){
   println("Loop: $num")
   num--
}
❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin for Loop with examples
  2. Kotlin do-while Loop with examples
  3. Kotlin Keywords, Soft Keywords and Identifiers
  4. Kotlin Ranges
  5. Kotlin Class and Objects – Object Oriented Programming (OOP)

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