> For the complete documentation index, see [llms.txt](https://gauntletlang.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gauntletlang.gitbook.io/docs/loops.md).

# Loops

## Gauntlet supports two types of loops

{% hint style="info" %}
For syntax of `varPattern` see [Read before Proceeding](/docs/read-before-proceeding.md#common-placeholders-throughout-documentation)
{% endhint %}

{% hint style="info" %}
`break` and `continue` are supported
{% endhint %}

### 1. For-loops

#### Syntax for-loop (traditional style)&#x20;

```fsharp
for let <varPattern> = <expr>; <terminal expr>; <expr run after every iteration> {
    <loop body>
} 
```

#### Working Example

{% tabs %}
{% tab title="Gauntlet" %}

```fsharp
package main

import "fmt" as fmt

fun Unit main() {
  fmt.println("Counting to 10...")
  for let a = 1; a <= 10; a++ {
    fmt.println(a)
  }
  fmt.println("Done!")
}
```

{% endtab %}

{% tab title="Transpiled" %}

```go
package main

import fmt "fmt"

func main() {
	fmt.Println("Counting to 10...")
	for a := 1; a <= 10; a++ {
		fmt.Println(a)
		// Eliminates any 'unused variable' errors
		_ = a
	}
	fmt.Println("Done!")

}

```

{% endtab %}

{% tab title="Output" %}

```
Counting to 10...
1
2
3
4
5
6
7
8
9
10
Done!
```

{% endtab %}
{% endtabs %}

#### Syntax for for-loop (shorthand style)

```fsharp
for let <varPattern> in <iterable> {
    <loop body>
}
```

#### Working Example

{% tabs %}
{% tab title="Gauntlet" %}

```fsharp
package main

import "fmt" as fmt

fun Unit main() {
  fmt.println("Iterating through every letter in the word 'Hello'")
  for let _, c in "Hello" {
    fmt.println((String)(c))
  }
  fmt.println("Done!")
}
```

{% endtab %}

{% tab title="Transpiled" %}

```go
package main

import fmt "fmt"

func main() {
	fmt.Println("Iterating through every letter in the word 'Hello'")
	for _, c := range "Hello" {
		fmt.Println(string(c))
		// Eliminates any 'unused variable' errors
		_ = c
	}
	fmt.Println("Done!")

}

```

{% endtab %}

{% tab title="Output" %}

```
Iterating through every letter in the word 'Hello'
H
e
l
l
o
Done!
```

{% endtab %}
{% endtabs %}

### 2. While-loops

#### While-loop syntax:

```fsharp
for <terminal expr> {
    <loop body>
}
```

#### Working Example

{% tabs %}
{% tab title="Gauntlet" %}

```fsharp
package main

import "fmt" as fmt

fun Unit main() {
  fmt.println("The program will terminate if you say the magic word...")
  
  let magicWord = "please"
  zero String input
  
  while input != magicWord {
    fmt.println("What's the magic word?")
    fmt.scan(&input)
  }
  fmt.println("You said it!")
}
```

{% endtab %}

{% tab title="Transpiled" %}

```go
package main

import fmt "fmt"

func main() {
	fmt.Println("The program will terminate if you say the magic word...")
	magicWord := "please"
	var input string
	for {
		if !(input != magicWord) {
			break
		}
		fmt.Println("What's the magic word?")
		fmt.Scan(&input)
	}
	fmt.Println("You said it!")
	// Eliminates any 'unused variable' errors
	_, _ = magicWord, input

}

```

{% endtab %}

{% tab title="Output" %}

```
The program will terminate if you say the magic word...
What's the magic word?
Gauntlet
What's the magic word?
Go
What's the magic word?
Life
What's the magic word?
please
You said it!
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gauntletlang.gitbook.io/docs/loops.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
