0% found this document useful (0 votes)
42 views

CPP C - 3

The document discusses expressions in C++. It defines an expression as consisting of operators, constants, and variables arranged according to the rules of the language. Expressions specify computations and return values, while variables specify storage. The document outlines the different types of operators in C++ including arithmetic, relational, logical, increment/decrement, assignment, and conditional operators. It provides examples of each operator type and how they are used in expressions.

Uploaded by

lalisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

CPP C - 3

The document discusses expressions in C++. It defines an expression as consisting of operators, constants, and variables arranged according to the rules of the language. Expressions specify computations and return values, while variables specify storage. The document outlines the different types of operators in C++ including arithmetic, relational, logical, increment/decrement, assignment, and conditional operators. It provides examples of each operator type and how they are used in expressions.

Uploaded by

lalisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter Three

Expressions

1 03/09/2022
Expression

C++ expression consists of operators, constants, and variables

which are arranged according to the rules of the language.


Expressions are the fundamental means of specifying
computations in a Programming Language.
An expression is said to return a value, while variables are the

means for specifying storage


To understand expression evaluation, need to be familiar with

the orders of operator and operand evaluation

2 03/09/2022
Operators

The operations (specific task) are represented by operators

and the objects of the operation(s) are referred to as operands


An operator is a symbol that causes the compiler to take an

action. Operators act on operands, and in C++ all operands


are expressions.
Operand is part of computer instruction which specifies

what data is to be manipulated or operated on, while at the


same time representing the data itself.

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

Note: All expression have at least one operand

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

Divisions performed with integral operands will

produce integral results; for example, 7/2 computes to


3.
If at least one of the operands is a floating-point

number, the result will also be a floating-point number;


e.g., the division 7.0/2 produces an exact result of 3.5.

7 03/09/2022
Arithmetic Operation cont…
Remainder division is only applicable to integral

operands and returns the remainder of an integral


division. For example, 5%3 computes to 2.
Normal mathematical rules (multiplication before

addition) apply when evaluating an expression, i.e. the


*, /, % operators have higher precedence than + and -.

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

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).

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 (--)

operators provide a convenient way of, respectively,


adding and subtracting 1 from a numeric variable.
In C++, increasing a value by 1 is called incrementing,

and decreasing by 1 is called decrementing.

16 03/09/2022
Cont…
The increment operator (++) increases the value of the variable

by 1, and the decrement operator (--) decreases it by 1. Thus, if


you have a variable, C, and you want to increment it, you
would use this statement:

C++; //start with C and increment by 1


This statement is equivalent to the more verbose statement

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;

a=++x; //evaluated before the assignment i.e. Increment the


value and then fetch it. X=6 & a=6
 a=x++;//is evaluated after i.e. Fetch the value and then

increment the original. a=5 & x=6

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

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 unreadable,

perform some bitwise manipulations on it.

23 03/09/2022
Bitwise op…..
Operator Action

& AND

| OR

^ XOR

~ One’s complement

<< Shift left

>> Shift right

24 03/09/2022
Operator AND

The AND operator (&) is a single ampersand, as


opposed to the logical AND, which is two ampersands.
When you AND two bits, the result is 1 if both bits are
1, but 0 if either or both bits are 0.
The way to think of this is: The result is 1 if bit 1 is set
and if bit 2 is set.
E.g.
0 000010 1
0 000011 0

25 03/09/2022
Operator OR

The second bitwise operator is OR (|).


Again, this is a single vertical bar, as opposed to the
logical OR, which is two vertical bars.
When you OR two bits, the result is 1 if either bit is set
1 or if both are.
E.g.
0 000010 1
0 010011 0

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

Highest ++ -- (post fix) Right to left


++ -- (prefix) Right to left
*/% Left to right
+- Left to right
< <= > >= Left to right

== != Left to right

&& 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

You might also like