CPP C - 3
CPP C - 3
Expressions
1 03/09/2022
Expression
2 03/09/2022
Operators
3 03/09/2022
cont…
Operators act on operands, and in C++ all operands are
expressions.
In the expression m+4, m and 4 are operands and + is
operator.
y= ax+b
4 03/09/2022
C++ provides different types of operators
Arithmetic Operator
Relational Operators
Logical Operators
Increment/Decrement Operators
Assignment Operator
Conditional Operator
We will look at each category of operators in turn
5 03/09/2022
1) Arithmetic operators
Arithmetic operators are used to perform calculations.
C++ provides operators for five (5) basic arithmetic
operations
Operator symbol and name Example
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/09/2022
Arithmetic Operation cont…
Addition and subtraction work as you would expect
7 03/09/2022
Arithmetic Operation cont…
Remainder division is only applicable to integral
Example : 2 + 7 * 3=23
(2 + 7) * 3=27
8 03/09/2022
2. Relational operators
The operators which determine the relations among
different operands.
The relational operators are used to determine whether
(TRUE) or 0 (FALSE).
9 03/09/2022
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
10 03/09/2022
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 (==).
11 03/09/2022
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)
12 03/09/2022
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.
13 03/09/2022
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.
14 03/09/2022
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)
15 03/09/2022
4. Increment /decrement
operators(++, --)
The auto increment (++) and auto decrement (--)
16 03/09/2022
Cont…
The increment operator (++) increases the value of the variable
C = C + 1;
which is also equivalent to the moderately verbose statement
(C += 1)==(C++);
17 03/09/2022
Cont…
Both the increment operator (++) and the decrement
operator(--) come in two varieties: prefix and postfix. The
prefix variety is written before the variable name (++myAge);
the postfix variety is written after (myAge++).
Example: Let x=5;
18 03/09/2022
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.
19 03/09/2022
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!
20 03/09/2022
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.
21 03/09/2022
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.
22 03/09/2022
Bitwise Operators ( &, |, ^, ~, <<, >> ).
Bitwise operators modify variables considering the bits
23 03/09/2022
Bitwise op…..
Operator Action
& AND
| OR
^ XOR
~ One’s complement
24 03/09/2022
Operator AND
25 03/09/2022
Operator OR
26 03/09/2022
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
27 03/09/2022
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.
28 03/09/2022
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
29 03/09/2022
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.
30 03/09/2022
Level Operator Order
== != Left to right
|| Left to right
?: Left to right
= ,+=, -=, *=, /=,^= ,%=, &= ,|= ,<<= ,>>= Right to left
31 03/09/2022
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.
32 03/09/2022
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.
33 03/09/2022
End Of Chapter Three
Thank you!!!!
34 03/09/2022