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

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

The for loop in Kotlin is used to iterate or cycle though the elements of array, ranges, collections etc. In this guide, we will learn how to use for loop in Kotlin with the help of various examples.

A simple example of for loop in Kotlin

In the following example we are iterating though an integer range using for loop.

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

package beginnersbook

fun main(args : Array<String>){

    for(n in 10..15){
        println("Loop: $n")
    }
}

Output:
Kotlin for loop

Kotlin for loop using Array

In the following example we have declared an array myArray and we are displaying the elements of the array using for loop.

package beginnersbook

fun main(args : Array<String>){

    val myArray = arrayOf("ab", "bc", "cd", "da")
    for (str in myArray){
        println(str)
    }
}

Output:

ab
bc
cd
da

Kotlin for loop iterating though array indices

We can also use array indexes to iterate though the array.

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

package beginnersbook

fun main(args : Array<String>){

    val myArray = arrayOf("Steve", "Robin", "Kate", "Lucy")
    for (n in myArray.indices){
        println("myArray[$n]: ${myArray[n]}")
    }
}

Output:
Kotlin array loop through collection

Function withIndex() in for loop

In the above example we have iterated through the array using array indices. Another way of doing the same is with the use of withIndex() function.

package beginnersbook

fun main(args : Array<String>){

    val myArray = arrayOf("Steve", "Robin", "Kate", "Lucy")
    for((index, value) in myArray.withIndex()){
        println("Value at Index $index is: $value")
    }
}

Output:

Value at Index 0 is: Steve
Value at Index 1 is: Robin
Value at Index 2 is: Kate
Value at Index 3 is: Lucy
❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Nested and Inner Class with examples
  2. Kotlin Keywords, Soft Keywords and Identifiers
  3. Kotlin String
  4. Kotlin Class and Objects – Object Oriented Programming (OOP)
  5. Kotlin Constructors with examples

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

Comments

  1. Sbusiso Nhlumayo says

    July 17, 2019 at 6:18 AM

    Now I have found the best website to learn Kotlin!!!!

    Keep up the good work.

    Reply
  2. Daniel says

    July 31, 2020 at 4:36 AM

    Nice and clear explanations. I have one question: Let’s say I want to use a for loop to iterate a list but I want to assign its result to a variable that is declared outside the loop, how can I do that?
    Because whenever I try to use a variable declared inside loop and try to use it outside, it gives me an error. Thanks a lot for your help.

    Reply

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