0% found this document useful (0 votes)
24 views6 pages

C++ Operators

Uploaded by

wiidekingstone
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
0% found this document useful (0 votes)
24 views6 pages

C++ Operators

Uploaded by

wiidekingstone
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/ 6

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

Operator Description Example


+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator gives the remainder after an B % A will give 0
integer division
++ Increment operator, increases integer value by A++ will give 11
one
-- Decrement operator, decreases integer value by A-- will give 9
one

Task, using declared and initialized variables


a) Write a C++ program that will add two numbers and store the results on variable
named result and print out the result.

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.

II. Assignment operators

Operator Description Example


= Simple assignment operator, assigns values from C = 30; will assign value of
right side operands to left side operand 30 into variable C
+= Add AND assignment operator, it adds right X += 30; is equivalent to
operand to the left operand and assign the result X = X + 30;
to left operand
-= Subtract AND assignment operator, it subtracts A -= 30; is equivalent to
right operand from the left operand and assign A = A – 30;
the result to left operand
*= Multiply AND assignment operator, it multiplies B *= 20; is equivalent to
right operand with the left operand and assign B = B * 20;
the result to left operand
/= Divide AND assignment operator, it divides left B /= 20; is equivalent to
operand with the right operand and assign the B = B / 20;
result to left operand
%= Modulus AND assignment operator, it takes D %= 10; is equivalent to
modulus using two operands and assign the D = D % 10;
result to left operand

Task, using declared and initialized variables


a) Write a C++ program that will add 10 to an integer variable and store the answer on
that variable

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

III. Relational Operators


Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


== Checks if the values of two operands are equal or A == B is false
not, if yes then condition becomes true, else
becomes false
!= Checks if the values of two operands are equal or A != B is true.
not, if values are not equal then condition
becomes true, else becomes false
> Checks if the value of left operand is greater than A > B is false
the value of right operand, if yes then condition
becomes true, else becomes false
< Checks if the value of left operand is less than the A < B is true.
value of right operand, if yes then condition
becomes true, else becomes false
>= Checks if the value of left operand is greater than A >= B is false
or equal to the value of right operand, if yes then
condition becomes true, else becomes false
<= Checks if the value of left operand is less than or A <= B is true
equal to the value of right operand, if yes then
condition becomes true, else becomes false

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

Operator Description Example


&& Called Logical AND operator. If both the A && B is false
operands are non-zero, then condition becomes
true, else becomes false
|| Called Logical OR Operator. If any of the two A || B is true.
operands is non-zero, then condition becomes
true, else becomes false
! Called Logical NOT Operator. Use to reverses the ! (A && B) is true
logical state of its operand. If a condition is true,
then Logical NOT operator will make false.

Task, using declared and initialized variables


a) Write a C++ program that will perform logical AND on two variables named x and y
and store the test results on variable named andResults

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

You might also like