For Loop and Its Usage in Kotlin


Definition

In Kotlin, for loop is used for looping a certain block based on Range and Array.

Types of Usage

  • Range Operator
  • Range Operator with downTo
  • Range Operator with step
  • For loop for Array
  • For loop for Array index
  • For loop for String 

Syntax

for (element in array) {
    // body of loop
}

For Loop Example Program


// For Loop in Kotlin example program
// Loop and Control Statements Kotlin Programs, Basic Kotlin Programs

fun main(args: Array<String>) {

    var num1 = 0
    
    for (num1 in 1..5) {
       println(" Value : $num1")
    }
}

Sample Output

 Value : 1
 Value : 2
 Value : 3
 Value : 4
 Value : 5