For the complete documentation index, see llms.txt. This page is also available as Markdown.

Operator Types

An operator is a symbol that tells the interpreter to perform specific mathematical, relational, or logical operations and produce the final result. This section details the operators available in GPC.

int a = 10;
int b = 5;
int c = 0;

main {
    // assignment operators
    c = 5;       // c is set to a value of 5
    c += 5;      // c is set to a value of 10 (5 + 5 = 10)
    c -= 5;      // c is set to a value of 5 (10 - 5 = 5)
    c *= 5;      // c is set to a value of 25 (5 * 5 = 25)
    c /= 5;      // c is set to a value of 5 (25 / 5 = 5)
    c %= 5;      // c is set to a value of 0 (5 % 5 = 0)

    // mathematical operators
    c = a + b;   // addition, c = 15
    c = a - b;   // subtraction, c = 5
    c = a * b;   // multiplication, c = 50
    c = a / b;   // division, c = 2
    c++;         // increment, c = 3
    c--;         // decrement, c = 2

    // logical operators
    if(c != a)   // NOT: if c is not equal to a
    if(a && c)   // AND: if a and c are true
    if(a || c)   // OR: if a or c are true
    if(a ^^ c)   // XOR: if either a or c are true but not both

    // relational operators
    if(c == 10)  // equal to: if c is equal to 10
    if(c != 50)  // not equal to: if c is not equal to 50
    if(c < 20)   // less than: if c is less than 20
    if(c > 30)   // greater than: if c is greater than 30
    if(c <= 40)  // less than or equal to: if c is less than or equal to 40
    if(c >= 40)  // greater than or equal to: if c is greater than or equal to 40
}

Assignment

= is the assignment operator. Think of this as "gets set to" rather than "equal to". When = is used, the left operand gets set to the value of the right operand.

There are also a number of shorthands for common tasks such as incrementing a value by a set amount:

Operator
Description

=

Sets the left operand to the value of the right operand

+=

Sets the left operand to the value of the left operand plus the right operand

-=

Sets the left operand to the value of the left operand minus the right operand

*=

Sets the left operand to the value of the left operand multiplied by the right operand

/=

Sets the left operand to the value of the left operand divided by the right operand

%=

Sets the left operand to the remainder of dividing the left operand by the right operand

In the example below, assume a holds a value of 10:


Arithmetic

It is often necessary to perform arithmetic on two values. The following table lists the arithmetic operators available in GPC:

Operator
Description

+

Adds two operands

-

Subtracts right operand from the left operand

*

Multiplies both operands

/

Divides the left operand by the right operand

%

Modulus, gives the remainder of an integer division

++

Increments by 1

--

Decrements by 1

In the example below, assume a holds a value of 10 and b holds a value of 5:

{% hint style="info" %} GPC does not support fractions so the division operator / will drop any fractions. For example, 10 / 3 = 3 as the fraction is dropped. It also does not round, so 3 / 4 = 0 and not 1. {% endhint %}


Logical

Logical operators are important in any programming language as they allow to tell the interpreter to make decisions based on certain conditions. The following table lists the logical operators within the GPC language:

Operator
Description

&&

AND operator, if both operands are true then the condition becomes true

||

OR operator, if either operand is true then the condition becomes true

^^

XOR operator, if either operand is true but not both then the condition becomes true

!

NOT operator, reverses the logical state of an operand

In the example below, assume a holds a value of 1 and b holds a value of 0:


Relational

Relational operators produce boolean results (true or false) while comparing two operands. The following table lists the relational operators which are available in GPC:

Operator
Description

==

Equal to, if the left operand holds the same value as the right then the condition becomes true

!=

Not equal to, if the left operand does not hold the same value as the right then the condition becomes true

>

Greater than, if the left operand holds a value greater than the right then the condition becomes true

<

Less than, if the left operand holds a value less than the right then the condition becomes true

>=

Greater than or equal to, if the left operand holds a value which is greater than or equal to the right then the condition becomes true

<=

Less than or equal to, if the left operand holds a value which is less than or equal to the right then the condition becomes true

In the example below, assume a holds a value of 30 and b holds a value of 10:


Binary

Binary is the same as logical, except they work with bits.

Operator
Description

&

AND operator, if both bits are 1 then the resulting bit is 1

|

OR operator, if either bit is 1 then the resulting bit is 1

^

XOR operator, if either bit is 1 but not both then the resulting bit is 1

<<

Left shift operator, shifts bits to the left by the specified amount

>>

Right shift operator, shifts bits to the right by the specified amount

~

NOT operator, inverts all bits

In the example below:

Last updated