1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

  • Published on
    21-Dec-2015

  • View
    216

  • Download
    1

Embed Size (px)

Transcript

<ul><li> Slide 1 </li> <li> 1 Operators And Expressions </li> <li> Slide 2 </li> <li> 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators </li> <li> Slide 3 </li> <li> 3 Arithmetic Operators OperatorAction Subtraction, also unary minus +Addition *Multiplication /Division %Modulus --Decrement ++Increment </li> <li> Slide 4 </li> <li> 4 Precedence and Associativity Precedence of the Arithmetic operators: High ++ -- - (unary minus) * / % Low+ - - a * b c((- a) * b) c </li> <li> Slide 5 </li> <li> 5 inside parenthesesExpressions inside parentheses are evaluated first. 1 * (2 - 3) Operators on the same level of precedence are evaluated from left to right. (Associativity). 1 + 2 + 3 + 4 5 (((1 + 2) + 3) + 4) 5 </li> <li> Slide 6 </li> <li> 6 Increment &amp; Decrement Operators Provide a concise notation for incrementing or decrementing a variable by 1. Are unary operators. ++x or x++ --x or x-- Can be applied to variables but not to constants or ordinary expressions. ++i;legal cnt--;legal 777++;illegal ++(a * b -1); illegal </li> <li> Slide 7 </li> <li> 7 May either prefix or postfix the operand. Prefix++x; or Postfixx++; x = x + 1; ++ &amp; -- both cause a value of a variable to change in memory. ( Have a side effect). </li> <li> Slide 8 </li> <li> 8 Increment Postfix: i++; Expression value is the current value (Before you increment) then it increments. use - then increment Increment Prefix:++i; Expression value is the value After you increment. increment - then use Decrement Postfix:i--; use - then decrement Decrement Prefix:--i; decrement - then use </li> <li> Slide 9 </li> <li> 9 Examples x =10;y = ++x; y 11 x =10;y = x++; y 10 int i = 3, j = 2, k; i++;i j = ++i;ji k = i++;k i k = (--j+3) kj 4 5 5 5 6 4 7 </li> <li> Slide 10 </li> <li> 10 l = 4; n = 3; m = 2; x = l * n + m++;x After the assignment to x. m 14 3 </li> <li> Slide 11 </li> <li> 11 int a, b, c=0; a = ++c; b = c++; a = ? b = ? c= ? int b = 2, d = 4; 7--b*++d 7-((-b)*(++d)) ? int j = 2, k = 3, m = 4; j*=k=m+5 j=(j*(k=(m+5))) ? </li> <li> Slide 12 </li> <li> 12 int a,b; a = 1; b = 12; printf (a+++b = %d/n, a+++b); a = 1; b = 12; printf (a++ +b = %d/n, a++ +b); a = 1; b = 12; printf (a+ ++b =% d/n, a+ ++b); </li> <li> Slide 13 </li> <li> 13 Relational and Logical Operators Relational refer to the relationship that value can have with one another. Logical refers to the ways these relationships can be connected. True is any value other than zero. False is zero. </li> <li> Slide 14 </li> <li> 14 Relational Operators: OperatorAction &gt;Greater than &gt;=Greater than or equal </li> <li> 16 Precedence and Associativity High! &gt; &gt;= &lt; 10); __?"&gt; </li> <li> 17 Both are lower in precedence than the arithmetic operators. 10 &gt; 1 + 12 10 &gt; (1 + 12) FALSE 0 Associativity left to right. int x; x = 100; printf(''%d", x&gt;10); __? </li> <li> Slide 18 </li> <li> 18 !A is false (0) if As value is: __. is true (1) if As value is: __. !!5 ! (!5)! (0) 1 NOT (Any NON-zero)0 5 &amp;&amp; 3 ? Examples </li> <li> Slide 19 </li> <li> 19 inti, j = 1; j = j &amp;&amp; (i = 2); 1) (i=2)i 2) &amp;&amp;j &amp;&amp; 2 true &amp;&amp; true1 3) =j 1 1 2 ( ) needed </li> <li> Slide 20 </li> <li> 20 ji j = j &amp;&amp; (i = = 3); 1) (i = = 3) false0 2) &amp;&amp; j&amp;&amp; 0 0 3) = j ( ) not needed 1 21 0 </li> <li> Slide 21 </li> <li> 21 ji j = j || (i/2) 1) (i/2)(2/2) 1 2) ||j|| 1true1 3) =j 02 0 1 ( ) not needed </li> <li> Slide 22 </li> <li> 22 ji j = !j &amp;&amp; (i = i + 1); 1)i + 13 2)=i 3)!!j !1 0 4)&amp;&amp;0 &amp;&amp; 3 5)=j 3 21 0 </li> <li> Slide 23 </li> <li> 23 The Comma Operator lLowest precedence of all the operators. lCauses a sequence of operations, do this and this and this. lIs a binary operator. expression_1, expression_2 lAssociates left to right. expression_1 is evaluated first expression_2 is evaluated second x = (y=3, y+1); x 4 ( ) needed </li> <li> Slide 24 </li> <li> 24 l The Comma Expression as a whole has the value and type of expression_2. inti = 2; j = 4; j = i++, i - j; * i * j(3-4) 3 </li> <li> Slide 25 </li> <li> 25 It allows multiple initializations and multiple processing of indices. for (sum=0, i=1; i9 ? 100 : 200; x = 10; if(x&gt;9) y = 100; else y = 200; Examples"&gt; </li> <li> 29 x = 10; y = x&gt;9 ? 100 : 200; x = 10; if(x&gt;9) y = 100; else y = 200; Examples </li> <li> Slide 30 </li> <li> 30 int i, h, j = 2; i = (j==2) ? 1:3; k = (i&gt;j) ? i:j; ( ) not needed i get 1 k get max of I or j </li> <li> Slide 31 </li> <li> 31 This statement does what? c = (c &gt; =a &amp;&amp; c &lt; = z) ? c - (z - Z):c; IF True - have a Lower case letter in the variable C. Exper2: c - (z - Z) will give Capital Letter of whatever is in C. e.g. a - (z - Z) 97 - (122 90) = 65 which is A. IF False Capital Letter and leaves it alone. </li> <li> Slide 32 </li> <li> 32 Expressions An expression in C is any valid combination of operators, constants, functions and variables. An statement is a valid expression followed by a semicolon. func(); /* a function call */ a = b+c; /* an assignment statement */ b+f(); /* a valid, but strange statement */ ; /* an NULL statement */ </li> <li> Slide 33 </li> <li> 33 Null Statement:; [A semi-colon alone by itself]. Can be useful in loops &amp; conditional statements. The body of a loop could be empty. Scanf(%d, &amp;x); While(printf(%d,x),scanf(%d,&amp;x)==1) ; </li> <li> Slide 34 </li> <li> 34 Spacing and Parentheses Redundant or additional parentheses do not cause errors or slow down the execution of an expression. x=10/y-(127/x); x = y/3-34*temp+127; x = (y/3) - (34*temp) + 127; </li> </ul>

Recommended

View more >