Chapter 5: C++ Operators
Chapter 5: C++ Operators
Objectives:
a.) Know the difference of integer division and the use of
modulus
b.) Know how to combine mathematical and assignment
operators
c.) Create a C++ program with application of operators.
OPERATORS
Once we know of the existence of variables and constants, we can begin to operate with
them. For that purpose, C++ integrates operators. Unlike other languages whose operators are
mainly keywords, operators in C++ are mostly made of signs that are not part of the alphabet but
are available in all keyboards. This makes C++ code shorter and more international, since it relies
less on English words, but requires a little of learning effort in the beginning.
You do not have to memorize all the content of this page. Most details are only provided
to serve as a later reference in case you need it.
Page 1
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
An operator is a symbol that cause the compiler to take an action. Operators act on operands,
and in C++ all operands are expressions. In C++ there are several different types of operators.
These are:
o mathematical operators (+ , - , / , * )
o relational operators ( true or false statement)
o logical operators ( and, or, not )
This statement assigns the integer value 5 to the variable a. The part
a = 5;
at the left of the assignment operator (=) is known as the lvalue (left
value) and the right one as the rvalue (right value).
The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the
result of an operation or any combination of these.
The most important rule when assigning is the right-to-left rule: The assignment operation always
takes place from right to left, and never the other way:
Consider also that we are only assigning the value of b to a at the moment of the assignment
operation. Therefore a later change of b will not affect the new value of a.
+ addition
Operations of addition, subtraction, multiplication and
- subtraction division literally correspond with their respective
mathematical operators. The only one that you might not
* multiplication
be so used to see is modulo; whose operator is the
/ division percentage sign (%). Modulo is the operation that gives
% modulo
the remainder of a division of two values.
Page 2
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
Integer division is somewhat different from everyday division. When you divide 21 by 4, the result
is a real number (a number with fraction part), integers don’t have fractions, and so the
“remainder” is looped off. The answer is therefore 5. To get the remainder, you take 21 modulus
(21 % 4) and the result is 1. The modulus operator tells you the remainder after an integer division.
Compound Assignments
(+=,-=, *=, /=, %=, <<=, >>=, &=, ^=, |= )
When we want to modify the value of a variable by performing an operation on the value
currently stored in that variable we can use compound assignment operators:
expression is equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
1 c++;
2 c+=1;
3 c=c+1;
➢ https://www.youtube.com/watch?v=AZvKbSdvoRU
➢ https://www.youtube.com/watch?v=RP3BWoep69U
➢ https://www.youtube.com/watch?v=mS9755gF66w
REFERENCES
❖ https://www.w3schools.com/cpp/cpp_operators.asp
Page 3
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
ACTIVITY:
#include<iostream.h>
#include<conio.h>
main()
{
int a, b;
a = 53;
b = 5;
clrscr();
cout<<”QUOTIENT IS : "<<a/b;
cout<<”REMAINDER IS : ”<<a%b;
getch();
}
REFLECTION:
A. I understand that:
CHAPTER QUIZ:
A. Suppose that a = 2, b = 3 and c = 4 evaluate the given expression below. Write the
word TRUE if the expression evaluates true otherwise write FALSE.
1 point for correct answer and 1 point for the solution.
EXPRESSION EVALUATION
1 ( a > b ) || ( c ! = c )
2 ( c != 4 ) && (b < a )
3 ( a == 2 ) || ( ! ( c > c)
4 ( b != b ) && ( c>c)
5 ( c <6) || (a==a)
6 ( b >=b) || (c !=b)
7 ( 6 >= 6 ) && ( a == 1 )
8 ( 9 == b ) || ( ! (c = 9 )
9 ( c <= 5 ) && ( c!=b )
10 ! (a==b) || ( b >=5)
Page 4