> For the complete documentation index, see [llms.txt](https://analytics.datalit.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://analytics.datalit.de/introduction-to-r/loops/die-for-schleife.md).

# Die For-Schleife

Die FOR-Schleife erlaubt es uns, einen Codeblock eine bestimmte Anzahl Male hintereinander auszuführen, ohne dass wir den Code selbst wiederholen müssen. Nehmt den folgenden Code als Beispiel:

```r
i <- 1
print(i)

i <- i + 1
print(i)

i <- i + 1
print(i)

i <- i + 1
print(i)

i <- i + 1
print(i)
```

Was ist die Ausgabe des obigen Codes? Es wird fünfmal nacheinander die Variable `i` auf der Konsole ausgegeben und jedes Mal hat `i` einen anderen Wert.&#x20;

```
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
```

Nach jeder Ausgabe mit `print` wird der Wert von `i` um Eins erhöht. Wir wissen vor der Ausführung des Codes schon, dass wir `i` insgesamt viermal erhöhen, bis der es den Wert 5 angenommen hat. In einem solchen Fall können wir unseren Code mithilfe einer FOR-Schleife deutlich vereinfachen und verkürzen:

```r
for(i in 1:5) {
    print(i)
}
```

Die Ausgabe ist identisch zum ersten Programm. Der Code aber viel kürzer und übersichtlicher.

## Mehr lesen

{% embed url="<https://rstudio-education.github.io/hopr/loops.html#for-loops>" %}


---

# 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 dynamically by asking a question.

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

```
GET https://analytics.datalit.de/introduction-to-r/loops/die-for-schleife.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.
