Kotlin For Loop Tutorial 🎯

beginner
17 min

Kotlin For Loop Tutorial 🎯

Welcome to the Kotlin For Loop tutorial! In this comprehensive guide, we'll dive into one of the most fundamental aspects of programming in Kotlin - the For Loop. By the end of this tutorial, you'll have a solid understanding of for loops and be ready to apply them in your projects. 📝

Table of Contents

  1. Understanding the Basics of For Loops
  2. The Syntax of Kotlin For Loops
  3. Using For Loops with Various Data Structures
  4. Breaking and Continuing For Loops
  5. Nested For Loops
  6. Quiz

<a name="basics"></a>

1. Understanding the Basics of For Loops 💡

A For Loop is a control structure that allows you to repeat a block of code a specific number of times. In Kotlin, we use for loops to iterate through collections like arrays and lists.

For loops are useful when you need to perform an action a certain number of times, like printing numbers from 1 to 10 or iterating through an array of items.

<a name="syntax"></a>

2. The Syntax of Kotlin For Loops 📝

The basic syntax of a Kotlin For Loop looks like this:

kotlin
for (initializer; condition; incrementor) { // code to be executed }

Let's break down this syntax:

  • initializer: This is where we initialize our loop variable. It typically sets the initial value of the loop counter.
  • condition: This is a boolean expression that determines when the loop should continue or end. As long as the condition evaluates to true, the loop will continue.
  • incrementor: This is used to update the loop variable after each iteration. This is often used to count up or down.

Here's a simple example where we print numbers from 1 to 5:

kotlin
for (i in 1..5) { println(i) }

<a name="data-structures"></a>

3. Using For Loops with Various Data Structures 💡

For loops can be used with different data structures, such as arrays and lists.

Using For Loops with Arrays

Here's an example of iterating through an array using a for loop:

kotlin
val numbers = intArrayOf(1, 2, 3, 4, 5) for (number in numbers) { println(number) }

Using For Loops with Lists

Iterating through lists in Kotlin is quite similar to arrays:

kotlin
val fruits = listOf("Apple", "Banana", "Orange", "Mango", "Pineapple") for (fruit in fruits) { println(fruit) }

<a name="break-continue"></a>

4. Breaking and Continuing For Loops 💡

Sometimes, you might want to break out of a loop early or skip an iteration. Kotlin provides the break and continue keywords for this purpose.

Breaking a For Loop

The break keyword ends the loop immediately. Here's an example:

kotlin
for (i in 1..10) { if (i == 5) { break } println(i) }

Continuing a For Loop

The continue keyword skips the current iteration and moves on to the next one. Here's an example:

kotlin
for (i in 1..10) { if (i % 2 == 0) { continue } println(i) }

<a name="nested"></a>

5. Nested For Loops 💡

Nested for loops allow you to iterate through multiple collections at the same time. Here's an example:

kotlin
val numbers1 = intArrayOf(1, 2, 3) val numbers2 = intArrayOf(4, 5, 6) for (number1 in numbers1) { for (number2 in numbers2) { println("${number1} * ${number2} = ${number1 * number2}") } }

<a name="quiz"></a>

6. Quiz 💡

Quick Quiz
Question 1 of 1

What is the output of the following code snippet?