# 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
