100% found this document useful (1 vote)
321 views4 pages

Chapter 5: C++ Operators

The document discusses operators in C++. It defines mathematical operators like addition, subtraction, multiplication, division, and modulus. It explains that integer division discards remainders, while modulus uses the % operator to obtain the remainder. Compound operators like += combine assignment with another operation. Increment (++) and decrement (--) operators increase or decrease a variable by 1. The document provides examples of operator usage and references for further reading. It includes a practice activity to write the output of sample code using various operators.

Uploaded by

Ryan Zaguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
321 views4 pages

Chapter 5: C++ Operators

The document discusses operators in C++. It defines mathematical operators like addition, subtraction, multiplication, division, and modulus. It explains that integer division discards remainders, while modulus uses the % operator to obtain the remainder. Compound operators like += combine assignment with another operation. Increment (++) and decrement (--) operators increase or decrease a variable by 1. The document provides examples of operator usage and references for further reading. It includes a practice activity to write the output of sample code using various operators.

Uploaded by

Ryan Zaguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CC02 MODULE FUNDAMENTALS OF PROGRAMMING

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.

PRE-TEST: TABLE COMPLETION: Fill up the missing data below.


C++ Operator Name Description
Adds together two values
Subtracts one value from another

Multiplies two values

Divides one value by another

Returns the division remainder

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.

Assignments Operators (=)

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:

a = b; This statement assigns to variable a (the lvalue) the value contained in


variable b (the rvalue). The value that was stored until this moment
in a is not considered at all in this operation, and in fact that value is lost.

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.

Arithmetic Operators (+, -, *, /, % )


The five arithmetical operations supported by the C++ language are:

+ 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.

Integer division and modulus

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);

Increment(++) and Decrement(--)


Shortening even more some expressions, the increase operator (++) and the decrease
operator (--) increase or reduce by one the value stored in a variable. They are equivalent
to +=1 and to -=1, respectively. Thus:

1 c++;
2 c+=1;
3 c=c+1;

For more knowledge Fundamentals of Programming, please check the


link provided;

➢ 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:

Write the output of the given source code below.

// TRY THIS: Write your output here:

#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

You might also like