Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views18 pages

Operators

Operators are used to perform calculations and evaluate conditions. The main types of operators are assignment, arithmetic, increment/decrement, cast, relation, and logical operators. Assignment operators assign values, arithmetic operators perform math operations, and increment/decrement operators modify values. Examples demonstrate using different operator types to calculate results and evaluate expressions. Operator precedence determines the order of operations performed in expressions containing multiple operators.
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)
20 views18 pages

Operators

Operators are used to perform calculations and evaluate conditions. The main types of operators are assignment, arithmetic, increment/decrement, cast, relation, and logical operators. Assignment operators assign values, arithmetic operators perform math operations, and increment/decrement operators modify values. Examples demonstrate using different operator types to calculate results and evaluate expressions. Operator precedence determines the order of operations performed in expressions containing multiple operators.
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/ 18

BNJ 20802

Computer Programming
CHAPTER 3 : OPERATOR

Lecture Note
FTK
Operation and operator

2
Operators
 What is the operators??
 Operator is an expression or evaluation of certain value or condition to
achieve final value.
 Why we need operator?
 Its needed for calculation and selection of the value or condition.
 Type of Operators
1. Assignment Operator
2. Arithmetic Operator
3. Increment And Decrement Operator
4. Cast Operator
a) Relation operator
b) Logical operator

3
1. Assignment operator

 Use the assignment operator = to assign the value


of a variable to an expression
 In addition, assignment operator can also
perform simultaneously an arithmetic operator
and assignment.
 In expression of this type, the variable must be
placed on the left and assigned value on the right
of the assignment operator.

4
#include <iostream>

using namespace std;

int main()
{
int A = 4;
int B = 4;
int C = 4;
int D = 4;
int E = 4;
int F = 4;
int G = 4;
int H = 4; Output
int I = 4;
int J = 4; cout << "\n1.Please enter A = " << A <<endl;
int K = 4; cout << "\n2.Please enter B = " << B <<endl;
cout << "\n3.Please enter C = " << C <<endl;
A = 2; cout << "\n4.Please enter D = " << D <<endl;
B += 2; cout << "\n5.Please enter E = " << E <<endl;
C -= 2; cout << "\n6.Please enter F = " << F <<endl;
D *= 2; cout << "\n7.Please enter G = " << G <<endl;
E /= 2; cout << "\n8.Please enter H = " << H <<endl;
F %= 2; cout << "\n9.Please enter I = " << I <<endl;
G &= 2; cout << "\n10.Please enter J = " << J <<endl;
H |= 2; cout << "\n11.Please enter K = " << K <<endl;
I ^= 2;
J <<= 2; return 0;
K >>= 2; }
Exercise: Please determine the
final value of this operation. #include <iostream>
using namespace std;
a)Int i = 2, j = 5;
i *= j + 2; int main()
{
b)Int A = 3; int i = 2, j = 5, A = 3, a = 4, b = 7.9, c = 4;
A += 3; double d = 7.9;

i*= j + 2;
c) Int a = 4; b = 7.9; A += 3;
a *= b + 5; a *= b + 5;
c *= d + 5;
d)Int c = 4; double d = 7.9;
c *= d + 5; cout << "1. i = " << i <<endl;
cout << "2. A = " << A <<endl;
cout << "3. a = " << a << " b = "<< b <<endl;
Output cout << "4. c = " << c << " d = "<< d <<endl;
return 0;
}

6
2. Arithmetic operator
 Provide mathematical arithmetic calculation for numeric
quantities
 Except for remainder (%), all other arithmetic operator can
accept mix of integer and real operands.
 Generally, if one or both operands are reals then result will be
a real (or double to be exact)

7
Example: Calculate or estimate the result for operation
below?

a) Int a = 9, b =2; c = a / b;
b) Int a = -9, b = 2; c = a / b;
c) Double a = 9, b = 2; c = a/b
d) Double a = 4.75, b = 12.3456; c = (a + b)/2;
e) Int a = 2, b = 7, c = 3; d = a + b * c;
f) Int a = 2, b = 7, c = 3; d = (a + b) * c;
g) Intd; d=3*5%2;

8
3. Increment and decrement operator
 Increment and decrement operators of ++ and -- modifies
the operand by adding 1 or subtract 1 to its value and
cannot be used with constant for this reason.
 Let say, given ‘i’ is variable, both i++ (postfix) and ++i (prefix)
raise the value of ‘i’ by 1. both case, the operation i = i + 1 is
perform.

9
Example a: Calculate
a) Float x = 5.0;y = x++ -7.0 / 2;
b) Float x = 5.0;y = ++x –7.0 / 2;

Example b: Given the main function below, suggest the display output?
Int main()
{
inti=2, j=8;
cout<< i++ << endl;
cout<< i << endl;
cout<< j-- << endl;
cout<< --j << endl;
return 0;
}

10
4. CAST OPERATOR

4.1 Relation operator

4.2 Logical operator

11
Exercises

1. Int a = 5 , b = 3, c = 6, z = 1;
a) z -=a++ * b / --c + 7;

2. Int z = 9 ,a = 3, b = 9, c = 7;
a) z *= 7 * ++a - --b % 3 * c--;

3. The int variable x contains the number 7. Calculate the


value of the following logical expressions:

a) x < 10 && x >= 01


b) !x<10 && x >= 3
c) x++ ==8 || x == 7

12
Priority in operators

13
Exercise

14
15
16
17
18

You might also like