Operator Types
An operator is a symbol which tells the interpreter to perform specific mathematical, relational or logical operation and produce final result. This section details the operators available in GPC.
int a = 10;
int b = 5;
int c = 0;
main {
// Assignment Operator
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)
// Mathmatical 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 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
}
=
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 short hands 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
.
a = 5; // a is set to 5
a += 5; // a is set to 15
a -= 5; // a is set to 5
a *= 5; // a is set to 50
a /= 5; // a is set to 2
a %= 3; // a is set to 1
Last updated
Was this helpful?