You are on page 1of 15

O P E R ATO R S

AND
EXPRESSIONS
IN C++
OBJECTIVES
By the end of this learning guide module, the students should be able to:
1. understand simple C++ statements involving arithmetic expressions and assignments;
2. write arithmetic expressions;
3. apply the precedence rule in computing arithmetic statements;
You may have used the GWA calculator before to compute for your GWA to know if you made it to the
Director’s List or not. But have you wondered how the computer solves this? If you want to create your own
GWA calculator in C++, you’ll need to learn first the concepts of arithmetic operators and how they are used in a
program.
O P E R AT O R S
• Operators are the foundation of any programming language. We can define operators as symbols that help us to
perform specific mathematical and logical computations on operands. In other words, we can say that an operator
operates the operand.

3 Classification of Operators
• 1. Arithmetic Operator
• 2. Relational Operators
• 3. Logical Operators
Arithmetic operators in C++ are symbols that tell the compiler to perform specific
mathematical manipulations while an expression refers to anything that evaluates to
a numeric value.

The standard arithmetic operators available in C++ include:


Each of these operators works as you probably expect.
Examples:
amount = 4 + 8;
The addition operator returns the sum of its two operands. The variable amount will
be assigned the value 12.
temperature = 112 - 14;

The subtraction operator returns the value of its right operand subtracted from its
left operand. This statement will assign the value 98 to temperature.
interest = 12 * 0.25;
The multiplication operator returns the product of its two operands. The variable interest is
assigned the value 3.
points = 100 / 20;
The division operator returns the quotient of its left operand divided by its right operand.
The variable points are assigned the value 5.
Note: When both of the division operators’ operands are integers, the result of the division will
also be an integer. This is called an integer division. If the result has a fractional part, it will be
thrown away.
remainder = 17 % 3;
The modulus operator, which only works with integer operands, returns the remainder of an
integer division. This statement assigns 2 to the remainder.
Integer Division
As mentioned earlier, when both operands of a division statement are integers, the statement will result in integer
division. This means the result of the division will be an integer as well. If there is a remainder, it will be
discarded. For example, look at the following code:

double number;
number = 5 / 2;
This code divides 5 by 2 and assigns the result to the number variable. What will be stored in number? You would
probably assume that 2.5 would be stored in number when you divide 5 by 2. However, that is not what happens
when the previous C++ code is executed. Because the numbers 5 and 2 are both integers, the fractional part of the
result will be thrown away or truncated. As a result, the value 2 will be assigned to the number variable. It doesn’t
matter that the number variable is declared as a double because the fractional part of the result is discarded before
the assignment takes place.
In order for a division operation to return a floating-point value, one of the
operands must be of a floating-point data type. 
For example, the previous code could be written as follows: 

float number;
number = 5.0 / 2;

In this code, 5.0 is treated as a floating-point number, so the division operation will


return a floating-point number. The result of the division is 2.5.
Arithmetic Assignment Operators

These types of operations are very common in programming. For convenience, we may use the arithmetic assignment
operators which are designed specifically for these jobs.
BELOW SHOWS THE ARITHMETIC
A S S I G N M E N T O P E R AT O R S A N D
THEIR SAMPLE USAGE.
Summary
● Operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulation
● Precedence numbers indicate the order in which the computer performs the
operation in an expression.
● Arithmetic assignment operators modify the current value of a variable by
performing an operation on it. They are equivalent to assigning the result of
an operation to the first operand
If you use the char data type then it will only
take in a single character.
If you want to input multiple characters, use the
string data type instead.
However, since a string input will stop after the
first word/space, to input multiple words you
will need to use a function called getline().
<< VS >>
To remember which operator goes with which keyword, simply notice the direction of the symbols.
In cout statements, the << points to the cout. This means you are sending the data from the program
to be outputted to the user/display screen.

In cin statements the >> points to the variable name. This means you are sending the data from the
user/keyboard to the program and storing it in the variable.
S U M M A RY
1. The keyword cin is used to retrieve input in C++ and it is used with the extraction operator ( >>
).
2. The input is stored in the variable that follows the extraction operator >>
3. The data inputted should match the data type of the variable where it will be stored or it might
cause unexpected results

You might also like