Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Operators and Expressions

Similar presentations


Presentation on theme: "Chapter 3 Operators and Expressions"— Presentation transcript:

1 Chapter 3 Operators and Expressions
PROGRAMMING IN ANSI C

2 Operators Arithmetic + - * / % ++ -- Relational
2/24/2019 Operators Arithmetic * / % Relational < <= > >= == != Logical ! && || Bitwise << >> ~ | ^ & Assignment = (Shorthand Assignment: += -= etc.) Comma , Explicit conversion (type) Conditional ?: Arrow -> Pointer * & Array element [ ] Number of bytes sizeof others ( ) - .

3 2/24/2019 Expressions An expression is a sequence of operands and operators that produces a single value. This value may be any data type except void.

4 How to learn operators and expressions?
2/24/2019 How to learn operators and expressions? When we study operators and expressions, we must pay attention to such 5 points: The function of operators. The relation of operators and operands: How many operands does the operator need? Which types does the operator require? The precedence of the operator. The associativity of the operator. The type of the calculated result.

5 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions 5 % 2 = -5 % 2 = 5 % -2 = -5 % -2 = 5 % 1 = 5 % 1.0 = 5 / - 2 = 5 / = +:Addition or unary plus e.g. 3+2,+3.5 - :Subtraction or unary minus e.g. 3-2,-3.5 * :Multiplication e.g. 3*2,3.5*2 / :Division e.g. 3/2,3.5/2 If the 2 operands both are integers, the result is an integer and its fractional part is truncated. e.g. 8/5=1 % :Modulo division e.g. 5%2 = 1 Both of the 2 operands must be integers . The sign of the result is the same as the dividend. 1 - 1 1 - 1 O - 2 - 2.5

6 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions +,-,*,/,% Precedence: (Unary) higher than * / % higher than + - Associativity:+,- (Unary): Right to left others:Left to right

7 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions Notice: If the operands are all integers, the result must be an integer. If one of the operands is a real number, the result must be a real number. The modulo division operator cannot used on real numbers but only integers, and the sign of result is the same as the sign of dividend.

8 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. e.g. a * b / c – ( 'f') * (5 % 3)

9 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions You must pay attention to : The expression “a multiplied by b” must be written to “a*b” but not “ab”; C doesn’t supply exponential operator, so you can write some multiplication, or you can use mathematic function pow(x,y) in the C function library to express xy; Notice the precedence of those arithmetic operators, and you should reasonably use parentheses.

10 Arithmetic Operators & Expressions
2/24/2019 Arithmetic Operators & Expressions When expressions include real values, then it is important to take necessary precautions to guard against certain computational errors. e. g. a = 1/3.0; b = a * 3.0; Don’t make any expression divide zero. Avoid data overflow.

11 Relational Operators & Expressions
2/24/2019 Relational Operators & Expressions < <= > >= == != Value of Relational Expression: 1(True) & 0(False) Precedence: < <= > >= higher than == != Arithmetic operators higher than relational operators. Associativity:Left to right Relational operators are used in the test condition of decision or loop statements to decide the course of action of running program.

12 Relational Operators & Expressions
2/24/2019 Relational Operators & Expressions int a=3, b=2, c=1, d, f; a > b c == a > b a < b + c d = a > b f = a > b > c 3>2,the value is 1 1==1, the value is 1 b+c=3, the value is 0 d=1 a>b is 1,1>c is 0,so: f = 0

13 Relational Operators & Expressions
2/24/2019 Relational Operators & Expressions Notice Avoid carrying out “==” and “!=” between real numbers. e.g / 3.0 * 3.0 == 1.0 /*not always 1 */ fabs ( 1.0 / 3.0 * ) < 1e-6 Pay attention to distinguish “=” and “==”. e.g. int a = 3, b = 2; a = b == a; /* a = 0 */

14 Logical Operators & Expressions
2/24/2019 Logical Operators & Expressions ! && || Precedence: ! higher than relational operators higher than && higher than || Associativity: ! : Right to left && , ||: Left to right Value of logical Expression: 1(True) & 0(False) Operands: 0 denotes false, others denote true. e.g && 0 a b !a a&&b a||b 1

15 Logical Operators & Expressions
2/24/2019 Logical Operators & Expressions int a = 4, b = 5; !a a&&b a||b !a||b 4&&0||2 5>3&&0||8<4-!0 'c'&&'d' !4 => 0 4 && 5 => 1 4 || 5 => 1 0 || 5 => 1 0 || 2 => 1 ((5>3)&&0)||(8<(4-(!0))) = 0||0 => 0 , || 100 => 1

16 Logical Operators & Expressions
2/24/2019 Logical Operators & Expressions Question:Write the expression of leap year. If the “year” is a leap year, it must satisfy one of the 2 conditions: 1) It can be divided exactly by 4, and it can’t be divided exactly by 100. 2) It can be divided exactly by 400. ( && ) year%4==0 year%100!=0 || year%400==0

