Operators are basically just instructions to DO stuff. The ones we’ve seen so far are math operators:
//Assignment operator
int i = 5;
//Another assignment
i = 7;
//Assignment and addition operators
int x = i + i + i;The operators above have operands, the things they DO WORK on. Let’s look at some common operators!
Incrementing an integer variable
int i = 5; //assignment
//All of these lines do the same thing:
i = i + 1; //Line 1
i += 1; //Line 2
++i; //Line 3
i++; //Line 4Result
Each of the lines of code above do the same thing. On each line, the value of the integer variable
iis increased by 1. After all of this code has run, the value ofiwill be 9.
Decrementing an integer variable
int i = 10; //assignment
//All of these lines do the same thing:
i = i - 1; //Line 1
i -= 1; //Line 2
--i; //Line 3
i--; //Line 4Result
Each of the lines of code above do the same thing. On each line, the value of the integer variable
iis decreased by 1. After all of this code has run, the value ofiwill be 6.
Modulo
| Division (this is NOT modulo) | Modulo |
|---|---|
| 7 / 3 = 2 | 7 % 3 = 1 |
| 6 / 4 = 1 | 6 % 4 = 2 |
| -8 / 3 = -2 | -8 % 3 = -2 |
| 21 / 7 = 3 | 21 % 7 = 0 |
| 1 / 5 = 0 | 1 % 5 = 1 |
The modulo operator looks like this:
//It's this guy below
%It is used to get the remainder of division:
int x;
//The value of x after the code below runs is 0
//because 3 divides 3 evenly
x = 3 % 3;
//The value of x after the code below runs is 1
//because 4/3 gives a remainder of 1
x = 4 % 3;
//The value of x after the code below runs is 2
//because 5/3 gives a remainder of 2
x = 5 % 3;
//The value of x after the code below runs is 0
//because 3 divides 6 evenly
x = 6 % 3;
//The value of x after the code below runs is 1
//because 7/3 gives a remainder of 1
x = 7 % 3;You seeing the pattern here? The pattern for anything modulo 3 is to cycle through 0, 1, and 2. There are 3 different possible answers, starting at 0 and going to 2.
This same pattern exists for any positive integer number. For example, anything modulo 5 will cycle through the numbers 0 to 4:
int z;
//0
z = 25 % 5;
//1
z = 26 % 5;
//2
z = 27 % 5;
//3
z = 28 % 5;
//4
z = 29 % 5;
//0
z = 30 % 5;What is modulo useful for? At some point you might want to alternate between two values repeatedly. You could increment an integer and modulo it by 2:
int k = 0;
while(true) //the loop condition isn't important here, ignore it
{
// The value of x will alternate between 0 and 1
int x = k % 2;
++k;
}You could also accomplish this task by using a boolean variable and swapping it between true and false, but the modulo way works too!
There are a few other tricks regarding modulo that, if you make further headway in your programming journey, you might find useful… but that’s all I’ll say for now!
Literal Values
You might have noticed that for some code we can choose to use numbers, OR we can choose to use variables.
int x = 8;
//Printing out the value of an integer variable
Console.WriteLine("Value of x is:" + x);
//Printing out an integer literal
Console.WriteLine("This is silly, but we'll print the 10 here: ", + 10);When we use “raw values” in our code, they are called literals, or literal values. Below are examples of literals of different data types:
//int literal values
2
89
0
500025748
//float literal values
-472.45516f
2000.0000f
0.000008733f
//char literal values
'A'
'b'
'$'
' '
'\n'
//bool literal values (there's only two)
true
falseThis shouldn’t be a mind-blowing revelation; this is only meant to explain another concept about the C# language. If you see a value hanging out in your code that is NOT a variable, chances are that it is a literal value of some kind.
Expressions
When we write a line of code in C#, we’re most often creating something called an expression. Understanding expressions is key to understanding what a line of code is actually accomplishing.
Expressions are just operators and operands.
For example, look at the following lines of code:
int Health = 10;
Health = Health + 20;
OK, it’s relatively easy to see what’s going on here. But what about this:
int a, b, c, d, e;
e = a * b * (c + d);
int i, j, k, u, v, w, x, y, z;
z = ((x + y) + (w * v * u)) / (j - k + i);
Those last couple lines are pretty long! How do we translate this C# code into a running program?
Recall that after we are done writing C# code, it must be converted into machine code that can be run by our computer. To do this, we COMPILE our code; it is handed to the compiler, which attempts to translate our C# code into machine code, represented by an actual program (.exe).
The compiler breaks down our lines of code like so:
- Identify all tokens
- Sort tokens into operators and operands
- Convert operands into other types (if possible)
- Resolve each operator according to precedence
What does this all mean? We’ll walk through the steps together!
Tokenization
When the compiler encounters an expression, it is broken down into individual tokens.
Tokens are the smallest elements of a programming language, used to represent things in that language.

Semicolons denote the end of expressions! We’ve seen those before! The compiler also tries to figure out if a token is valid, like a variable name or operator.
Operators and Operands
The compiler sorts the tokens into operators and operands! Variables and literals are operands (things that are WORKED UPON) and things like ’+’, ’-’, and ’=’ are operators (things that DO WORK).
Operands: data Operators: manipulate data (instructions!)

There are unary, binary, and ternary operators (which operate on one, two, or three operands). The vast majority of our operators will be binary, but we’ll use a few unary operators.
Type Conversion
The compiler may need to convert some values into different types. Remember how we talked about what kind of work is attractive to our CPU? It’s good at performing operations on things of the same type! So, the compiler is usually smart enough to know about converting types, when needed:
int three = 3;
float decimals = 0.14159f;
//Value stored in myPi will be 3.14159f
float myPi = three + decimals;In the example above, what is happening? The compiler is doing an implicit conversion; it knows how to do this conversion without any input from the programmer.
But sometimes the compiler isn’t so smart:
float x = 1234.898f;
int a = 0;
a = x;What happens when we do this?
We can prevent issues by doing the following:
float x = 1234.898f;
int a = 0;
//Value of a is now 1234
a = (int)x;This is called CASTING. We are casting the float variable x to an integer; we want to interpret its value as an integer for the purpose of assigning to a.
Operator Precedence
Remember order of operations, from highschool math? Yeah… it’s like that!
3 + 4 * 2 //11
3 + (4 * 2) //Also 11
(3 + 4) * 2 //14
-4 + 7 //3
-(4 + 7) //-11TLDR; Follow the PEMDAS way like in highschool math and you should be fine:
Parentheses, Exponent, (Multiplication, Division), (Addition, Subtraction)
Remember also (from math) that operators have associativity; if operators have the same precedence, then they’re resolved based on associativity, usually LEFT to RIGHT.
2 * 6 / 4 //Answer is 3You don’t need to memorize the image below, but it’s a nice reference:
