Hey, everyone.
Today we will be going over expressions and operators.
First and foremost, let’s define these terms.
An expression is a series of operators and operands.
What are operators and operands?
Operators are basically symbols that represent an operation which returns a single result.
So plus, minus, division, multiplication, assignment, equality checks, all those are
examples of operators.
And operands are the inputs for your operators.
Operands are data elements that are used as inputs for operators.
5-10 is an expression, and it’s made out of operators and operands. 5 and 10 would
be the operands and ‘-‘ would be the operator.
So 5 and 10 are the operands that are inputs for your ‘-‘operator in order for the
operation to happen.
And once the operation happens, you will get a single result.
One more thing is that expressions can be combined into more complex expressions.
The way you combine expressions is through operators.
You might be asking how do we get a result back.
That happens through evaluation.
Your expressions will be evaluated down to a single result.
Evaluation is pretty much the process of applying operators to operands in the proper sequence.
The key is proper sequence.
In this video, I will not be going over the way the proper sequence works, but it’s
good to keep in mind that there is a proper sequence when your expression gets evaluated
down to a single result.
In the future videos, I will be showing you the actual sequence.
But for the sake of simplicity, I just want to show you how expressions work and how you
can combine expressions together, and when you get value, what to do with it, and how
you can assign that value to a variable, which we already went over, what variables are,
how to use them, why they exist.
I want to show you all that so you can combine these two new concepts together.
And later down the line, we will definitely go over the sequence and how your expressions
get evaluated.
Without further ado, let’s go ahead and create a new project.
Let’s call it ExpressionsAndOperators.
You already know the Solution Explorer.
I went over the solutions, projects, and what they mean.
This is the Main method.
This is where the execution of our program begins.
Let’s create a variable called simpleExpression1.
int simpleExpresssion1 = 5 - 10; Here, 5 and 10 are the operands for the operator
‘-‘. And then this gets evaluated down to result.
Once that happens, you then assign the result of that to simpleExpression1 variable.
How does that happen? ‘=’ is called the assignment operator.
This assignment operator takes in two operands.
What are those two operands?
The operand on the left is the variable declaration.
We already went over that, the type, the variable name.
It takes the left side and it takes the right side.
These are the inputs for the assignment operator.
And what the assignment operator does when it gets evaluated is it assigns the value
from the right side to its left side.
That’s what happens.
You have your operator1 (-) and your operator2 (=) here.
Now we can go ahead and create another expression: int simpleExpression2 = 4 * 10;
And pretty much the same thing will happen.
Let’s create another variable.
int complexExpression = (((6*7) - 4) + simpleExpression1) / simpleExpression2;
The numbers don’t matter.
None of this matters.
It’s just that I want you to understand how the evaluation process works and how you
can use the variables in this scenario and how you can use other variables in a complex
expression.
As to what these numbers mean, they mean nothing.
In this case, there’s no special meaning.
There’s not a formula.
So don’t pay attention to that.
This is just for the sake of the example.
The above statement looks a bit more complex.
This right here shows you that you can get the
value in a variable using the assignment operator.
And you have you expression here.
You put those in a variable.
And you can use those variables in another expression.
And you can use this variable in another expression.
You can see that this is pretty powerful.
Let’s print out this variable.
Console.WriteLine(simpleExpression1); Console.WriteLine(simpleExpression2);
Console.WriteLine(complexExpression);
And we can run the program.
Let’s put the break point right here and run the program.
Following is the output: -5
40 0
The value returned from simpleExpression1 is -5, simpleExpression2 is 40, and complexExpression
is 0.
The reason why this is 0 is that we have our type as int.
We can actually change that to decimal.
Let’s do that.
decimal simpleExpresssion1 = 5 - 10; decimal simpleExpression2 = 4 * 10;
decimal complexExpression = (((6*7) - 4) + simpleExpression1) / simpleExpression2;
Console.WriteLine(simpleExpression1); Console.WriteLine(simpleExpression2);
Console.WriteLine(complexExpression); We now should get result back.
Let’s run this.
-5 40
0.825 complexExpression is 0.825.
Those are not related to the expressions and operators, but just wanted to show you that
that was the reason why we got a 0 back as a result.
That’s it.
These are the basics of expressions and operators.
And later down the line, we’ll go over the proper sequence in which they get evaluated.
But for now, that’s it.
I hope you enjoyed the video.
And please, don’t forget to subscribe.