Operators and Expression Dr Muhamad Zaini Yunos JKBR, FKMP
Arithmetic operators Unary operators Relational operators Logical operators Assignment operators Conditional operators Comma operators Operators
Expression Arithmetic expression Logical expression Character/string expression
Arithmetic Operators Arithmetic operators C operators Addition + Substraction - Multiplication * Division / Modulo division %
Arithmetic Expression Integer Expression Floating point expression Mixed mode expression
Integer Expression Mathematical expression obtained by combining integer variables and/or integer constant with help of arithmetic operators Rules A signed or unsigned integer variable or an integer constant is an integer expression An integer expression connected by arithmetic operator to an unsigned integer constant is an integer expression An integer expression enclosed in parentheses is an integer expression 2 integer expression connected by an arithmetic operator is an integer expression
Floating point expression Mathematical expression obtained by combining floating point variables and/or floating point constant with help of arithmetic operators Rules- same with integer
Mixed mode expression and implicit conversion Expression formed by integer, floating point or character constant A is integer and B floating point.. A/B?? Before division A must be converted to floating point then A is divided by B. Finally result will be stored as floating point number. During conversion lower type operand are converted to higher type operand and the result is the higher type
Data type Long double Double Float Unsigned long int Long int Unsigned int int Char, short Order Higher Lower
Type casting Format: (data_type) variable name Expression Action A= (int) 8.75 The value of A is finally 8 B= (float)x +y x is converted to float then added to y C=(double)(x+y) x +y converted to double D=(char) 65 65 converted to character (A) ASCII value E= (float)1/5 1 is converted to float 1.0 divided by 5. the value of E becomes 0.2 I=(int) D D converted to integer. The ASCII value of D is stored to I (68)
Hierarchy of arithmetic operators Operator Arrangement/Priority (if appears more than once in a statement) ( ) Left to right ++, -- Right to left *, /, % Left to right +, - Left to right
1. 5 + 2 * 6 4 / 2 5 + 12-4 / 2 5 + 12-2 17-2 15
Library Functions Mathematic function C Equivalent Mode of output Header file Power (a b ) pow (a,b) double math.h Exponential exp(x) double math.h logarithm log(x) double math.h squareroot sqrt(x) double math.h Sin sin( x) double math.h Cos cos(x) double math.h Tangent tan(x) double math.h Absolute x abs(x) int stdlib.h Absolute x fabs(x) double math.h Round up to next integer value floor(x) double math.h
Unary Operators Operator Meaning - negative + positive -- decrement ++ increment
Give the size of operand in bytes Size of operator
Relation Expression Relational expression use Relational operator Combination of more than one statement Can consists of variable vs variable produce variable vs constant constant vs constant 0 (if false) 1(if true) 19
Relation Expression Relational Operator Operator Description = = Equal to > Greater than < Less than >= Greater than or equal <= Less than or equal!= Not equal 20
Relation Expression Example 1: P/s: a, b and c are variables, Replace with the given values int a=6, b =1, c = -2; 1) a+ b == c 2) a!= b 6 + 1== -2 6!= 1 7 == -2 Answer: 0(False) Answer : 1 (True) 22
Relation Expression Example 2 : int a=6, b =1, c = -2; 3) b < a 4) b + c <= a 1 < -2 1 + -2 < 6-1 < 6 Answer: 0 (False) Answer : 1 (True) 23
Relation Expression An example program which uses relational expression #include <stdio.h> void main() { int age; printf( \nplease enter your age >> ); scanf( %d,&age); if (age > 21) Relational expression printf( \nyou are qualified to vote ); } 24
Logical Expression Logical expression use Logical Operator Combination of one or more expressions Can consists of Relational expr. vs logical expr. produces Relational expr. vs variable Relational expr. vs constant 0 (if false) 1(if true) 25
Logical Expression Logical Operator Operator && Description AND OR! NOT Logical operator && dan is used between 2 or more relational expression 26
Logical Expression Logical operator truth table for AND AND (&&) Logical Operator Result False AND False False Value 0 1 False AND True False 0 0 0 True AND False False 1 0 1 True AND True True 27
Logical Expression Logical operator truth table for OR OR ( ) Logical Operator Result False OR False False Value 0 1 False OR True True 0 0 1 True OR False Trrue 1 1 1 True OR True True 28
Logical Expression Logical operator truth table for NOT NOT(!) Value Result!0 1!1 0 Logical Operator Not false Not true Result True False 29
Logical Expression Example 1: Evaluate the following logical expression: a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) ( 2 * 3 == 6) 1 && 1 (1 < 2) (6 == 6) 1 1 1 1 30
Logical Expression Example 2: Evaluate the following logical expression: Given a = 3, b = 4; c)!((5 * b <= 23 - a )) d)! ((b +3!= 8) &&( 3 * a < 2))!((5 * 4 <= 23 3))!(( 7!= 8 ) && ( 9 < 2 ))!(20 <= 20)! ( 1 && 0 )!(1)! ( 0) 0 1 31
Logical Expression An example program which using Logical Expression: #include <stdio.h> void main() { int mark; printf( \nenter your mark >> ); scanf( %d,&mark); if (mark >= 85 && mark <= 100) printf( \ngred A ); } else if( mark >=80 && mark <=84) printf( \ngred A- ); 32
Assignment Statement Format /sintax : variable = value; variable = constant; or variable = variable; variable = expression; Example : 1.average= ( 6 + 5) * 4; average 44 2.grossSalary = 1500; nettsalary = grosssalary + 200; grosssalary 1500 nettsalary 1700 3.price= 50.00; pay = price; ` price pay 50.00 50.00 33
Compound Assignment Statement Use more than one operator (=) Example : int a = b= c = d = e = 250; b =2, number =0; number = b = 10; int age = workhour = 0; 34
Arithmetic Expression Compound Assignment Statement Function To combine two different operator together. To simplify arithmetic operator Original function of operator does not affected Allowed combination are as follow: operator: +=, -=, *=, /=, %= 35
Compound Assignment Statement Example : Assignment Expression Meaning Operator += total + = 300 total = total+ 300 -= total - = count+ 300 total = total - (count + 300) *= total *=300 total = total * 300 /= total /= count 10 total = total / ( count 10) %= total % = 7 total = total % 7 36
Conditional operator The general synthax is Expression1? Expression2: expression 3; Value of expression1 is evaluate first. If value true, expression2 is performed otherwise expression3 is performed.
Comma operator A set of expression within parentheses and separated by commas can be assigned to variable The expression evaluated left to right and the value of last expression is the final value of variable