# 1.4.2 For Loops

Basic for loops are the same as Java:

```
for (variable initialization; boolean expression; increment)
{
    // Will execute if boolean is true, and until the boolean 
    // expression is false
}
```

The `for` loop initializes a value, then loops while the boolean expression is true. Each time through the loop, the loop variable takes on a new value.

The boolean expression gets evaluated after the loop variable is first initialized and then each cycle after the variable increments. Once inside the block of statements, the block will continue to execute even if the loop variable changes and the boolean expression is no longer true.

### Additional Examples of For Loops

Many for loops are designed to start at zero and increment up by 1 each time. For loops are a lot more versatile than this though.

For loops can start, stop, and increment with any value. Here are a few other examples:

```
for (int i = 10; i >= 0; i--) // counts down from 10 to 0

for (int i = 2; i <= 100; i += 2) // counts by 2s from 2 to 100

for (int i = 1; i <= 256; i *= 2) // increments in multiples of 2
```

### Try This Example

{% embed url="<https://onlinegdb.com/Bgd1D-Qt3>" %}
For Loops
{% endembed %}


---

# 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://mr-poston-1.gitbook.io/c++/1.-c++-basics/1.4-loops/1.4.2-for-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.
