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.
1
int a = 10;
2
int b = 5;
3
int c = 0;
4
5
main {
6
7
// Assignment Operator
8
c = 5; // c is set to a value of 5
9
c += 5; // c is set to a value of 10 (5 + 5 = 10)
10
c -= 5; // c is set to a value of 5 (10 - 5 = 5)
11
c *= 5; // c is set to a value of 25 (5 * 5 = 25)
12
c /= 5; // c is set to a value of 5 (25 / 5 = 5)
13
c %= 5; // c is set to a value of 0 (5 % 5 = 0)
14
15
// Mathmatical Operators
16
c = a + b; // Addition c = 15
17
c = a - b; // Subtraction c = 5
18
c = a * b; // Multiplication c = 50
19
c = a / b; // Division c = 2
20
c++; // Increment c = 3
21
c--; // Decrement c = 2
22
23
// Logical Operators
24
if (c ! a) // NOT if c is not a
25
if (a && c) // AND if a and c are TRUE
26
if (a || c) // OR if a or c are TRUE
27
if (a ^^ c) // XOR if either a or c are TRUE but not both
28
29
// Relational Operators
30
if (c == 10) // EQUAL TO if c is equal to 10
31
if (c != 50) // NOT EQUAL TO if c is not equal to 50
32
if (c < 20) // LESS THAN if c is less than 20
33
if (c > 30) // GREATER THAN if c is greater than 30
34
if (c <= 40) // LESS THAN OR EQUAL TO if c is less than or equal to 40
35
if (c >= 40) // GREATER THAN OR EQUAL TO if c is greater than or equal to 40
36
}
Copied!
Assignment
Arithmetic
Logical
Relational
Basics
= 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.
1
a = 5; // a is set to 5
2
a += 5; // a is set to 15
3
a -= 5; // a is set to 5
4
a *= 5; // a is set to 50
5
a /= 5; // a is set to 2
6
a %= 3; // a is set to 1
Copied!
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.
1
a + b; // will give a value of 15
2
a - b; // will give a value of 5
3
a * b; // will give a value of 50
4
a / b; // will give a value of 2
5
a % b; // will give a value of 0
6
a++; // will give a value of 11
7
a--; // will give a value of 9
8
Copied!
Note, 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.
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 languauge.
Operator
Description
&&
AND operator, if both operators 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.
1
a && b // value is FALSE
2
a || b // value is TRUE
3
a ^^ b // value is TRUE
4
!b // value is TRUE
5
!a // value is FALSE
Copied!
Relational operators produce boolean results (TRUE or FALSE) while comparing two operands. The following tables list the relational operands 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 left then the condition becomes TRUE
<
Less than, if the left operand holds a value less than the left 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.
1
a == b // is FALSE
2
a != b // is TRUE
3
a > b // is TRUE
4
a < b // is FALSE
5
a >= b // is TRUE
6
a <= b // is FALSE
Copied!
Basics are the same as Logical, except they work with bits
Operators
Description
&
AND operator, if both operators 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
<<
Left Shift operator, Less than, if the left operand holds a value less than the left then the condition becomes TRUE
>>
Right Shift operator, Greater than, if the left operand holds a value greater than the left then the condition becomes TRUE
-
NOT operator, Subtracts right operand from the left operand
In the example below,
1
3 & 1 = 1
2
1 | 2 = 3
3
1 ^ 2 = *
4
1 << 1 = 2
5
1 >> 1 = 0
6
~ 1 = -32768
Copied!
Last modified 7mo ago
Copy link