Hello, and welcome back. We're into a new section.
In this section, we're going to talk about operators. So I wanted to just give you a quick overview of
a couple terms and tell you what operators are.
And then we're going to dive right into a lot of examples
of the different operators.
So operators use functions that use a symbolic name.
And they perform all sorts of different actions in the program itself.
The most common types of things you do is you perform mathematical or logical functions,
so you do arithmetic type stuff. 5 plus 9,
3 plus 8 or you compare things
like is this number or variable greater than this variable.
And instead of actually calling a
specific routine like a printf or a scanf, you don't call a function.
You just use a symbol. You just use like a character,
like the less than symbol or the plus symbol. Those are called operators.
So operators are predefined in c, just like other languages have this concept of operators.
And most operators tend to be combined with the infix style.
And all that means is that the operators in between
the data.
So I mentioned that operators can perform mathematical functions,
just 5 plus 8.
Plus is in the middle between 5 and 8.
And that's referred to as infix style. You can actually do like plus 5 8, that's
considered prefix style.
And then postfix style would be the operator is after the arguments.
So 5 8 plus, for example. But we're most
familiar with how you do math in high school and everywhere else,
the actual operator goes in between the arguments, which is referred to as the operand.
So operators and operands, you want to think of those 2 terms.
Operator is the actual symbol
and operand are the arguments to the symbol that are usually on the left and right-hand side.
But again, it depends on the actual operator.
So we're going to talk about logical operators. These are sometimes called
the Boolean operator because they return true or false values.
And we know what a Boolean is, Boolean variable at least.
So it's an operator that returns a Boolean result, true or false that's based on the Boolean
Boolean result of one or 2 other expressions.
And so we've also been hearing this term expression a lot. So I wanted to formally define that as well.
We'll do that in a second, and how it's a little bit different than a statement.
Another example of an operator would be arithmetic operators, as I mentioned,
plus minus, 2 operands on the left and right-hand side, you perform some calculation.
Other operators include assignment where you're assigning data.
And we've done this in previous previous challenges and
in previous lectures.
And this is the most common thing, that's the equals operator.
And then there's things like relational operators, which are really comparison operators, less than,
not equal, that's used with the exclamation point.
And then bitwise operators, where you can perform operations on bits,
and we'll talk about that as well.
This is just designed to give you a little introduction to what an operator is and how can be very useful.
Now in order to really use operators, you have to understand a couple different terms.
I've been using these terms kind of casually
expressions and statements but I haven't formally defined them.
And it's important to know the difference because if you read or
learn about anything in programming, you're going to hear these 2 terms.
And you may have been brushing them off and maybe you understand what they are,
but you should know there are specific differences. Usually,
you can identify the differences
for the most part statements end in semicolons expressions don't and expressions return values
statements don't necessarily.
But let's go through a formal definition.
Statements are the basic programming steps of c.
Most statements are constructed from expressions, so you see a combination.
You may see an expression inside of a statement. And I'll give an example of that.
So an expression consists of a combination of operators and operands.
And now that we know what an operator is formally and we know what an operand is,
operand is just the arguments to the operator,
together that makes an expression. We can now actually say what an expression is.
Operands are what an operator operates on, I mentioned that they're the arguments.
Operands can be constants, variables or combinations of the 2,
so it can be 5 plus 8, it can be variable x plus y
or it can be variable x plus 8.
Every expression has a value, and expressions generally return values.
So here are some examples of expressions,
negative 6 minus.
Minus is an actual operator in c. It's a unary operator,
single operator with a single argument.
And then 4 plus 21 where the operator is plus and it's in between
2 constants.
This is an expression.
And then another expression it's a little more complicated,many operators, the star for multiplication,
the plus for addition and then the forward slash for division.
And then you see an assignment which is q equals 5 times 2,
that's where you have an expression 5 times 2
on the right-hand side you have a variable on the left-hand side, together those make up a statement.
Another one is x equals plus plus q percent 3,
we'll talk about those plus plus as an incrementation operator.
Percent is a modulus operator for remainder.
And then a comparison operator q is greater than 3, a variable q is greater than 3.
But these are examples of expressions.
Now statements,we've mentioned that they're the building blocks of a program,
but there's different types of statements. We've already learned how to declare variables.
That's actually a statement, that's a declaration statement.
So a program is going to use a series of statements with special syntax ending with a semicolon.
Those are referred to as simple statements.
Statements that are single lines that end with a semicolon.
You can also have compound statements which I'll tell you in a second.
But a statement is essentially instruction to the computer.
So declaration statement, data type followed by variable name and then a semicolon.
That's an example.
An assignment statement, Jason equals 5.
It uses the assignment operator. It's not an expression.
It's just Jason equals 5.
A function call statement. When you call a function printf,
that's the one function we know. And we call that Jason pass it.
Structure statement, we haven't learned this syntax yet or this keyword while,
but this is a loop where it has a single statement on a single line ending with a semicolon.
A return statement is when we return data from a function.
Again, we don't fully understand what this means,
but you see it ends in a semicolon. These are different types of statements.
So c considers any expression to be a statement if you append a semicolon.
So that's why some of this looks a little tricky. When you see Jason equals 5,
you might have thought initially that's an expression because it has the assignment operator.
But because it has a semicolon, it's actually referred to as a statement.
Again not necessarily super important to know, but you probably see these terms all over the
place and you're going to be just asking yourself what expression, why they keep saying expression.
So expression statements
that end in semicolon or statements themselves
or referred to as expression statements.
So in c, it's that perfectly legal to just have a number and a semicolon.
Even though it doesn't do anything and is wasteful, but it is okay and same as 3 minus 4,
semicolon. That's an expression normally,
but it ends in a semicolon so it's an expression statement but you're not doing anything with it,
you're not assigning it, but it is perfectly valid, but
no reason to do those 2 examples.
Compound statements a little different than a single statement
where you just have a semicolon at the end.
These are usually inside blocks of code.
And I tried to tell you what a block of code is previously where you have the brackets.
But this is where you just have multiple statements and then they group them together by
basically braces. And that's referred to as a block.
So an example that would be something like this, int index equals 0,
that's a single statement, end with a semicolon.
Well
while index is less than 0, this is a while loop. You don't have to understand what that means,
but what you should notice here is these braces.
A starting brace is on the left
and left braces to start an ending brace is on the right or at the end
which is a right braces.
It looks like the main function where the main routine
has a left and right braces. And I mentioned that's a block of code.
So a block of code is just multiple statements,
but that's referred to as a compound statement as well.
And we'll be talking about blocks and how you can use these braces for while statements, looping
and if conditionals and so forth. But again, a quick overview,
we're now going to go into some examples of specific operators that are supported and how to use them.
Thank you.