Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende for Loop in Kotlin | Introduction to Loops
Kotlin Loops

bookfor Loop in Kotlin

Desliza para mostrar el menú

The for Loop in Kotlin

The for loop is a fundamental control structure in Kotlin that lets you repeat a block of code for each item in a collection or over a range of values. This is especially useful when you need to perform the same action multiple times, such as printing numbers, processing lists, or iterating through arrays.

What Is a for Loop?

A for loop in Kotlin allows you to execute a block of code for every element in a sequence, such as a range (1..5), an array, or a list. The loop automatically assigns each value to a loop variable, making your code concise and easy to read.

Basic Syntax

The basic syntax for a for loop in Kotlin looks like this:

for (variable in collection) {
    // Code to repeat
}
  • variable is a name you choose to represent the current item in the collection;
  • collection is any sequence you want to loop through, such as a range, array, or list.

Example: Looping Through a Range

Here is a simple example that prints numbers from 1 to 5:

for (number in 1..5) {
    println(number)
}

This loop will print:

1
2
3
4
5

When to Use a for Loop

Use a for loop in Kotlin when you need to:

  • Repeat an action for every item in a list or array;
  • Execute code for each value in a range of numbers;
  • Process items in a collection one by one.

for loops help you write efficient, readable code when working with repeated actions or collections.

Using Ranges with for Loops

Kotlin makes it easy to iterate over a sequence of values using ranges. A range is a sequence of values defined by a start and an end point. Ranges are commonly used with the for loop to repeat actions for each value in the range.

Creating a Range

You create a range in Kotlin using the .. operator. For example, 1..5 creates a range containing the numbers from 1 to 5, inclusive.

Iterating Over a Range

You can use a for loop to iterate over each value in a range:

package com.example

fun main() {
    for (number in 1..5) {
        println("Number: $number")
    }
}

This code prints:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Using downTo and step

You can also create ranges that count down or use a custom step:

  • Use downTo to create a descending range;
  • Use step to skip values by a specified interval.
package com.example

fun main() {
    for (number in 5 downTo 1 step 2) {
        println("Counting down: $number")
    }
}

This prints:

Counting down: 5
Counting down: 3
Counting down: 1

Summary

  • Use .. to create an ascending range;
  • Use downTo for a descending range;
  • Use step to change the increment or decrement.

Ranges make for loops in Kotlin concise and expressive.

question mark

Which of the following is the correct way to write a simple for loop in Kotlin that iterates from 1 to 5?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 1. Capítulo 2
some-alt