ProgramGuru

For loops and ranges

For loops and ranges

A teacher wants to total several scores. A for loop visits each element of a sequence and performs the body for that element. It lets you describe one calculation without duplicating it for every student.

Kotlin for visits elements in an iterable, array or progression. Closed ranges use .., while ..< excludes the upper endpoint. downTo and step form other progressions. Keep the accumulator outside the body when its value must survive between iterations.

What you’ll learn

  • Trace a running total across iterations.
  • Explain endpoints, break and continue.

For loops and ranges in the example

  1. Total 0

    A teacher wants to total several scores.

    Total 0
  2. Add each score

    Kotlin for visits elements in an iterable, array or progression.

    Add each score
  3. Total 240

    The running total changes from zero to 70, 150 and 240.

    Total 240
The running total changes from zero to 70, 150 and 240. The desk loop prints one and two, stopping before three. The final total is printed once because that statement is outside the first loop.

Follow for loops and ranges

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  val scores = listOf(70, 80, 90)
  var total = 0
  for (score in scores) total += score
  println(total)
  for (desk in 1..<3) println(desk)
}

Expected output

240
1
2

Follow the execution

The running total changes from zero to 70, 150 and 240. The desk loop prints one and two, stopping before three. The final total is printed once because that statement is outside the first loop.

Try this in the Kotlin compiler by pasting the complete program.

Practice: For loops and ranges

Your task: How many desk values print if the range becomes 1..3?

Kotlin/JVM · complete program

fun main() {
  val scores = listOf(70, 80, 90)
  var total = 0
  for (score in scores) total += score
  println(total)
  for (desk in 1..<3) println(desk)
}

Worked answer

Three values print: 1, 2 and 3. The closed range includes its upper endpoint.

For loops and ranges: the important boundary

Break exits a loop and continue skips the remaining work for one iteration. Nested loops can multiply the amount of work. Mutating the structure of a collection during iteration needs a deliberate supported policy.

Check your understanding

How many desk values print if the range becomes 1..3?
Choose one answer. You can try again.

Answer and explanation

Three values print: 1, 2 and 3.

Three values print: 1, 2 and 3. The closed range includes its upper endpoint.

For loops and ranges: what to keep in mind

Three values print: 1, 2 and 3. The closed range includes its upper endpoint. Break exits a loop and continue skips the remaining work for one iteration.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: While and do-while loops →