Part 06 - Operators
Mathematical
Name |
Syntax example |
Comments |
|---|---|---|
Multiplication |
|
|
Division |
|
|
Remainder |
|
Often called mod or modulus |
Addition |
|
|
Subtraction |
|
|
Exponent |
|
Do not confuse this with Bitwise Xor ^ |
Bitshift Right |
|
|
Bitshift Left |
|
|
Bitwise And |
|
|
Bitwise Or |
|
|
Bitwise Xor |
|
The mathematical operators can also be used in the syntax a <operator>= b, for example, a += b.
This is merely a shortcut for a = a <operator> b, or in our example a = a + b.
Relational and Logical
Name |
Syntax Example |
Comments |
|---|---|---|
Less Than |
|
|
Greater Than |
|
|
Less Than or Equal To |
|
|
Greater Than or Equal To |
|
|
Equality |
|
|
Inequality |
|
|
Logical And |
|
Only use when |
Logical Or |
|
Only use when |
Logical Not |
|
Only use when |
Types
Name |
Syntax Example |
Comments |
|---|---|---|
Typecast |
|
|
Typecast |
|
|
Type Equality/Compatibility |
|
|
Type Retrieval |
|
|
Type Retrieval |
|
Primary
Name |
Syntax Example |
Comments |
|---|---|---|
Member |
|
Classes are described in Part 08 - Classes |
Function Call |
|
Functions are described in Part 07 - Functions |
Post Increment |
|
|
Post Decrement |
|
|
Constructor Call |
|
Classes are described in Part 08 - Classes |
Unary
Name |
Syntax Example |
Comments |
|---|---|---|
Negative Value |
|
|
Pre Increment |
|
|
Pre Decrement |
|
|
Grouping |
|
Difference between Pre and Post Increment/Decrement
When writing inline code, Pre Increment/Decrement (+i/-i) commit the action, then return its new value, whereas Post Increment/Decrement (i+/i-) return the current value, then commit the change.
0 1 2 3 4 --- 1 2 3 4 5
| Recommendation To make your code more readable, avoid using the incrementors and decrementors. |
Exercises
- Put your hands on a wall, move your left foot back about 3 feet, move the right foot back 2 feet.
Go on to Part 07 - Functions
