C++Ch3
C++Ch3
Expressions
1 03/06/2025
Expression
y= ax+b
4 03/06/2025
C++ provides different types of operators
Arithmetic Operator
Relational Operators
Logical Operators
Increment/Decrement Operators
Assignment Operator
Bitwise Operators
Conditional Operator
We will look at each category of operators in
turn
5 03/06/2025
1) Arithmetic operators
Arithmetic operators are used to perform
calculations.
C++ provides operators for five (5) basic
Operator symbol and Example
arithmetic
name
operations
Addition (+) 12 + 4.9 // gives 16.9
Subtraction (-) 3.98 - 4 // gives -0.02
Multiplication (*) 2 * 3.4 // gives 6.8
Division (/) 9 / 2.0 // gives 4.5
Remainder (%) 13 % 3 //gives 1
// gives 1
6 03/06/2025
Arithmetic Operation cont…
Addition and subtraction work as you would expect
7 03/06/2025
Arithmetic Operation cont…
Remainder division is only applicable to integral
Example : 2 + 7 * 3=23
(2 + 7) * 3=27
8 03/06/2025
Quiz (5%)
Write C++ code that accept two numbers
from keyboard and perform all arithmetic
operations.
Output:
Addition of numbers’: AA
Subtraction :SS
Multiplication: MM
Division: DD
Remainder: RR
9 03/06/2025
2. Relational operators
The operators which determine the
relations among different operands.
The relational operators are used to
determine whether two numbers are equal,
or if one is greater or less than the other.
Every relational statement evaluates to
either 1 (TRUE) or 0 (FALSE).
10 03/06/2025
Relational operators
cont…
C++ provides six different relational operators
which works with numbers and characters but
not with strings.
These relational operators are:--
Example:
Evaluates
1. < 100 < 50;
false
2. <= 100 <= 50; false
3. == 100 == 50; false
4. > 100 > 50; true
5. >= 100 >= 50; true
6. != 100 != 50; true
11 03/06/2025
Cont…
Note that the <= and >= operators are
only supported in the form shown.
In particular, =< and => are both invalid
and do not mean anything.
DON'T confuse the assignment operator
(=) with the equals relational operator
(==).
12 03/06/2025
3. Logical operator
Logical operators refer to the ways these
relationships (among values ) can be
connected.
C++ provides three logical operators .
They are:-
1.|| (logicalOR)
2.&& (logical AND)
3.! (logical NOT)
13 03/06/2025
Logical AND
A logical AND statement evaluates two
expressions, and if both expressions are
true, the logical AND statement is true as
well.
If it is true that you are hungry, AND it is
true that you have money, THEN it is true
that you can buy lunch. Thus, if ( (x == 5)
&& (y == 5) )
would evaluate TRUE if both x and y are
equal to 5, and it would evaluate FALSE if
either one is not equal to 5.
14 03/06/2025
Logical OR
A logical OR statement evaluates two
expressions.
If either one is true, the expression is true.
Example: if ( (x == 5) || (y == 5) )
evaluates TRUE if either x or y is equal to 5,
or if both are.
Note: logical OR is two || symbols. A single
| symbol is a different operator logical AND
is && symbols and & does not mean logical
AND.
15 03/06/2025
Logical Not
A logical NOT statement evaluates true if
the expression being tested is false.
Again, if the expression being tested is
false, the value of the test is TRUE! Thus,
if ( !(x == 5) ) is true only if x is not equal
to 5.
This is exactly the same as writing if (x !=
5)
16 03/06/2025
4. Increment /decrement
operators(++, --)
The auto increment (++) and auto
decrement (--) operators provide a
convenient way of, respectively, adding
and subtracting 1 from a numeric variable.
In C++, increasing a value by 1 is called
17 03/06/2025
Cont…
The increment operator (++) increases the value of the
statement
A = A+ 1;
which is also equivalent to the moderately verbose
statement (A += 1)==(A++);
18 03/06/2025
Cont…
Both the increment operator (++) and the decrement
19 03/06/2025
Assignment operators
The assignment operator (=) causes the
operand on the left side of the assignment
operator to have its value changed to the value
on the right side of the assignment operator.
The expression
x = 2 + 3; assigns the value that
is the result of adding a and b to the operand x.
An operand that legally can be on the left side
of an assignment operator is called an lvalue.
That which can be on the right side is called
(you guessed it) an rvalue.
20 03/06/2025
Cont…
Constants are r-values.
They cannot be lvalues. Thus, you can write
x = 35; // ok
but you can't legally write
35 = x; // error, not an lvalue!
21 03/06/2025
Conditional operator ( ? )
The conditional operator evaluates
an expression and returns a different
value according to the evaluated
expression, depending on whether it
is true or false.
Syntax: condition ? result1 : result2
if condition is true the expression
will return result1, if not it will return
result2.
22 03/06/2025
Example:
7==5 ? 4 : 3
returns 3 since 7 is not equal to
5.
7==5+2 ? 4 : 3
returns 4 since 7 is equal to 5+2.
5>3 ? a : b
returns a, since 5 is greater than
3.
a>b ? a : b
returns the greater one, a or b.
23 03/06/2025
Bitwise Operators ( &, |, ^, ~,
<<, >> ).
Bitwise operators modify variables
considering the bits that represent the
values that they store, that means, their
binary representation.
The bitwise operators are often used in
cipher routines.
If you want to make a disk file appear
& AND
| OR
^ XOR
~ One’s complement
25 03/06/2025
Operator AND
27 03/06/2025
Operator Exclusive OR
The third bitwise operator is exclusive OR
(^).
When you exclusive OR two bits, the result
is 1 if the two bits are different.
E.g.
0 000010 1
0 010011 0
28 03/06/2025
The Complement Operator
The one's complement operator, ~, reverses the
state of each bit in its operand. That is, all 1's are
set to 0, and all 0's are set to 1.
Example:
Original: 0 0 1 0 1 1 0 0
One’s complement: 1 1 0 1 0 0 1 1
Two’s complement: 0 0 1 0 1 1 0 0
Notice that a sequence of two complements in a
row always produces the original number. Thus,
the first complement represents the coded
version of that byte. The second complement
decodes the byte to its original value.
29 03/06/2025
Explicit type casting operators.
Type casting operators allows you to
convert a datum of a given type to another
data type.
E.g. int i;
float f = 3.14;
i = (int)f; -> equivalent to i = int(f);
Then variable i will have a value of 3
ignoring the decimal point
30 03/06/2025
Operator Precedence.
The order in which operators are evaluated
in an expression is known as operator
precedence.
Operators in higher levels take precedence
over operators in lower levels.
31 03/06/2025
Level Operator Order
== != Left to right
|| Left to right
?: Left to right
= ,<<= ,>>=
32 03/06/2025
Cont…
E.g. a==b+c*d
Firstly c * d is evaluated because * has a
higher precedence than + and = =.
Then result is added to b because + has a
higher precedence than = =
Finally == is evaluated.
33 03/06/2025
Cont…
Precedence rules can be overridden by using
brackets.
E.g. rewriting the above expression as:
a = = (b + c) * d causes + to be evaluated before
*.
Operators with the same precedence level are
evaluated in the order specified by the
column on the table of precedence rule.
E.g. a = b += c the evaluation order is right
to left, so the first b += c is evaluated
followed by a = b.
34 03/06/2025
End Of Chapter Three
Thank you!!!!
35 03/06/2025