Left Index

Kotlin While Loop

Loop is used in programming to repeat a specific block of code. In this article, you will learn to create while and do...while loops in Kotlin programming. The while loop is used to iterate a part of program several time. Loop executed the block of code until the condition has true. Kotlin while loop is similar to Java while loop.


Let's have a look of while loop syntax:

Syntax:
while(condition) {
   // TODO your task
}

Let's try an example to print number from 1 to 10 using while loop:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 1

	while (num1 <= 10) {
		println("Num is: " + num1)
		num1++
	}
}