If Else in AngelScript

Branching with if and else in AngelScript is straightforward.

void main()
{
    // Here's a basic example.
    if (7 % 2 == 0)
    {
        print("7 is even\n");
    }
    else
    {
        print("7 is odd\n");
    }

    // You can have an `if` statement without an else.
    if (8 % 4 == 0)
    {
        print("8 is divisible by 4\n");
    }

    // Logical operators like `&&` and `||` are often useful in conditions.
    if (8 % 2 == 0 || 7 % 2 == 0)
    {
        print("either 8 or 7 are even\n");
    }

    // A variable can be declared and initialized before the condition.
    // This variable will be available in the current and all subsequent branches.
    int num = 9;
    if (num < 0)
    {
        print(num + " is negative\n");
    }
    else if (num < 10)
    {
        print(num + " has 1 digit\n");
    }
    else
    {
        print(num + " has multiple digits\n");
    }
}

To run this script, you would typically use an AngelScript interpreter or embed it in a host application. The output would be:

7 is odd
8 is divisible by 4
either 8 or 7 are even
9 has 1 digit

Note that in AngelScript, you need parentheses around conditions, and the braces are required for multi-line blocks. Single-line blocks can omit the braces, but it’s generally good practice to include them for clarity.

AngelScript doesn’t have a ternary operator either, so you’ll need to use a full if statement even for basic conditions.