You are on page 1of 6
Chapter:2 Expression and Operator 24 Operators: = Operators are the symbols which are used to perform some operations. + Operators are classified as follow: 1. Arithmetic Operators: = These operators are used to perform mathematical operations, = Callows following arithmetic operators: Operator | Explanations Example ‘Answer + | Addition 33 ~__| Subtraction 24-16 + | Multiplication A* 10 1 | Division x/y. % | Module Division | 5%2 - About ‘%" operator: ©. Tt returns the remainder value. © Itean’t work with real numbers. (© Sign of result is always the sign of the first operand. * &:5%2 AN $%2 5% -2 + About ‘/' operator: ‘© If both operands are integer then the result is integer but if one operand is a real number then the result is real = Be5/2 Ans 5/20 An: 2. Unary Operators: ~ Unary operators have only one operand. © Increment and Decrement operators (++, --): Increment operator is used to increment the value by one. There are two increment operators available: Pre increment (++) and Post increment (a++). = Decrement operator is used to decrement the value by one. There are two decrement operators available: Pre decrement (--a) and Post decrement (a~). ~ Following are the operators: ‘Ans: Ans: 5 Operator Explanations Example ‘Answer (If a=5) ++a_| Increment first then Count=++a; and a=6 assign value to variable, ‘a++ | Assign first then increase | Count=ar+; and a=6 the value Decrementfirst then | Count= —a and a=4 assign value. Assign valueffirst then | Count: anda=a decrease value. Ms. Hiral A. Patel Page 1 Chapter:2 Expression and Operator © Unary minus (-): = To represent the negative number, this unary minus is used Ex: -34 (Negative 34 number.) = Torepresent the positive number, (+) operator can be used but itis optional = Ex: 34 or +34 (Positive 34 number. ~ Both are same) 3. Relational Operators: ~ These operators are used to compare two variables. + The result is either 0 or 1 (Means FALSE or TRUE-{Non Zero value} - These are used to decision, ~ Callows following relational operators: Operator Explanations Example Answer >| Isgreater than 12534 0 <__[sless than 12<34 Non zero \s greater thanequalto | M1>=40 O (if Ma = 12) A(ifM1=55) Is less than equal to Age <= 50 (if Age = 5) 4 (iF Age = 55) Is equal to ch O{iFch= 10) 1(ifCh=5) isnot equal to 65 [2100 4 4, Logical Operators: = Like relational operators, logical operators are used to check condition, - Callows following logical operations: Operator Explanations Example T Answer && | Perform logical AND | 3<5 && 6>34 o IL [Perform logical OR | 3«5 || 6>34 1 1 [Perform logical NOT I(3<5) o - ‘¥ operator reverses the result of given condition. = '&& and ‘||’ operators are used to compare multiple conditions, ~ _Truth table for logical operators are as follows: Conditiont | Condition? | _1(NOT) ‘BB (AND) TL(or) WCondition3) | Condition. && | Condition Condition? [[Condition2 a 1 0 1 1 1 o ° 1 0 1 1 o 1 0 0 o 0 5. Assignment Operators: ~ Assignment operators are used to assign result of expression to variable. = Itis also called as SHORT HAND Operators. Ms. Hiral A. Patel Page 2 Chapter:2 Expression and Operator Callows following assignment operators: Operator Example Explanations ‘Answer = [ARS 15 is assigned in ‘A158 variable A. Conditional Operator (or Ternary operator): (? :) Conditional operator is used to check the condition and execute expression. Syntax: condition ? expression1: expression2; First the condition is checked and if the result is non-zero number (TRUE) then expressiont is. evaluated and if itis zero (FALSE) then expression? is evaluated. Ex: Find the greater number from two numbers: Result= a>b ? Output: ifa=7 and b=4 7>4 Ans: Result=7 Hfa=10 and b=14 10> 14 Ans: Result Other operators: sizeof operator: ‘©. Itreturns numbers of bytes used with variable. (© Syntax: sizeof(variable_name}; © Ex:ifsizeof{m); (Returns 2 bytes if mis of int type.) (Returns 1 byte if m is of char type.) Comma operator: ‘© Comma operator is used to link multiple expressions. © Itperforms left to write, © Ex: for(i=0, i<5, H+) (First 20 is evaluated then i<5 and then it+ is evaluated) Bitwise operator: © Bitwise operators are used to manipulate data at bit level. © Following are the operators: Operator Explanations & | Bitwise AND Hl Bitwise OR 5 | Bitwise Exclusive OR <__| Shift Left >>__| Shift Right Ms. Hiral A. Patel Page 3 Chapter:2 Expression and Operator 22 Expressio Expressions are constructed using variables, constants and operators. Arithmetic Expression: Arithmetic expressions are used to represent mathematical expressions. Arithmetic operators are used with variables and constants to make arithmetic expression. It follows Mathematical rule of BODMAS (Bracket, Order, Division, Multiplication, Addition and Subtraction). Syntay Operand1 Arithmetic operator Operand2; (Operandi & operand2 can be one of the constant or variable or expression.) ‘The mathematical expressions and respective arithmetic expressions are shown below: “Mathematical Expression ‘Arithmetic Expression 35 35 akb or ab a*b abec or @ atb/e a(b-c} a*(b-c) pared Ba Px 5 Boolean Expression: Boolean expression is used to specify condition. ‘The output of Boolean expression is either one or zero value, (ie TRUE or FALSE.) There are two types: a. Relational Expressio Relational expression uses relational operators. ~ Itis used to check a condition. - Syntax: Operandi Arithmetic operator Operand2; (Operand1 & operand? can be one of the constant or variable or expression.) Be: . 5 = 65-9 © (atb) I= (74a) b. Logical Expression: Logical expression is used to join one or more relational expression, Itis used to check many conditions. = Ituses logical operators with relational expressions. Ms. Hiral A. Patel Page 4 Chapter: Expression and Operator = Syntax: Relational_Expression1 Logical_Operator Relational_Expression?; (Here relational_expression1 is not used with’ operator. - Be + (a==5) 88 (6>=9) #165 >m) + (de-a) |] (1955) 2.3 Evaluation and assignment of Expression: = Expressions are evaluated using assignment statements. ~ Syntax: Variable = expression; = When the statement s executed then frst the expression is evaluated and the result is assigned to the ‘given variable. - x © a=345-8; © b=a6+2-4; © c#=2°3; 2.3. Precedence and associati ity of Operators: = The precedence of operator species which operator will be evaluated first. ~The associativity specifies the direction of operator for evaluation; (Either left to right or right to left Category Operator Postfix oo>.+-- (ype) * & sizeot, Right to left Ms. Hiral A. Patel Page5 Chapter:2 Expression and Operator Exercise 2 then caleulate following expressions: ab 4j-75 iy leifis i-itjri, iejfie2; i+ 22; Page 6

You might also like