> For the complete documentation index, see [llms.txt](https://guide.cronuszen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.cronuszen.com/gpcscripting/gpc-script-guide/learn-gpc/flow-control.md).

# Flow control

### `if`, `else if`, `else`

```cpp
if(mode == 0) {
    // first path
} else if(mode == 1) {
    // second path
} else {
    // fallback
}
```

***

### `switch`, `case`, `default`

```cpp
switch(mode) {
    case 0: {
        set_val(TRACE_1, 10);
        break;
    }
    case 1: {
        set_val(TRACE_1, 20);
        break;
    }
    default: {
        mode = 0;
        break;
    }
}
```

Case values must be compile-time constants and unique. Braces are required around case bodies. Omit `break` only when fallthrough is deliberate and documented.

{% hint style="info" %}
Deliberate fallthrough still produces `GPC6018`. GPC has no annotation to mark it as intentional, so the warning appears on every case that falls through — expect it, and document the intent in a comment.
{% endhint %}

### `for`

```cpp
for(i = 0; i < sizeof(BUTTONS); i++) {
    // bounded work
}
```

### `while`

```cpp
while(i < 8) {
    i++;
}
```

### `do ... while`

```cpp
do {
    i++;
} while(i < 8);
```

***

### `break` and `continue`

`break` exits the nearest loop or switch. `continue` skips to the next iteration of the nearest loop. Neither is valid outside its allowed context.

***

### `return`

`return;` immediately exits the current user-created function. `return expression;` also supplies the function's result:

{% code title="return-value.gpc" %}

```cpp
function absolute(value) {
    if(value < 0) {
        return -value;
    }
    return value;
}
```

{% endcode %}

A bare `return;` exits without a value. Do not mix bare and valued returns in the same function. If execution reaches the end of a user function without returning a value, its result is `0`; callers may also ignore a user function's result.

{% hint style="info" %}
All real-time loops must have a provable quick exit. A loop that waits for a future physical event will lock the current VM cycle; use persistent state evaluated across `main` cycles instead.
{% endhint %}


---

# 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://guide.cronuszen.com/gpcscripting/gpc-script-guide/learn-gpc/flow-control.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.
