Operators#

Introduction#

Operators in C++ are symbols that let you perform operations on variables and values. They include arithmetic, logical, comparison, assignment, and bitwise operators. C++ also supports increment/decrement, conditional expressions, and compound assignment. These form the core of most logic and math in any C++ program, including robotics control with VEXcode IQ (2nd gen).

Arithmetic#

Name

Symbol

Operator

Example

Addition

+

a + b

5 + 3   // 8

Subtraction

-

a - b

10 - 3  // 7

Multiplication

*

a * b

5 * 3   // 15

Division

/

a / b

10 / 4  // 2 (integer division)

Modulo

%

a % b

10 % 4  // 2

Negation

-

-a

-5      // -5

Augmented Arithmetic#

Name

Symbol

Operator

Example

Addition

+=

a += b

int a = 5;
int b = 3;
a += b; // a = 8

Subtraction

-=

a -= b

int a = 10;
int b = 4;
a -= b; // a = 6

Multiplication

*=

a *= b

int a = 2;
int b = 3;
a *= b; // a = 6

Division

/=

a /= b

int a = 10;
int b = 4;
a /= b; // a = 2

Modulo

%=

a %= b

int a = 10;
int b = 3;
a %= b; // a = 1

Comparison#

Name

Symbol

Operator

Example

Equality

==

a == b

5 == 5    // true

Inequality

!=

a != b

5 != 10   // true

Greater Than

>

a > b

10 > 5    // true

Less Than

<

a < b

5 < 10    // true

Greater Than or Equal

>=

a >= b

10 >= 10  // true

Less Than or Equal

<=

a <= b

5 <= 5    // true

Logical#

Name

Symbol

Operator

Example

Logical AND

&&

a && b

true && false   // false

Logical OR

||

a || b

false || true  // true

Logical NOT

!

!a

!true           // false

Assignment#

Name

Symbol

Operator

Example

Assignment

=

a = b

int a = 10;

Bitwise#

Name

Symbol

Operator

Example

Bitwise AND

&

a & b

6 & 3   // 2 (0b110 & 0b011)

Bitwise OR

|

a | b

6 | 3   // 7 (0b110 | 0b011)

Bitwise XOR

^

a ^ b

6 ^ 3   // 5 (0b110 ^ 0b011)

Bitwise NOT

~

~a

~5      // -6

Left Shift

<<

a << b

2 << 3  // 16

Right Shift

>>

a >> b

16 >> 2 // 4

Augmented Bitwise#

Name

Symbol

Operator

Example

Bitwise AND

&=

a &= b

int a = 6;
a &= 3;  // a = 2

Bitwise OR

|=

a |= b

int a = 6;
a |= 3; // a = 7

Bitwise XOR

^=

a ^= b

int a = 6;
a ^= 3;  // a = 5

Left Shift

<<=

a <<= b

int a = 5;
a <<= 2; // a = 20

Right Shift

>>=

a >>= b

int a = 20;
a >>= 2; // a = 5

Ternary (Conditional)#

Name

Syntax

Example

Conditional Operator

condition ? expr1 : expr2

int a = 6;
int b = 3;
int max = (a > b) ? a : b;

Increment / Decrement#

Name

Symbol

Operator

Example

Pre-Increment

++

++a

int a = 5;
int b = ++a;
// a = 6, b = 6

Post-Increment

++

a++

int a = 5;
int b = a++;
// a = 6, b = 5

Pre-Decrement

--

--a

int a = 5;
int b = --a;
// a = 4, b = 4

Post-Decrement

--

a--

int a = 5;
int b = a--;
// a = 4, b = 5