Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=

Similar presentations


Presentation on theme: "Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment="— Presentation transcript:

1 Operators and Expressions Rohit Khokher

2 Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment= Increment and decrement ++ -- Conditional?: Bitwise& | ^ > Special, sizeof

3 Arithmetic Operators +Addition or unary +a + b or +a14+4=18 -Subtraction or unary -a-b or -a14-4=10 *Multiplicationa * b14*4=56 /Divisiona/b14/4 =3 (Decimal truncated) %Modulo divisiona % b14/4 = 2 (Remainder) #include void main () { int month, days; days = 45; month = days /30; days = days % 30; printf (“Month = %d Days = %d”, months, days) } month = 45/30 = 1 days = 45 % 30 = 15

4 Real & mixed mode arithmetic #include void main () { float x, y, z; int a, b, c; x=6.0; y=7.0; a=6; b=7; z= x/y; printf (“Z = %d\n”, z); c=a/b; printf (“C = %d\n”, c); } #include void main () { float x, y, z; int a, b, c; x=6.0; y=7.0; a=6; b=7; z= a/y; printf (“Z = %d\n”, z); c=x/b; printf (“C = %d\n”, c); } z=6.0/7.0 =0.857143 c=6/7=0 z=6/7.0 =0.857143 c=6.0/7=0 Note: If one operand is float then the result is float.

5 Relational Operators <Less thana < bTrue/False <=Less than or equal toa <=bTrue/False >Greater thana >bTrue/False >=Greater than or equal toa >=bTrue/False ==Equal toa == bTrue/False !=Not equal toa != bTrue/False

6 Logical Operators && 0 AND a>b && x==10 ||OR a>b || x==10 !NOT!(a>b) a>bx==10a>b && x==10 TTT TFF FTF FFF a>bx==10a>b || x==10 TTT TFT FTT FFF a>b!(a>b) TF FT

7 Assignment Variable operator = Expression Variable = Variable operator (Expression) x+=y+1 x=x+(y+1) a=a+1a+=1 a=a-1a-=1 a=a*(n+1)a*=n+1 a=a/(n+1)a/=n+1 a= a% ba%= b Increment Decrement ++m, m++m = m+1 ; or m+=1; - -mm = m-1 ; or m-=1; y=++m (prefix) increase the value of m and assign it to y. y=m++ (postfix) assign the value m to y then increment m.

8 Conditional Operators Expression 1 ? Expression 2 : Expression 3 x=(a>b) ? a : b; If expression 1 is true then the value is expression 2 else the value is expression 3 If a> b then x=a else x=b

9 &Bitwise AND |Bitwise OR ^Bitwise exclusive OR <<Shift left >>Shift right Bitwise Manipulating data at bit level Bitwise operators The comma operators v = (x=10,y=5, x + y); x=10; y=5; v= x + y;

10 Arithmetic Expression Algebraic expression C expression

11 Example #include void main () { float a, b, c, x,y, z; a=9; b=12; c=3; x=a-b/3+c*2-1; y=a-b/(3+c)*(2-1); z= a-(b/(3+c)*2)-1; printf (“x= %f\n”, x); printf (“x= %f\n”, y); printf (“x= %f\n”, z); } x= 9 – 12 / 3 + 3 * 2 - 1; 46 5 11 10 y= 9 – 12 / (3+3) * (2-1); 61 2 2 7 z = 9- (12 / (3+3) * 2) -1; 6 2 4 5 4

12 Precedence of Arithmetic operators 1.An arithmetic expression is evaluated from left to right. 2.High priority operators ( * / % ) are evaluated first. 3.Low priority operators ( + - ) are evaluated next. x= 9 – 12 / 3 + 3 * 2 - 1; 46 5 11 10

13 Parenthesized expression 1.The parenthesized expressions change the evaluation priority. 2.The innermost parentheses have the highest priority. 3.The outermost parentheses have the lowest priority. 4.The nesting levels of intermediate parentheses determine their priority. 5.Un-parenthesized expressions within parentheses are evaluates according to the rules described for un parenthesized expressions. z = 9 - ( 18 / (3 + 3 * 2 ) * 2 ) - 1; 6 9 2 4 5 4

14 Type conversions in expressions Implicit Type Conversion short and char are automatically converted to int. If one operand is long double then the other operand is converted to long double and the result is in long double. else If one operand is double then the other operand is converted to double and the result is in double. else If one operand is float then the other operand is converted to float and the result is in float. else If one operand is unsigned long int then the other operand is converted to unsigned long int and the result is in unsigned long int. else If one operand is long int and the other is unsigned int, then – If unsigned int can be converted to long int, then the result will be long int – else both will be unsigned long int. else If one operand is long int then the other operand is converted to long int and the result is in long int. else If one operand is unsigned int then the other operand is converted to unsigned int and the result is in unsigned int. Explicit Type Conversion (Type name) expression (int) a+b (double) x/y Operator Associativity Decides the order in which multiple occurrences of the same level operators are applied.


Download ppt "Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment="

Similar presentations


Ads by Google