Kotlin while loop Last Updated : 10 May, 2025 Comments Improve Suggest changes 16 Likes Like Report In programming, loop is used to execute a specific block of code repeatedly until certain condition is met. If you have to print counting from 1 to 100 then you have to write the print statement 100 times. But with help of loop you can save time and you need to write only two lines.While loopIt consists of a block of code and a condition. First of all the condition is evaluated and if it is true then execute the code within the block. It repeats until the condition becomes false because every time the condition is checked before entering into the block. The while loop can be thought of as repeating of if statements.Syntaxwhile(condition) { // code to run}FlowchartKotlin program to print numbers from 1 to 10 using a while loop:In the program below, we print the numbers using a while loop. First, initialize the variable number to 1. Put the expression (number <= 10) in a while loop and check if it is true or not?. If true, enter the block and execute the print statement and increment the number to 1. This repeats until the condition becomes false. Example: Kotlin fun main(args: Array<String>) { var number = 1 while(number <= 10) { println(number) number++; } } Output: 12345678910Kotlin program to print the elements of an array using a while loop: In the below program, we create an array(names) and initialize it with different numbers of strings, and also initialize a variable index to 0. The size of an array can be calculated by using arrayName.size. Put the condition (index < names.size) in the while loop. If index value less than or equal to array size then it enters into the block and print the name stored at the respective index and also increment the index value after each iteration. This repeats until the condition becomes false. Example: Kotlin fun main(args: Array<String>) { var names = arrayOf("Praveen","Gaurav","Akash","Sidhant","Abhi","Mayank") var index = 0 while(index < names.size) { println(names[index]) index++ } } Output: PraveenGauravAkashSidhantAbhiMayank Create Quiz Comment P Praveenruhil Follow 16 Improve P Praveenruhil Follow 16 Improve Article Tags : Kotlin Kotlin Control-flow Explore OverviewIntroduction to Kotlin4 min readKotlin Environment setup for Command Line2 min readKotlin Environment setup with Intellij IDEA2 min readHello World program in Kotlin2 min readBasicsKotlin Data Types3 min readKotlin Variables2 min readKotlin Operators4 min readKotlin Standard Input/Output4 min readKotlin Type Conversion2 min readKotlin Expression, Statement and Block4 min readControl FlowKotlin if-else expression4 min readKotlin while loop2 min readKotlin do-while loop2 min readKotlin for loop4 min readKotlin when expression6 min readKotlin Unlabelled break4 min readKotlin labelled continue4 min readArray & StringKotlin Array6 min readKotlin String4 min readFunctionsKotlin functions7 min readKotlin Default and Named argument7 min readKotlin Recursion3 min readKotlin Tail Recursion2 min readKotlin Lambdas Expressions and Anonymous Functions6 min readKotlin Inline Functions5 min readKotlin infix function notation5 min readKotlin Higher-Order Functions6 min readCollectionsKotlin Collections4 min readKotlin list : Arraylist6 min readKotlin list : listOf()7 min readKotlin Set : setOf()4 min readKotlin hashSetOf()4 min readKotlin Map : mapOf()5 min readKotlin Hashmap7 min readOOPs ConceptKotlin Class and Objects4 min readKotlin Nested class and Inner class3 min readKotlin Setters and Getters4 min readKotlin Class Properties and Custom Accessors3 min readKotlin Constructor6 min readKotlin Visibility Modifiers6 min readKotlin Inheritance10 min readKotlin Interfaces7 min readKotlin Data Classes3 min readKotlin Sealed Classes4 min readKotlin Abstract class5 min readEnum Classes in Kotlin4 min readKotlin extension function4 min readKotlin generics6 min readException HandlingKotlin Exception Handling - try, catch, throw and finally5 min readKotlin Nested try block and multiple catch block3 min readNull SafetyKotlin Null Safety7 min readKotlin Type Checking and Smart Casting3 min readKotlin Explicit Type Casting3 min readRegex & RangesKotlin Regular Expression4 min readKotlin Ranges3 min read Like