17 Logical Operators & Expressions
2/24/2019 Logical Operators & Expressions Short-circuit When a logical expression is being evaluated, it is not each part linked by && or || all is evaluated. But only when it has to be evaluated, it is done. a&&b /* Only when a is true, b is evaluated If a is false, b is not evaluated.*/ a||b /* Only when a is false, b is evaluated If a is true, b is not evaluated.*/ a=1; b=2; c=3; d=4; m=1; n=2; ( m = a >b ) && ( n = c > d ); /* m=0,n=2*/

18 Assignment Operators & Expressions
2/24/2019 Assignment Operators & Expressions = Format:variable = expression Precedence : = lower than || Associativity: Right to left a = b = 5; int a = b = 5; Wrong! int a = 5, b = 5; Right!

19 Assignment Operators & Expressions
2/24/2019 Assignment Operators & Expressions  c = 5, b = c, a = b /*a=5, b=5, c=5*/  c = 6, a = 5+c /*a=11, c=6*/  b = 4, c = 6, a = b+c /*a=10, b=4, c=6*/ int a, b, c; a = b = c = 5; a = 5 + (c=6); a = (b=4) + (c=6);

20 Assignment Operators & Expressions
2/24/2019 Assignment Operators & Expressions “shorthand ” assignment operators: +=, -=, *=, /=, %= variable op = expression; is equivalent to: variable = variable op expression e.g. x * = y + 1; is equivalent to: x = x * (y + 1);

21 Assignment Operators & Expressions
2/24/2019 Assignment Operators & Expressions int a = 2; a += a -= a*a ;  a += a –= 4  a += (a = a – 4)  a += (a = -2)  a += a  a = a + a  a = (-2) + (-2)  a = -4

22 Increment and Decrement Operators
2/24/2019 Increment and Decrement Operators (10 / m) ++ Wrong! 10 / m ++ Right! ++ -- m ++; or m; is equivalent to: m = m + 1; m --; or m; is equivalent to: m = m - 1; Precedence : same as unary +, unary -, ! Associativity: Right to left

23 Increment and Decrement Operators
2/24/2019 Increment and Decrement Operators int m=5, n; n = 10 / m++; is equivalent to: n = 10/m; m++; n = 10 / ++m; is equivalent to: ++m; n = 10/m;

24 Increment and Decrement Operators
2/24/2019 Increment and Decrement Operators j=3; k=++j; j=3; k=j++; j=3; printf("%d",++j); j=3; printf("%d",j++); a=3;b=5;c=(++a)*b; a=3;b=5;c=(a++)*b; j=j+1; k=j; result:k=4, j=4 k=j; j=j+1; result :k=3, j=4  j=j+1; printf(); output:4  printf(); j=j+1; output:3  a=a+1; c=a*b; result :a=4,c=20  c=a*b; a=a+1; result :a=4,c=15

25 Increment and Decrement Operators
2/24/2019 Increment and Decrement Operators Notice The operand of ++ or -- must be a variable and not any expression or any constant.

26 Comma Operator & Expressions
2/24/2019 Comma Operator & Expressions expression 1, expression 2, ……, expression n Value of Relational Expression: the value of expression n. Precedence : the lowest Associativity: Left to right

27 Comma Operator & Expressions
2/24/2019 Comma Operator & Expressions a = 3*4, 5*2 ; b = (a = 3*4, 5*2) ; a=1; b=2; c=3; printf("%d,%d,%d", a, b, c); printf("%d,%d,%d", (a, b, c), b, c); a = 12 a = 12, b = 10 output: 1, 2, 3 output: 3, 2, 3

28 Implicit Type Conversion
2/24/2019 Implicit Type Conversion short & char int unsigned int long unsigned long float double long double The rules of conversion: P70 In all expressions except assignments, any implicit type conversions are made from a lower size type to a higher size type as shown here:

29 Implicit Type Conversion
2/24/2019 Implicit Type Conversion During assignment: If expression is real type and the variable is integer type, the value of expression will be truncated its fractional part. If expression is double type and the variable is float type, the value of expression will be round its digits. If expression is long type and the variable is int type, the value of expression will be drop its higher byte.

30 Implicit Type Conversion
2/24/2019 Implicit Type Conversion int i, x; float f; double d; long L; x = L / i i * f d; long float double Notice: in the whole process of type conversion, only the type of the interim value used in evaluation is converted, but all the types of variables are not converted. Except the variable “x” assigned a value, all the values of other variables are not changed. float float double int double

31 Explicit Type Conversion
2/24/2019 Explicit Type Conversion Form :(type-name) expression Precedence : same as unary +, unary -, !, ++, -- Associativity: Right to left e.g. (int) (x+y)  (int) x + y Notice: like the implicit type conversion, only the type of the interim value used in evaluation is converted, but all the types of variables are not converted. Of course, the values of these variables are not changed. main() { float x, y ; x = 3.6 ; y = (int) x * 2 ; printf("x=%.2f, y=%.2f", x, y); } x=3.60, y=6.00

32 2/24/2019 Homework Review Questions P78 3.1~3.8 & 3.10 write down in your exercise book Programming Exercises


Download ppt "Chapter 3 Operators and Expressions"

Similar presentations


Ads by Google