C++ Operators
C++ Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
The four basic types of operators in C++:
i). Arithmetic operators,
ii). Assignment operators,
iii). Relational operators,
iv). Logical operators
I. Arithmetic Operators
Assume variable A has value of 10 and variable B has a value of 20, then
1
b) Write a C++ program that will subtract one number from the other number and print
out the answer.
c) Write a C++ program that will multiply two number and print out the answer.
d) Write a C++ program that will divide two number and print out the answer.
e) Write a C++ program that will print out the remainder when two integer numbers
are divided.
f) Write a C++ program that increase the value of an integer number by one and print
out the final value.
g) Write a C++ program that decrease the value of an integer number by one and print
out the final value.
2
b) Write a C++ program that will divide a variable by 5 and store the answer on that
variable
c) Write a C++ program that will get the remainder of an integer variable divided by 7
and store the answer on the same variable
3
Task, using declared and initialized variables
a) Write a C++ program that will compare two integer variables named x and y if they
are equal or not and store the test results on variable named areEqual
b) Write a C++ program that will compare two integer variables named x and y by
checking if x is greater than y, and store the results on variable named isGreater
c) Write a C++ program that will compare two integer variables named x and y by
checking if x is less than y, and store the results on variable named isLess
d) Write a C++ program that will compare two integer variables named x and y by
checking if x is greater than or equal to y, and store the results on variable named
isGreaterOrEqual
e) Write a C++ program that will compare two integer variables named x and y by
checking if x is less than or equal to y, and store the results on variable named
isLessOrEqual
4
IV. Logical Operators
Assume variable A holds 1 and variable B holds 0, as of true and false
b) Write a C++ program that will perform logical OR on two variables named x and y
and store the test results on variable named orResults
c) Write a C++ program that will perform logical NOT on a variable named x and store
the test results on variable named notResults
5
Other classification of operators
Basing on a number of operands.
➢ Unary operators - Operators that operate on single operand,
Example: – minus sign, ! Logical NOT, ++ increment, -- decrement
➢ Binary operators - Operators operate on two operands
Example: + addition, - subtraction, / division, % modulus, * multiplication
➢ Ternary or conditional operators - The conditional operators ? and : are called
ternary operators since they take 3 arguments. Note that ternary operator takes 3
operands for its implementation.
Example:
What this expression says is: “if expression 1 is true, then the value returned will be
expression 2, otherwise the value returned will be expression 3”.
Another example:
X > 10 ? “Yes”: “No”;