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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/learn-gpc/flow-control.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
