One of the key concepts of any computer program written in any programming is language is that every processes done by computer programs were commonly consists of both logical and mathematical operations. Because of that, operators does the job.
Operators are symbols that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:
Assignment Operator
Arithmetic Operator
Relational Operator
Logical Operator
Placing or assigning a value in a variable is an easy way through the use of assignment operator (=) which takes in the following form:
variable = expression;
To evaluate, first the expression on the right-hand side of the equal sign is evaluated, and then the variable on the left hand side of the equal sign is set to this value.
A variable is said to be initialized the first time a value is placed in the variable.
A C++ statement such as:
means 5 will be stored in a variable y
means add 2 to the value of the variable y (which is 5) and assign the new value to the variable x. Hence, x will yield a value of 7
Examples of Variable Initialization
int x = 10;
double pi = 3.14;
char ans = 'y';
string name = "Mary";
Adding, multiplying every data handled by the program can be done using arithmetic operators. The following table is a table consists of the list of arithmetic operators with their meaning and example:
Here is a sample video tutorial demonstrating how to use arithmetic operators in C++ program:
Increment and Decrement Operators
Increment (++) and Decrement (--) operators are placed either before or after the operand. The statements:
++x; or x++;
--y; or y--;
means x is incremented by 1 and y is decremented by 1, respectively.
When used in assignment statements, however placing the value after the value takes a different meaning. Consider the following statements:
x = 5;
y = x++;
z = y-- * 2;
In the assignment statement for the variables y and z, the increment and decrement operators are placed after the variable operands. As such, the operators are called post-increment and post-decrement operators, respectively. This means that the variable operands will be incremented/decremented only after the expressions having been fully evaluated.
x = 5;
//x equals 5
y = x++;
//x is still equal to 5, y will be equal to 5. x will be equal to 6 after evaluating y
z = y-- * 2;
//y is still equal to 5, z will be equal to 10 (5 * 2). y will be equal to 4 after evaluating z
But in the following statement:
x = 5;
y = ++x;
z = --y * 2;
When the increment/decrement operator is placed ahead of the variable operand, it is considered a pre-increment and pre-decrement operators, and the increment/decrement is executed before anything else is done with the variable.
x = 5;
//x equals 5
y = ++x;
//x is equal now to 6, y will be equal to 6
z = --y * 2;
//y is is equal now to 5, z will be equal to 10 (5 * 2)
Compound Operators (Compound Assignments)
Compound assignments can be used when you want to modify the value of a variable by performing an operation on the value currently stored in that variable. (For example: A = A + 1 ). This is a shorthand notation that combines the assignment operator (=) and an arithmetic operator.
Here is a sample video tutorial demonstrating how to use increment/decrement and compound operators in C++ program:
Two expressions can be compared using relational and equality operators. For example, to know if two values are equal or if one is greater than the other. The result of such an operation is either true or false (i.e., a Boolean value)
The relational operators in C++ are:
Boolean Expressions
A Boolean expression is any expression that is either true or false.
(7 == 5) //evaluates to false
(5 > 4) //evaluates to true
(3 != 2) //evaluates to true
(6 >= 6) //evaluates to true
(5 < 5) //evaluates to false
Of course it's not just numeric constants that can be compared, but just any value, including the variables.
Suppose that a = 2, b = 3 and c = 6, then:
(a == 5) //evaluates to false, since a is not equal to 5
(a * b >= c) //evaluates to true, since (2 * 3 >= 6) is true
(b + 4 > a * c) //evaluates to false, since (3 + 4 > 2 * 6) is false
Here is a sample video tutorial demonstrating the use of relational operators in C++ program particularly in dealing with conditions:
They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under consideration. They are described below:
Logical AND operator: The && operator returns true when both the conditions under consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
Logical OR operator: The || operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.
Logical NOT operator: The ! operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a = 0.
Suppose that a = 5, b = 6 and c = 3, then:
((a == 5) && (b > 6)) //evaluates to false (true && false)
((a == 5) || (c > 8)) //evaluates to true (true || false)
!(a == 5) //evaluates to false because the expression at its right (a == 5) is true
Here is a sample video tutorial demonstrating the use of logical operators in C++ program particularly in dealing with conditions with considerations:
It is possible to build mathematical and logical expressions combined together using several operators.
Order of precedence is a collection of rules that specifies in which operations need to be performed in a given expression.
Certain operators have higher precedence that others. For example, multiplication operator has a higher precedence than the addition operator. In this expression:
int x = 10 + 3 * 8;
variable x is assigned 34, because the operator ( * ) has a higher precedence than operator ( + ), so it first gets multiplied with 3 * 8 then adds into 10.
The table below is the list of order of precedence:
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. In the expression, all the binary operators except assignment operators are evaluated from left to right. To avoid confusion, programmers used parentheses () to group expressions.
Precedence rules can be overridden by explicit parentheses (). These can be used to group items in an expression. Parentheses are used to tell the computer which operations to perform first. For example, consider the ff. two expressions with different positioning of their parentheses:
int a = 10, b = 3, c = 8, ex1, ex2;
ex1 = (a +b) * c; //example 1, result is 104
ex2 = a + (b * c); //example 2, result is 34
To evaluate the first expression, the computer first adds variable a and b and multiply the result by variable c. To evaluate the second expression, it multiplies variables b and c and then adds the result to variable a. When these two (2) expressions are evaluated, they produce different results.
Here's a video that shows how to deal with operator precedence in C++ program: