---
title: Flow Control
slug: gpc/flow-control
description: Learn about the different commands used in GPC scripting, such as "if", "else", "else if", "while", "do while", "for", and "break". Understand how each command functions, including their specific conditions and loop behaviors. Enhance your programming ski
docTags: 
createdAt: 2021-12-04T12:29:25.000Z
---

::Image[]{src="https://api.archbee.com/api/optimize/IwUlH55FdaPsVELSqm1Fc/PHJuojlBH9QQq_XrBzntg_image.png" size="50" width="338" height="286" position="center" darkWidth="800" darkHeight="677" showCaption="false"}

| 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                                                                    | ✔️  | ✔️45 |
| for      | A loop that runs a set amount of times                                                            | ✔️  | ✔️   |
| break    | Breaks out early while in a loop                                                                  | ✔️  | ✔️   |

:::::Tabs
::::Tab{title="if"}
The **if&#x20;**&#x63;ommand 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,&#x20;**&#x61;nd **function&#x20;**&#x73;ections of your code.&#x20;
The **if&#x20;**&#x63;ommand works by evaluating the expression within it to its boolean value.  &#x20;
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.&#x20;
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.&#x20;
If the expression placed within an **if&#x20;**&#x63;ommands brackets is TRUE, then the code nested within it is executed, as shown below

:::CodeblockTabs
GPC

```apex
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
    }
}
```
:::
::::

::::Tab{title="else"}
Should you wish to execute a different block of code when an **if&#x20;**&#x63;ommand does not return TRUE then you would use the **else&#x20;**&#x63;ommand.&#x20;
An else command must have an **if&#x20;**&#x63;ommand 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

:::CodeblockTabs
GPC

```apex
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
    }
}
```
:::
::::

::::Tab{title="else if"}
**else if** is a combination of **else&#x20;**&#x61;nd **if**. Just like the **else&#x20;**&#x63;ommand it allows for a different block of code to be executed when the statement within the **if&#x20;**&#x63;ommand returns FALSE. However, it will only execute the code block when the statement within its parameter returns TRUE.&#x20;
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&#x20;
~~NOTE: Example below is more of a how it works, needs to be updated with one reflecting above text!~~

:::CodeblockTabs
GPC

```apex
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
    }
}
```
:::
::::

::::Tab{title="while"}
The operation of the **while&#x20;**&#x63;ommand is straight forward, it will execute code nested within it until the expression contained in its parameter is no longer TRUE

:::CodeblockTabs
GPC

```apex
int a;
main {
    while (a < 10) {
        // Do something while a is less than 10
    }
}
```
:::
::::

::::Tab{title="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

:::CodeblockTabs
GPC

```apex
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);
}
```
:::
::::

::::Tab{title="for"}
The **for&#x20;**&#x6C;oop can be used to run a block of code for a set amount of times, the syntax for using it is as follows:
**for&#x20;**( \< initalizer > ; \< condition > ; \< increment >)
See the below example for how it could be used

:::CodeblockTabs
GPC

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

::::Tab{title="break"}
:::CodeblockTabs
GPC

```apex
int a;
main {
    while (TRUE) {
        if (a == 0) {
            // We want to end the loop if the variable a has a value of 0
            break;
            The break command is used to end a loop early based on a condition, see below for an example
        }
    }
}
```
:::
::::
:::::

