> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin For Loop

The `for` loop in Kotlin iterates over any object that provides an iterator. Unlike traditional C-style `for` loops that rely on initialization, condition, and increment expressions, Kotlin's `for` loop operates exclusively as a "foreach" construct. It uses the `in` operator to traverse iterables, arrays, ranges, or any custom class that implements the required operator functions.

```kotlin theme={"dark"}
for (item in iterable) {
    // execution block
}
```

If the body of the loop contains only a single statement, the curly braces `{}` can be omitted.

## Underlying Mechanism

Kotlin's `for` loop relies on convention-based duck typing rather than requiring the implementation of a specific interface like `kotlin.collections.Iterator`. For an object to be evaluated on the right-hand side of the `in` keyword, it must satisfy the following compiler requirements via member or extension functions:

1. It must provide an `iterator()` function that returns an object.
2. The returned object must provide a `next()` function.
3. The returned object must provide a `hasNext()` function returning a `Boolean`.

All three of these functions must be explicitly marked with the `operator` modifier.

```kotlin theme={"dark"}
// Conceptual translation of a Kotlin for loop by the compiler
val iterator = iterable.iterator()
while (iterator.hasNext()) {
    val item = iterator.next()
    // execution block
}
```

## Ranges and Progressions

When iterating over primitive types, Kotlin avoids the overhead of allocating iterator objects. Iteration over ranges (`IntRange`, `LongRange`, `CharRange`) is compiled directly into optimized, index-based loops at the JVM level.

**Closed-ended Range (`..`):**
Iterates up to and including the upper bound.

```kotlin theme={"dark"}
for (i in 1..5) { }
```

**Half-open Range (`until` / `..<`):**
Iterates up to, but excluding, the upper bound.

```kotlin theme={"dark"}
for (i in 1 until 5) { }
// or
for (i in 1..<5) { }
```

**Reverse Progression (`downTo`):**
Iterates in descending order.

```kotlin theme={"dark"}
for (i in 5 downTo 1) { }
```

**Custom Step (`step`):**
Modifies the increment or decrement value of the progression. The step value must be strictly positive.

```kotlin theme={"dark"}
for (i in 1..10 step 2) { }
for (i in 10 downTo 1 step 3) { }
```

## Array and Collection Iteration

Arrays and standard library collections natively implement the required `iterator()` operator.

**Iterating by Index:**
To iterate over an array or list using its indices without boxing overhead, use the `indices` property.

```kotlin theme={"dark"}
for (i in list.indices) {
    val element = list[i]
}
```

**Destructuring Declarations:**
Kotlin supports destructuring within the `for` loop declaration. The `withIndex()` library function returns an `Iterable` of `IndexedValue` objects, which can be destructured directly into index and value components.

```kotlin theme={"dark"}
for ((index, value) in list.withIndex()) {
    // index is Int, value is the element type
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
