“In the end, all business operations can be reduced to three words: people, product, and profits” Lee Iacocca
Operators are functions that perform computations on operands!
Operands are basically values.
For example, the * symbol is called multiplication operator and is used to multiply two values together.
Or the / symbol is called division operator and is used to divide values.
In short there are 5 categories of operators and in the next couple of sections we will dive deep in each of these categories:
| Name: | Example: |
|---|---|
| Arithmetic Operators | +, -, * etc. |
| Assignment Operators | =, -=, += etc. |
| Comparison Operators | <, >, <=, >=, == etc. |
| Logical Operators | &&, ||, ! |
| Bit fiddling operators (AKA bitwise operators) | <<, >>, ^, &, | |
But there are a couple of notes and concepts that we need to take a look at them now.
Operators like *, /, + etc. involve two values.
For example:
`2+2`;
Each of these values is called operand. An operand can be either a variable, a constant value (like 1, 2, 3 etc.) or a function call that returns a value.
Together operand and operators create expression.
Note: the keynote about expression is that it should return a value.
For example:
#include <iostream>
using namespace std;
double doubleReturn(){
return 2.4;
}
int main()
{
double result = 2 * doubleReturn();
cout<< result;
return 0;
}
Output:
4.8
In this example we’ve created the variable named result which is of type double and for its value we have the 2 * doubleReturn() expression.
The operands in this expression are 2 and the function call to the doubleReturn() which will end up being 2.4 after the result of the function returned. Also the operator of this expression is multiplication *.
As mentioned before, expressions always return a result. So the result of this expression will be assigned as the value of the result variable.
LValues and RValues:
When we work with assignment operators like =, the operand on the right side of the operator is called Rvalue and the operand on the left side of the operator is called LValue.
You should know that we can’t put constant values (like 1,2,3,4 etc.) or a function call on the left side of assignment operators.
The LValues should point to a memory-space that allows for storing a value. This makes variables the perfect candidate for LValues. This is because variables point to a memory space and allow values to be stored in there.
For example:
int jackSalary = 200000;
5000 = jackSalary;
The first assignment int jackSalary = 200000; is acceptable because the LValue is a variable that points to a memory space.
But the second assignment is error prone. This is because the value 5000 is a constant value and does not point to any memory location that allows for storing a value.
On the other hand, the RValues can be a constant value (like c, f, 10, 233.322 etc.), a variable, an expression that results a value or a function call that returns a value.
Example:
int jackSalary = 200000;
int ellenSalary = 160000;
int totalSalary = jackSalary + ellenSalary;