Programming Chapter 2
Author: Lucas Goss
Call the Operator
Note: The examples in this section use the following variables:
<csharp> string playerName; int playerLives; bool isPlayerAlive; </csharp>
An operator is a symbol used to operate on constants and variables. We already learned one operator, the assignment operator, but there are many others. I won't cover them all (use the language references), but a few common ones.
Code - Arithmetic Operators
Code - Parenthesis
We've already covered the assignment operator, but there are arithmetic assignment operators as well (+=, -=, *=, /=, %=). They are a shorthand way to do an arithmetic operation on the current value. For example:
Code - Arithmetic Assignment Operators
The '++' and '--' are increment and decrement operators, that increment and decrement a variable by 1.
Code - Increment/Decrement Operators
Conditional operators are a boolean type of operation. They're really important for conditional statements which we'll cover later. First there is the logical operators, AND (&&), OR (||) and NOT (!). Here's some truth tables using the logical operators:
Code - Logical Operators
isPlayerAlive = true || true; // = true isPlayerAlive = true || false; // = true isPlayerAlive = false || false; // = false
Another type of conditional operators are relational operators. These are EQUAL (==), NOT EQUAL (!=), LESS THAN (<), GREATER THAN(>), LESS THAN OR EQUAL (<=), and GREATER THAN OR EQUAL (>=):
Code - Relational Operators
And a third type of conditional operator is the ternary operator (?). It returns the first expression if true and returns the second expression if false as in "condition ? 1st expression : 2nd expression". For example:
Code - Ternary Operator
The '+' and '+=' operators are also used as shorthand for concatenation of strings.
Code - Concat Operators
TODO: index operator  TODO: cast operator ()
Another common operator is 'new'.
Code - New Operator
TODO: member access operator .