Prog 1 Module Lesson 5
Prog 1 Module Lesson 5
UNIT 5: OPERATORS
Introduction
Operators are special symbols use to perform operations on variables and constant
values. For example, a plus sign ( + ) is used for adding values. Operators can be used along
with some variables and constant. Since, almost all computer programs perform calculations it
is important to know how to make the right formulas with the use of operators available in C++
and their precedence.
Learning Objectives
Course Materials
Now that we know what are variables and constants, we can begin doing some operations
with them with the use of operators.
Assignment operators
Arithmetic operators
Comparison operators
Logical operators
The equal sign ( = ) is used to assign a value to a receiving variable. It stores the result of
the operation on the right side of the equal sign.
Examples
32
Operators
taxiFareRate
Expression Equivalent to …
a += b; a = a + b;
x -= 10; x = x – 10;
c *= m; c = c * m;
y /= 5; y = y / 5;
d %= 3; d = d % 3;
m *= a + 5; m = m * (a + 5);
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
The last one, modulo operator, represented by a percentage sign (%), gives the remainder
of a division of two values. For example:
x = 15 % 2;
results in variable x containing the value 1, since dividing 15 by 2 is equal to 7, with a remainder
of 1
33
Operators
The increment operator (++) and the decrement operator (--) increase or reduce by one
the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
are all equivalent in its functionality; the three of them increase by one the value of x.
It can be used both as a prefix and as a suffix. That means that it can be written either
before the variable name (++x) or after it (x++). This will have an effect if used with other
variables, constants or operators.
Examples:
a = 5; a = 5;
b = a++; b = ++a;
a =5; a = 5;
b = a; a = a + 1;
a = a + 1; b = a;
5.4 Relational or Comparison Operators (==, !=, >, <, >=, <=)
Relational operators are used to compare two values. The return value of a comparison is
either true (1) or false (0).
Operator Description
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Examples:
(8 == 5) // evaluates to false
(5 > 3) // evaluates to true
(3 != 9) // evaluates to true
34
Operators
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
Logical operators are used to determine the logic between variables or values. The return
value of a comparison is either true (1) or false (0). It is used together with relational operations.
The operator ! in C++ has only one operand, to its right, and inverts it, producing false if
its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean
value of evaluating its operand.
Examples:
The logical operators && and || are used when evaluating two expressions to obtain a
single relational result. The operator && corresponds to the Boolean logical operation AND,
which yields true if both its operands are true, and false otherwise. The following panel shows
the result of operator && evaluating the expression a&&b:
35
Operators
The operator || corresponds to the Boolean logical operation OR, which yields true if either
of its operands is true, thus being false only when both operands are false. Here are the
possible results of a||b:
|| OPERATOR (or)
A b a || b
True true true
True false true
False true true
False false false
Examples:
If there are multiple operators in a single expression, from the left to right the operators
with higher precedence is evaluated first.
Example
:
X = 12 – 5 * 2 ; // Answer: 2
If you wish to evaluate 12 – 5 first, then we must enclosed them with parentheses.
X = (12 – 5) * 2 ; // Answer: 14
Note : When an expression has two operators with the same precedence /
level, grouping determines which one is evaluated first: either left-to-right or right-to-left.
36
Operators
From greatest to smallest priority, C++ operators are evaluated in the following order:
37
Operators
Activities:
1. a = 6;
b = 10;
c = ++a * b--;
3. x = 3;
y = 7;
z = 8;
z += (x*y); x = ________ y = _______ z= _______
4. pupcet = 80;
hsAve = 78;
public = true;
5. 100 % 32 * 5 + 4 = _______
________________________________________________
________________________________________________
________________________________________________
_________________________________________________
10. Ask whether BRAND is “UNIQLO” and PRICE is less than 1000.
___________________________________________________
38
Operators
Online References
https://www.w3schools.com/cpp/cpp_operators.asp
https://www.tutorialspoint.com/cplusplus/cpp_operators.htm
http://www.cplusplus.com/doc/tutorial/operators/
39