In this tutorial you will learn about Loops via simple step by step examples.

Example 1: Loops

Let us look at a simple Loops example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

Kotlin Android Activity Lifecycle Example

Kotlin Android Activity Lifecycle E...
Kotlin Android Activity Lifecycle Example
  • Loops.kt
  1. In your editor or IDE, create a file known as Loops.kt.
  2. Then add the following code:

(a). Loops.kt

package Loops

fun main(args : Array<String>) {
    var presidents = arrayOf("jfk", "nixon", "carter")

    print("I love these presidents ")
    for (p in presidents) {
        print ("$p, ")
    }

    println("")
    print("I voted for these presidents ")
    for (p in presidents.indices) {
        print ("${presidents

}, ") } println("") print("I loath these presidents ") var i = presidents.size while (i > 0) { i-- print("${presidents[i]}, ") } println("") print("I puzzle these presidents ") var q = presidents.size do { q-- print("${presidents[q]}, ") }while(q > 0) }

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License

Example 2: Jumps or Breaks in Loops

Let us look at a simple Jumps or Break example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

  • Jumps.kt
  1. In your editor or IDE, create a file known as Jumps.kt.
  2. Then add the following code:

(a). Jumps.kt

package Jumps

fun main(args : Array<String>) {

    [email protected] println("print again")

    [email protected] for (i in 1..50) {
        print("$i,")
        if (i == 25) {
            [email protected]
        }
    }
    println("")
    println("end of line")
}

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License

Example 3: Ranges

Let us look at a simple Ranges example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

  • Ranges.kt
  1. In your editor or IDE, create a file known as Ranges.kt.
  2. Then add the following code:

(a). Ranges.kt

package Ranges

fun main(args : Array<String>) {
    for (i in 1..10)
        print("$i,")

    println("")

    for (i in 'A'..'z')
        print("$i,")
}

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License