HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Loops (for / while)

Loops (for / while)

Ranges with for (1..5), conditional repetition with while, and iteration with .each{}.

Simple enough for a 5-year-old to understand

range with for

The range 1..3 DOES include 3.

Example code
1
for (i in 1..3) {
1, 2, 3 (3 included).
Loop1, 2, 3 (3 included).
2
println i
3
}
Loop
💡 Hint: 1..3 → 3 is inclusive.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the numbers from 1 to 5 vertically using a for loop.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

Classic loops work in Groovy but are rarely used in everyday code. Instead, collection methods and number objects' own repetition methods are preferred.

There are two formats for writing ranges: the two-dot format includes the upper bound, while the format with a less-than sign excludes it. When working with collection indices, the exclusive format is the correct one.

There is a limitation to keep in mind: you cannot break out of collection traversal methods using a loop break command. If early exit is required, you must either use a classic loop or switch to a method that finds the first match.

What you will learn

  • Loop formats
  • Range bounds
  • Repetition methods
  • Traversal method limitation
  • Early exit methods

Who is this for?

  • Developers who need to repeat tasks in their scripts
  • Those who cannot break out of a traversal method
  • Those who make mistakes with range boundaries

When to use?

When a specific number of repetitions is needed, the number object's repetition method is the shortest path.

If you need to stop when a condition is met, you should choose a method that finds the first match or a classic loop instead of a traversal method.

Common mistakes

Wrong

def liste = ["a", "b", "c"]
for (i in 0..liste.size()) {
  println liste[i]
}

The two-dot range includes the upper bound, so the loop accesses an invalid index on the last step and throws an error. Valid indices range from zero to one less than the length; it is necessary to use an exclusive range or iterate directly over the elements.

Correct

def liste = ["a", "b", "c"]
for (eleman in liste) {
  println eleman
}

Wrong

def liste = [1, 2, 3, 4]
liste.each { sayi ->
  if (sayi == 3) break
  println sayi
}

A traversal method calls a block, it is not a loop; you cannot use a break command inside it and you will get a compilation error. If early exit is needed, you should switch to a classic loop or use a method that finds the element you are looking for.

Correct

def liste = [1, 2, 3, 4]
for (sayi in liste) {
  if (sayi == 3) break
  println sayi
}

Wrong

def i = 0
while (i < 5) {
  println i
}

Since the counter is not incremented, the condition remains true forever and the script never ends. In a build script, this causes the build to hang completely. If the number of repetitions is known, using a repetition method eliminates this risk.

Correct

5.times { i ->
  println i
}

Frequently asked questions

What are repetition methods?

There is a method that allows a number to repeat a specified number of times, methods that count up or down between two numbers, and a method that advances by specifying a step. All of them take a block and are much more readable than a classic loop.

Is there a way to stop traversal early?

The method that returns the first element matching a condition stops when it finds it and avoids unnecessary work. Additionally, methods that check whether any or all elements satisfy a condition also short-circuit; these cover most early exit needs.

How can I access both the index and the element together?

The indexed traversal method provides both the element and its sequence number at each step. There is no need to maintain a manual counter and the risk of out-of-bounds errors is eliminated.

Summary

The two-dot range includes the upper bound.

You cannot break out of a traversal method using a break command.

Repetition methods replace classic loops.

You must ensure progress in a while loop.

The method that finds the first match short-circuits.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code