> For the complete documentation index, see [llms.txt](https://university-west.gitbook.io/programming-paradigms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/control-structures/iteration-loops/for-loops.md).

# For loops

A for loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly. The for loop is used when you know in advance how many times you want to execute a statement or a block of statements. It is commonly used for iterating over a sequence (such as a list, tuple, dictionary, set, or string) or other iterable objects.

The for loop has a standard structure which includes: initialization, condition, and increment/decrement statements. The loop initiates the iteration by setting an initial value for its counter. Then, before each iteration, it evaluates the condition. If the condition is true, the loop's body is executed. After the body of the for loop executes, the loop increments or decrements the counter. This process repeats until the condition becomes false.

#### Python Example

In Python, the for loop is used to iterate over the elements of a sequence (like a list, tuple, string) or other iterable objects. Here’s a simple example:

```python
# Python example that prints numbers from 1 to 5
for i in range(1, 6):
    print(i)
```

#### C# Example

In C#, the for loop is used similarly, but the syntax is slightly different. Here's a basic example:

```csharp
// C# example that prints numbers from 1 to 5
for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}
```

**Understanding the classic loop syntax**

The C# `for` loop syntax is the "classic" syntax. Let's break down the syntax of the `for` loop as seen in the example:

```csharp
for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}
```

1. **Initialization (`int i = 1`):** This part is executed only once at the beginning of the loop. It allows us to declare and initialize the loop counter variable. In the example, `i` is initialized to `1`. This is where you set up your loop variable.
2. **Condition (`i <= 5`):** Before each iteration of the loop, C# evaluates this condition. If the condition evaluates to true, the loop body is executed. If it is false, the loop stops. In our example, the loop will continue as long as `i` is less than or equal to `5`.
3. **Increment/Decrement (`i++`):** After each iteration of the loop body, this part is executed. It's often used to increment or decrement the loop counter. In the example, `i++` increments `i` by `1` after each loop iteration.
4. **Loop Body (`{ Console.WriteLine(i); }`):** This is the block of code that gets executed repeatedly as long as the condition is true. Each time the condition evaluates to true, the code inside the loop body is executed. In the example, it prints the current value of `i` to the console.

Each of these components plays a vital role in how the `for` loop operates, providing a compact way to iterate over a range of values with precise control over the start point, end point, and the increment/decrement step size.


---

# 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, and the optional `goal` query parameter:

```
GET https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/control-structures/iteration-loops/for-loops.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
