Today’s Agenda

1 / 23

# Today’s Agenda - PowerPoint PPT Presentation

Today’s Agenda. Operators and Expressions Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment and Decrement Operators Conditional Operators Special Operators. Arithmetic Operators. Arithmetic Expression. Let int x = 15; int y = 6; Example:

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## Today’s Agenda

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Today’s Agenda • Operators and Expressions • Arithmetic Operators • Relational Operators • Logical Operators • Assignment Operators • Increment and Decrement Operators • Conditional Operators • Special Operators

2. Arithmetic Operators

3. Arithmetic Expression • Let int x = 15; int y = 6; Example: x-y = 9 x+y = 21 x*y = 90 x/y = 2 (decimal part truncated) x%y = 3 (remainder of the division) Q. If x and y are declared as float then? Q. If x is integer and y is float then?

4. More Examples: Write a equivalent C expression for given algebraic expressions. • (a+b)(c+d) • ax2+bx+c • pr2+2prh • s = ut + 1/2at2 • T = (m1m2/m1+m2)

5. Relational Operators

6. Examples: 5 <= 7 TRUE 5 < -8 FALSE 11 < 8+4 TRUE x+y == a+b TRUE if value of x+y is equal to value of a+b -20 >= 0 FALSE Q. Are !(a>b) and a<=b same?? Q. Are !(a==b) and a!=b same??

7. Logical Operators Used to test more than one condition Example: a<b && y==5 Expression is TRUE only if a<b is TRUE and y==5 is TRUE If either or both of them are FALSE then expression becomes FALSE

8. Assignment Operators Simple Assignment Operators a = a+1 a = a-1 a =a*(b+1) a = a/(b+1) a = a%b Shorthand Operators a+= 1 a-= 1 a*=b+1 a/=b+1 a%=b Q. b*=b; is equivalent to?? Short hand statement is more concise and easier to read Caution: x*=y+2 is x=x*(y+2) and not x = x*y+2

9. Increment and Decrement Operators ++ Adds one to the operand -- Subtracts one from the operand Both are unary operators ++i is equivalent to: i = i+1 --i is equivalent to: i = i-1 Is ++i and i++ are same?? Increment and Decrement operators can only be applied to variables. An expression like (i+j)++ is illegal.

10. 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 prefix the variable is incremented (or decremented) first and then the expression is evaluated using the new valueof the variable. Ex. m = --n;

11. Postfix and Prefix Examples (1) j =10; i =j++; What is the values of i and j? j =10; i=++j; What are the values of i and j?

12. Postfix and Prefix Examples (2) #include <stdio.h> int main (void) { int x, w=20, z=7; x = ((++z) – (w--)) % 5; printf (“Value of x = %d, w = %d, z = %d”,x, w, z); }

13. Conditional Operator • Also called Ternary operator Syntax expression1 ? expression2 : expression3 Example: a=5; b=10; x =(a>b) ? a : b; What will be the value of x??

14. Special Operators The Size of Operator Returns the number of bytes the operand occupies Examples: p=sizeof(value); q=sizeof(float); r=sizeof(long int);

15. The comma operator • Used to link the related expressions together. • Evaluation starts from left to right. • And the value of the right most expression is the value of the combined expression. Example: value = (a = 6, b = 7, c = 8, a+b+c); Value contains 21 Use of comma operator for (i = 1, j = 15; i<j; i++, j--)

16. Precedence and Associativity? Operators have rules that are used to determine how expressions are evaluated. Precedence and associativity deal with the evaluation order within expressions Precedencerules specify the order in which operators of different precedence level are evaluated Associativity rules decides the order in which multiple occurrences of the same level operator are applied.

17. Rules of Evaluation of Expression First, parenthesized sub expression from left to right are evaluated. If parentheses are nested, the evaluation begins with the innermost sub expression. The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions. The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.

18. Examples: Evaluate the following expressions 1) z = 2*3/4+4/4+8-2+5/7 z = ?? 2) y = 4/5*6+2/7+4 y = ?? 3) Let int a,b; float c,d; a=5;b=7; c=4.0;d=3.0; Then compute x = a/b*(c+d)/a-b+c/d

19. Example What is the output of the following piece of code ?? #include <stdio.h> int main (void) { int a, b=21, c=7; a = ++c – b-- % 5; printf (“Value of a = %d, b = %d, c = %d”,a, b, c); }

20. Example What is the output of the following piece of code ?? #include <stdio.h> int main (void) { int a, b=21, c=7; a = b+++++c; // Error. To remove write a = b++ + ++c printf (“Value of a = %d, b = %d, c = %d”,a, b, c); }

21. Precedence of Arithmetic Operators Priority can be overruled by parenthesis (1+2)*3 Expression inside parenthesis are evaluated first so (1+2)*3 gives 9 Now consider the expression 1+2-3+4-5 Note: + and – have the same precedence Associativity rule is used to determine how to evaluate the above expression. Associativity for Arithmetic Operators is L to R Try: x = 10 - 16/4 + 4 * 3 - 4

22. Example #include <stdio.h> int main (void) { int a=2 , b=7 , c=8 , d; d = a*=b+=c%2||1; printf ("a=%d b=%d c=%d d=%d",a,b, c, d); return (0); }