Skip to content

Operators

Dylan Tuttle edited this page Sep 17, 2022 · 3 revisions

Binary Operators

Arithmetic Operators

Addition: +, +=

Operands Output
int, int int

The addition operator adds two ints together. The addition-assignment operator, var += [intlit], expands to var = var + [intlit].

Example:

int x = 1 + 2;  // x = 3
x += 1;         // x = 4

Subtraction: -, -=

Operands Output
int, int int

The subtraction operator subtracts two ints from each other. The subtraction-assignment operator, var -= [intlit], expands to var = var - [intlit].

Example:

int x = 6 - 5;  // x = 1
x -= 10;        // x = -9

Multiplication: *, *=

Operands Output
int, int int

The multiplication operator multiplies two ints together. The multiplication-assignment operator, var *= [intlit], expands to var = var * [intlit].

Example:

int x = 2 * 3;  // x = 6
x *= 2;         // x = 12

Division: /, /=

Operands Output
int, int int

The division operator divides two ints from each other. The division-assignment operator, var /= [intlit], expands to var = var / [intlit]. Since soup doesn't support floating-point numbers, both the division and division-assignment operators perform integer division, throwing away any decimal part or remainder accrued during the division. If the user desires to keep the remainder, it can be stored in a separate variable using the modulus or modulus-assignment operator, %/%= on the same operands.

Since division by zero is an illegal operation, using the division or division-assignment operator with the second operand 0 results in an error.

Example:

int x = 10 / 2;  // x = 5
x /= 5;          // x = 1

Modulus: %, %=

Operands Output
int, int int

The modulus operator returns the remainder from the division of the operands. The modulus-assignment operator, var %= [intlit], expands to var = var % [intlit].

Since division by zero is an illegal operation, using the modulus or modulus-assignment operator with the second operand 0 results in an error.

Example:

int x = 100 % 10;  // x = 0
x %= 41;           // x = 0

Relational Operators

Less than: <

Operands Output
int, int bool

The less than operator compares two operands and returns true if the first operand is strictly less than the second operand.

Example:

bool x = 3 < 5;    // x = true
bool y = 7 < 2;  // y = false

Less than or equal to: <=

Operands Output
int, int bool

The less than or equal operator compares two operands and returns true if the first operand is either less than or equal to the second operand.

Example:

bool x = 3 <= 5;  // x = true
bool y = 9 <= 9;  // y = true
bool z = 4 <= 1;  // z = false

Greater than: >

Operands Output
int, int bool

The greater than operator compares two operands and returns true if the first operand is strictly greater than the second operand.

Example:

bool x = 5 > 3;  // x = true
bool y = 2 > 7;  // y = false

Greater than or equal to: >=

Operands Output
int, int bool

The greater than or equal operator compares two operands and returns true if the first operand is either greater than or equal to the second operand.

Example:

bool x = 5 >= 3;  // x = true
bool y = 9 >= 9;  // y = true
bool z = 1 >= 4;  // z = false

Equality Operators

Equal to: ==

Operands Output
int, int bool
bool, bool bool

The equal to operator compares two operands and returns true if they are equal.

Example:

bool x = 1 == 1;         // x = true
bool y = 2 == 3;         // y = false
bool z = true == false;  // z = false

Not equal to: !=

Operands Output
int, int bool
bool, bool bool

The not equal to operator compares two operands and returns true if they are not equal.

Example:

bool x = 1 != 1;          // x = false
bool y = 2 != 3;          // y = true
bool z = false != false;  // z = false

Logical Operators

And: &&

Operands Output
bool, bool bool

The and operator evaluates two operators and returns a boolean according to the following truth table:

Operand 1 Operand 2 Output
true true true
false true false
true false false
false false false

The and operator is also 'short-circuiting', meaning that if the first operand evaluates to false, we already know that the output will be false. Therefore, the second operand is not evaluated at all, as there is no point in wasting the time to do so.

Example:

bool x = true && true;  // x = true
x = x && false;         // x = false

Or: ||

Operands Output
bool, bool bool

The or operator evaluates two operators and returns a boolean according to the following truth table:

Operand 1 Operand 2 Output
true true true
false true true
true false true
false false false

The or operator is also 'short-circuiting', meaning that if the first operand evaluates to true, we already know that the output will be true. Therefore, the second operand is not evaluated at all, as there is no point in wasting the time to do so.

Example:

bool x = true || true;  // x = true
x = x || false;         // x = true

Unary Operators

Negation: -

Operand Output
int int

The negation operator negates an int.

Example:

int x = -1;  // x = -1
x = -x;      // x = 1

Not: !

Operand Output
bool bool

The not operator negates a bool.

Example:

bool x = !true;  // x = false
x = !x;          // x = true