Kotlin while loop Last Updated : 10 May, 2025 Comments Improve Suggest changes Like Article 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 Comment More info P Praveenruhil Follow Improve Article Tags : Kotlin Kotlin Control-flow Explore Kotlin Tutorial 4 min read OverviewIntroduction to Kotlin 4 min read Kotlin Environment setup for Command Line 2 min read Kotlin Environment setup with Intellij IDEA 2 min read Hello World program in Kotlin 2 min read BasicsKotlin Data Types 3 min read Kotlin Variables 2 min read Kotlin Operators 4 min read Kotlin Standard Input/Output 4 min read Kotlin Type Conversion 2 min read Kotlin Expression, Statement and Block 4 min read Control FlowKotlin if-else expression 4 min read Kotlin while loop 2 min read Kotlin do-while loop 2 min read Kotlin for loop 4 min read Kotlin when expression 6 min read Kotlin Unlabelled break 4 min read Kotlin labelled continue 4 min read Array & StringKotlin Array 6 min read Kotlin String 4 min read FunctionsKotlin functions 7 min read Kotlin Default and Named argument 7 min read Kotlin Recursion 3 min read Kotlin Tail Recursion 2 min read Kotlin Lambdas Expressions and Anonymous Functions 6 min read Kotlin Inline Functions 5 min read Kotlin infix function notation 5 min read Kotlin Higher-Order Functions 6 min read CollectionsKotlin Collections 6 min read Kotlin list : Arraylist 6 min read Kotlin list : listOf() 7 min read Kotlin Set : setOf() 4 min read Kotlin hashSetOf() 4 min read Kotlin Map : mapOf() 5 min read Kotlin Hashmap 7 min read OOPs ConceptKotlin Class and Objects 4 min read Kotlin Nested class and Inner class 3 min read Kotlin Setters and Getters 4 min read Kotlin Class Properties and Custom Accessors 3 min read Kotlin Constructor 6 min read Kotlin Visibility Modifiers 6 min read Kotlin Inheritance 10 min read Kotlin Interfaces 7 min read Kotlin Data Classes 3 min read Kotlin Sealed Classes 4 min read Kotlin Abstract class 5 min read Enum Classes in Kotlin 4 min read Kotlin extension function 4 min read Kotlin generics 6 min read Exception HandlingKotlin Exception Handling - try, catch, throw and finally 5 min read Kotlin Nested try block and multiple catch block 3 min read Null SafetyKotlin Null Safety 7 min read Kotlin Type Checking and Smart Casting 3 min read Kotlin Explicit Type Casting 3 min read Like