lect31 operators expressions
TRANSCRIPT
-
8/13/2019 Lect31 Operators Expressions
1/22
COMPUTERPROGRAMMING I
(TA C162)
Lecture 31 Operators & Expressions
-
8/13/2019 Lect31 Operators Expressions
2/22
Todays Agenda Operators and Expressions
Relational O erators Lo ical O erators
Assignment Operators Increment and Decrement Operators
Bitwise Operators, Special Operators
Control Structure Conditional
If statement , Switch statement, Conditional operator
s a emen
Tuesday, April 06, 2010 2Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
3/22
Control Structures
It is a combination of individual instructions
Only one entry point and one exit point
Selection control structure
A control structure that chooses among alternativeprogram s a emen s
Condition
n express on a s e er rue or a se What is TRUE and What is FALSE in C?
-
Tuesday, April 06, 2010 3Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
4/22
Relational & Equality Operators
pera or ean ng
< Is less than
=
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Tuesday, April 06, 2010 4Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
5/22
Relational & Equality Operators Most conditions will have one of the following forms
variable variable
variable constant
The variable / constant can be integer, float/double orcharacter
Tuesday, April 06, 2010 5Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
6/22
Examples:5 = 0 FALSE
5.8 = 2.8
9 >= 0
=a
-
8/13/2019 Lect31 Operators Expressions
7/22
Logical OperatorsOperator Meaning&& Logical AND
|| Logical OR
! Logical NOT
Used to test more than one condition
Example: a
-
8/13/2019 Lect31 Operators Expressions
8/22
Logical Operators
in_range = (n > -10 && n < 10);
=
-
8/13/2019 Lect31 Operators Expressions
9/22
Operand 1 Operand 2 Operand 1 && Operand 2
- -Non-Zero (TRUE) Zero (FALSE) 0 (FALSE)
Zero (FALSE) Non-Zero (TRUE) 0 (FALSE)
Zero (FALSE) Zero (FALSE) 0 (FALSE)
Operand 1 Operand 2 Operand 1 || Operand 2
Non-Zero (TRUE) Non-Zero (TRUE) 1 (TRUE)
Non-Zero (TRUE) Zero (FALSE) 1 (TRUE)
-
Zero (FALSE) Zero (FALSE) 0 (FALSE)
Non-Zero (TRUE) 0 (FALSE)
Zero (FALSE) 1 (TRUE)
Tuesday, April 06, 2010 9Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
10/22
-
8/13/2019 Lect31 Operators Expressions
11/22
Assignment OperatorsSimple AssignmentOperators
Shorthand Operators
a = a+1 a+= 1
a = a-1 a-= 1
a =a*(b+1) a*=b+1
a = a/(b+1) a/=b+1
*
a = a%b a%=b
Tuesday, April 06, 2010 11Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
12/22
operator expression Explanation Assigns
i 3 d 5 4 f 6 12Assume:intc = 3 ,d = 5 ,e = 4 ,f = 6 ,g = 12; += c += 7 c = c + 7 10 to c-= d -= 4 d = d - 4 1 to d*= e *= 5 e = e * 5 20 to e/= f /= 3 f = f / 3 2 to f= g = 9 g = g 9 3 g 9 g g 9 3
Tuesday, April 06, 2010 12Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
13/22
Increment and Decrement Operators++ Adds one to the operand
-- Subtracts one from the operand
Both are unary operators
++ s equ va en o: = +
--i is equivalent to: i = i-1
is ++i and i++ are same??
13
-
8/13/2019 Lect31 Operators Expressions
14/22
What is Postfix and Prefix?In postfix the expression is evaluated first using the
original value of the variable and then the variable is
incremented (or decremented) by one.
Ex.
m = n++;
In refix the variable is incremented or decrementedfirst and then the expression is evaluated using the
new value of the variable.
Ex.
m = n;
Tuesday, April 06, 2010 14Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
15/22
Postfix and Prefix Examplesj =10;
i =j++;
What is the values of i and j?
=10;
i=++j;
Tuesday, April 06, 2010 15Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
16/22
i = 10; j = 20;
= ;
What will be the value of i, j and k?
i = 10; j = 20;
k = i++ + ++j;
What will be the value of i and k?
k = ++i + ++j;
a w e e va ue o , an
Tuesday, April 06, 2010 Biju K Raveendran@BITS Pilani 16
-
8/13/2019 Lect31 Operators Expressions
17/22
Operator Sample expression Explanation
++ ++a Increment a by 1, then use the new value of a in thea a . ++ a++ Use the current value of a in the expression in whicha resides, then increment a by 1.-- --b Decrement b by 1, then use the new value of b in theex ression in which b resides.
-- b-- Use the current value ofb in the expression in whichb resides, then decrement b by 1.
Tuesday, April 06, 2010 17Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
18/22
Bitwise OperatorsOperator Meaning
& Bitwise AND
| Bitwise OR
^
> Shift right
Manipulation of data at bit level
May not be applied to float values
18
-
8/13/2019 Lect31 Operators Expressions
19/22
Special Operators
The Size of Operator
occupies
Examples:
q=sizeof(float);
r=sizeof(long int);
Tuesday, April 06, 2010 19Biju K Raveendran@BITS Pilani
-
8/13/2019 Lect31 Operators Expressions
20/22
The comma operator
Used to link the related expressions together.
Evaluation starts from left to ri ht.
And the value of the right most expression is the valueof the combined expression.
Example:
, , ,
value contains 21
Use of comma o erator
for (i = 1, j = 15; i
-
8/13/2019 Lect31 Operators Expressions
21/22
Operators Associativity Rank
(),[], ., -> L to R 1
Postfix ++ Postfix -- R to L 2Prefix ++, Prefix --, !, ~, sizeof, Unary +,
Unary -, Unary *, Unary &
R to L 3
*, /, % L to R 5Binary plus(+) and Binary minus (-) L to R 6
L to R 7
= L to R 8
==, != L to R 9
Binary & L to R 10
Binary ^ L to R 11
Binar L to R 12
&& L to R 13
|| L to R 14
?: R to L
=, *=, /=, %=, +=, -=, &=, ^=, |=, = R to L 16
, L to R 1721
-
8/13/2019 Lect31 Operators Expressions
22/22
Short Circuit Evaluationa || b is true, if a is true, a && b is false, if a is false
Tuesday, April 06, 2010 22Biju K Raveendran@BITS Pilani