> 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/gpc-developer-guide/flow-control.md).

# Flow Control

```mermaid
flowchart TD
    A(( )) --> B{if
    condition}
    B -- TRUE --> C[statements]
    B -- FALSE --> D[statements]
    C --> E(( ))
    D --> E
```

| Command    | Description                                                                                         | Zen | Plus |
| ---------- | --------------------------------------------------------------------------------------------------- | --- | ---- |
| `if`       | Executes a block of code if the condition is met                                                    | ✔️  | ✔️   |
| `else`     | Executes a block of code if the previous `if` condition is not met                                  | ✔️  | ✔️   |
| `else if`  | Executes a block of code if the previous `if` condition is not met but the current condition is met | ✔️  | ✔️   |
| `while`    | A loop that runs while a condition is met                                                           | ✔️  | ✔️   |
| `do while` | A loop that runs at least once                                                                      | ✔️  | ✔️   |
| `for`      | A loop that runs a set amount of times                                                              | ✔️  | ✔️   |
| `break`    | Breaks out early while in a loop                                                                    | ✔️  | ✔️   |

***

### if

The `if` command is one of the single most useful tools for your GPC scripting needs. It allows you to control when certain blocks of code are run and it takes any expression, function, or code which returns or has a value. It can also be used in the `init`, `main`, `combo`, and `function` sections of your code.

The `if` command works by evaluating the expression within it to its boolean value. An expression is anything that has a value, such as a function call that returns a value, a variable, or a mathematical sum, literal values and comparisons.

In computer programming languages, a boolean is a data type with only two possible values, true or false. In GPC, false means 0 and true is a value other than 0, be it a negative or positive value.

If the expression placed within an `if` command's brackets is true, then the code nested within it is executed. If the expression is false and an `else` block is present, that block is executed instead:

As shown below:

```gpc
main {
    if(true) {
        // this will always run as the condition above is always true
    }
    if(false) {
        // this will never run as the condition above is never true
    }
}
```

***

### else

Should you wish to execute a different block of code when an `if` command does not return true then you would use the `else` command. An `else` command must have an `if` command preceding it. The code block contained within an `else` command will be executed if the expression in the `if` statement returns false, as shown below:

```gpc
main {
    if(false) {
        // this will never run as the condition above is never true
    } else {
        // this will run as the result of the previous condition never
        // is true
    }
}
```

***

### else if

`else if` is a combination of `else` and `if`. Just like the `else` command it allows for a different block of code to be executed when the statement within the `if` command returns false. However, it will only execute the code block when the statement within its parameter returns true.

For example, if you were playing a first person shooter game and wished for rapid fire to be disabled when you aim down the sights but wanted the Cronus Zen to automatically hold breath in the game to steady your aim while aiming down the sights, the following code would do this for you:

> **Note:** Example below is more of a "how it works", needs to be updated with one reflecting the above text!

```gpc
main {
    if(false) {
        // this will never run as the condition above is never true
    } else if(true) {
        // this will run as the result of the previous condition never
        // is true but the condition here is always true
    }
}
```

***

### while

The operation of the `while` command is straight forward, it will execute code nested within it until the expression contained in its parameter is no longer true.

```gpc
int a;

main {
    while(a < 10) {
        // do something while a is less than 10
    }
}
```

***

### do while

`do while` is similar to `while` in that it runs a block of code while a condition is true, the difference is that it executes the block at least once, see below for an example:

```gpc
int a = 0;

main {
    while(a != 0) {
        // this will never execute as a is already 0
    }

    do {
        // this will be executed once, then the condition will be checked
        // to see if we should run the block again
    } while(a != 0);
}
```

***

### for

The `for` loop can be used to run a block of code for a set amount of times, the syntax for using it is as follows:

```
for(<initializer>; <condition>; <increment>)
```

See the below example for how it could be used:

```gpc
int a;

main {
    for(a = 0; a < 10; a++) {
        // whatever you put here will be run 10 times
    }
}
```

***

### break

The `break` command is used to end a loop early based on a condition, see below for an example:

```gpc
int a;

main {
    while(true) {
        if(a == 0) {
            // we want to end the loop if the variable a has a value of 0
            break;
        }
    }
}
```


---

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