-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
Operands | Output |
---|---|
int , int
|
int |
The addition operator adds two int
s together. The addition-assignment operator, var += [intlit]
, expands to var = var + [intlit]
.
int x = 1 + 2; // x = 3
x += 1; // x = 4
Operands | Output |
---|---|
int , int
|
int |
The subtraction operator subtracts two int
s from each other. The subtraction-assignment operator, var -= [intlit]
, expands to var = var - [intlit]
.
int x = 6 - 5; // x = 1
x -= 10; // x = -9
Operands | Output |
---|---|
int , int
|
int |
The multiplication operator multiplies two int
s together. The multiplication-assignment operator, var *= [intlit]
, expands to var = var * [intlit]
.
int x = 2 * 3; // x = 6
x *= 2; // x = 12
Operands | Output |
---|---|
int , int
|
int |
The division operator divides two int
s 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.
int x = 10 / 2; // x = 5
x /= 5; // x = 1
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.
int x = 100 % 10; // x = 0
x %= 41; // x = 0
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.
bool x = 3 < 5; // x = true
bool y = 7 < 2; // y = false
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.
bool x = 3 <= 5; // x = true
bool y = 9 <= 9; // y = true
bool z = 4 <= 1; // z = false
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.
bool x = 5 > 3; // x = true
bool y = 2 > 7; // y = false
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.
bool x = 5 >= 3; // x = true
bool y = 9 >= 9; // y = true
bool z = 1 >= 4; // z = false
Operands | Output |
---|---|
int , int
|
bool |
bool , bool
|
bool |
The equal to operator compares two operands and returns true
if they are equal.
bool x = 1 == 1; // x = true
bool y = 2 == 3; // y = false
bool z = true == false; // z = false
Operands | Output |
---|---|
int , int
|
bool |
bool , bool
|
bool |
The not equal to operator compares two operands and returns true
if they are not equal.
bool x = 1 != 1; // x = false
bool y = 2 != 3; // y = true
bool z = false != false; // z = false
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.
bool x = true && true; // x = true
x = x && false; // x = false
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.
bool x = true || true; // x = true
x = x || false; // x = true
Operand | Output |
---|---|
int |
int |
The negation operator negates an int
.
int x = -1; // x = -1
x = -x; // x = 1
Operand | Output |
---|---|
bool |
bool |
The not operator negates a bool
.
bool x = !true; // x = false
x = !x; // x = true