unit 4. operators and expression
TRANSCRIPT
Operatorsand
Expressions
Unit 4
Ashim Lamichhane 2
Operators• Symbol that operates on certain data type or data item.
• Used in program to perform certain mathematical or logical manipulations.
• Ex: in a simple expression 5+6, the symbol “+” is called an operator which operates on two data items 5 and 6.
• The data items that operator act upon are called operands.
Ashim Lamichhane 3
Expression
• An expression is a combination of variables, constants and operators written according to syntax of the language.
• Ex:8+10 a+c*d a>b a/c
Ashim Lamichhane 4
• We can classify operators into – Unary operators• Which requires only one operand• Ex. ++ , --, +,-
– Binary operators• Which requires two operands• Ex. +,-,*, / , < , > etc.
– Ternary operators• Which require three operands• Ex. “ ?: ” (conditional operator)
Ashim Lamichhane 5
• Ternary operators can be further classified into following categories:
–Arithmetic Operators–Relational Operators–Logical Operators–Assignment Operators– Increment and Decrement Operators–Conditional Operators–Bitwise Operators–Special Operators
Ashim Lamichhane 6
Arithmetic OperatorsAssume variable A holds 10 and variable B holds 20 then
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = 10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and remainder of after an integer division.
B % A = 0
++ Increment operator increases the integer value by one.
A++ = 11
-- Decrement operator decreases the integer value by one.
A-- = 9
Ashim Lamichhane 7
Integer Arithmetic• Division Rule– Int / int =int– Float / float= float– Int / float = float– Float /int =float
Ashim Lamichhane 8
#include <stdio.h>Void main() {
int a = 21; int b = 10; int c ; c = a + b; printf("Line 1 - Value of c is %d\n", c ); c = a - b; printf("Line 2 - Value of c is %d\n", c ); c = a * b; printf("Line 3 - Value of c is %d\n", c ); c = a / b; printf("Line 4 - Value of c is %d\n", c ); c = a % b; printf("Line 5 - Value of c is %d\n", c ); c = a++; printf("Line 6 - Value of c is %d\n", c ); c = a--; printf("Line 7 - Value of c is %d\n", c );
}
Ashim Lamichhane 9
Relational OperatorsAssume variable A holds 10 and variable B holds 20
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true.
(A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.
(A != B) is true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.
(A > B) is not true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.
(A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.
(A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.
(A <= B) is true.
Ashim Lamichhane 10
#include <stdio.h>main() {
int a = 21; int b = 10; int c ; if( a == b ) { printf("Line 1 - a is equal to b\n" ); } else { printf("Line 1 - a is not equal to b\n" ); }
if ( a < b ) { printf("Line 2 - a is less than b\n" ); } else { printf("Line 2 - a is not less than b\n" ); } if ( a > b ) { printf("Line 3 - a is greater than b\n" ); } else { printf("Line 3 - a is not greater than b\n" ); } /* Lets change value of a and b */ a = 5; b = 20; if ( a <= b ) { printf("Line 4 - a is either less than or equal to b\n" ); } if ( b >= a ) { printf("Line 5 - b is either greater than or equal to b\n" ); }
}
Ashim Lamichhane 11
Logical Operators
Assume variable A holds 1 and variable B holds 0Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
(A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.
(A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
!(A && B) is true.
Ashim Lamichhane 12
Void main() { int a = 5; int b = 20; int c ; if ( a && b ) {
printf("Line 1 - Condition is true\n" ); } if ( a || b ) {
printf("Line 2 - Condition is true\n" ); } /* lets change the value of a and b */ a = 0; b = 10; if ( a && b ) {
printf("Line 3 - Condition is true\n" ); } else {
printf("Line 3 - Condition is not true\n" ); }
if ( !(a && b) ) { printf("Line 4 - Condition is true\n" );
} }
Ashim Lamichhane 13
Assignment OperatorsOperator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand
C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
C %= A is equivalent to C = C % A
Ashim Lamichhane 14
Increment And Decrement Operators• Increment operator is used to increase the
value of an operand by 1• Decrement operator is used to decrease the
value of an operand by 1Operator Description Example
++ ++variable (prefix notation) Variable = variable + 1
++ variable++ (postfix notation) Variable= variable + 1
-- - -variable (prefix notation) Variable =variable - 1
-- variable- - (postfix notation) Variable =variable - 1
Ashim Lamichhane 15
void main(){int a= 10;clrscr();printf("a =%d",a);printf("a =%d",++a);printf("a =%d",a++);printf("a =%d",a);
}
Ashim Lamichhane 16
void main(){int a= 10;clrscr();printf("a =%d",a);printf("a =%d",++a);printf("a =%d",a++);printf("a =%d",a);
}
OUTPUT:a=10a=11a=11a=12
Ashim Lamichhane 17
Conditional Operator
• The operator pair “?:” is known as conditional operator.• It takes three operands. Also called ternary operator.• General form:
expression1 ? expression 2 : expression 3• expression1 is evaluated first
If expression1 is true then value of expression2 is the value of condition
expressionelse
the value of expression3 is the value of conditional expression
Ashim Lamichhane 18
void main(){
int n1,n2, larger;clrscr();printf("Enter Two numbers:");scanf("%d%d",&n1,&n2);
larger= n1>n2 ? n1 : n2;printf("The larger number is %d", larger);getch();
}
Ashim Lamichhane 19
Bitwise Logical Operator
• Bitwise operators are used for manipulating data at bit level.
• These operators are used for testing the bits or shifting them to the
left or to the right.
• Can be applied only to integer-type operands and not to float or
double.
• Three types of bitwise operators
– Bitwise logical operators
– Bitwise shift operators
– One’s complement operator
Ashim Lamichhane 20
Bitwise logical Operators
• Performs logical tests between two integer-type operands.
• These operators work on their operands bit-by-bit starting
from the least significant(i.e rightmost) bit.
• Three logical bitwise operators
– Bitwise AND(&)
– Bitwise OR( | )
– Bitwise Exclusive OR (^)
Ashim Lamichhane 21
Bitwise And (&)
• Logical ANDing between two operands.
• The result of ANDing operation is 1 if both the bits have a
value of 1; otherwise it is 0.
• Consider n1=60 and n2=15
– N1 0000 0000 0011 1100
– N2 0000 0000 0000 1111
• If n3= n1 & n2;
– n3 0000 0000 0000 1100
Ashim Lamichhane 22
Bitwise OR( | )• ORing between two operands
• The result of Oring operations is 1 if either of the bits have
value of 1; otherwise it is 0.
• Consider n1=60 and n2=15
– N1 0000 0000 0011 1100
– N2 0000 0000 0000 1111
• If n3= n1 | n2;
– n3 0000 0000 0011 1111
Ashim Lamichhane 23
Bitwise Exclusive XOR ( ^ )• The result of Exclusive ORing operations is 1 only
if one of the bits have a value of 1; otherwise it is 0.
• Consider n1=60 and n2=15
– N1 0000 0000 0011 1100
– N2 0000 0000 0000 1111
• If n3= n1 & n2;
– n3 0000 0000 0011 0011
Ashim Lamichhane 24
#include<stdio.h>void main(){
int n1=60,n2=51,AND, OR, XOR;AND= n1 & n2;OR= n1 | n2;XOR= n1 ^ n2;printf("AND=%d\n",AND ); printf("OR=%d\
n",OR ); printf("XOR=%d\n",XOR );}
Ashim Lamichhane 25
Bitwise Shift Operators• Are used to move bit patterns either to the left
or to the right.
• There are two bitwise shift operators– Left shift ( << )– Right shift ( >> )
Ashim Lamichhane 26
• Left Shift ( << )– Causes the operand to be shifted to the left by n
positions.• operand << n
– The leftmost n bits in the original bit pattern will be lost and the rightmost n bits empty positions will be filled with 0’s.
– Ex n1=60• Execute the statement n2= n1 <<3;
n1 0000 0000 0011 1100
Shift 1 0000 0000 0111 1000
Shift 2 0000 0000 1111 0000
Shift 3 0000 0001 1110 0000 (==n2)
Ashim Lamichhane 27
• Right Shift ( >> )– Causes the operand to be shifted to the right by n
positions.• operand >> n
– The empty leftmost n bits positions will be filled with 0’s, if the operand is an unsigned integer.
– Ex unsigned int n1=60;• Execute the statement n2= n1 >>3;
n1 0000 0000 0011 1100
Shift 1 0000 0000 0001 1110
Shift 2 0000 0000 0000 1111
Shift 3 0000 0000 0000 0111 (==n2)
Ashim Lamichhane 28
Bitwise One’s Complement Operator
• Is a unary operator which inverts all the bits represented by its operand.
• This means that all 0s becomes 1s and 1s becomes 0s– If n1=60, then we execute the statement• n2= ~n1
– The resulting bit pattern represents the decimal: -61
Ashim Lamichhane 29
#include<stdio.h>voidmain(){
unsigned int n1=60 ,left, right;left =n1 << 3;right =n1 >> 3;
printf("%d \n", left);printf("%d\n", right);
}
Ashim Lamichhane 30
Special Operators• C supports some special operators and for
now we discuss– comma operator (,) – sizeof operator
Ashim Lamichhane 31
Comma operator• The comma operator can be used to link related expressions
together.
• A comma-linked list of expressions are evaluated from left-to-
right and the value of the rightmost expression is the value of
the combined expressions.
• Ex: n3=(n1=50, n2=10,n1+n2)
– The first assign the value 50 to n1
– Assign the value 10 to n2
– Assign sum n1+n2 to n3
Ashim Lamichhane 32
sizeof Operator• Is used with an operand to return the number
of bytes it occupies.
• The operand may be constant, variable or a data type qualifier.
Ashim Lamichhane 33
#include<stdio.h>Void main(){
unsigned int n1;printf("Integer Variable =>
%lu\n",sizeof(n1) );printf("Double Constant =>
%lu\n",sizeof(15.11) );printf("Long int Data type qualifier =>
%lu\n",sizeof(15L) );}
Ashim Lamichhane 34
Operator precedence and associativity• The precedence is used to determine how an expression
involving more than one operator is evaluated.• There are distinct level of precedence.• The operators at the higher level of precedence are evaluated
first.• Operators of same precedence are evaluated either from “left
to right” or “right to left” depending on the level also known as associativity.
Ashim Lamichhane 35
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type) * & sizeof Right to left Multiplicative * /% Left to right
Additive +- Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
Ashim Lamichhane 36
• Ex” consider four integer type variable n1=9 n2=12 n3=3 and x
x = n1 –n2 / 3 + n3 *2 -1Becomes,
x= 9 -12 /3 + 3 * 2 -1
Step1: 9 -12 / 6 * (2-1)Step2: 9-12/6*1Step3: 9-2*1Step4: 9-2Step5: 7
Ashim Lamichhane 37
• Example x=20 and y=5
• If(x==10+15 && y<10)
– Step1: If(x==25 && y<10)
– Step2: if(20==25 && 5 < 10)
– Step3: if(20==25 && TRUE)
– Step3: if(FALSE && TRUE)
• Since one condition is FALSE, the whole condition is
FALSE.
Ashim Lamichhane 38
Solve this• Find the value of “a” in each of the following
statements: – int i = 3, j = 4, k =8; – float a = 4.5, b = 6.5, c = 3.5; • a=b-i/k+c/k • a = (b-k)/j + (j+c)/k • a = c - ((i+j)/(k+i)) *b • a=c-i+j/k+i*b • a=c+j%2+b • a=(b+1)%(c+1)
Ashim Lamichhane 39
END