1 / 31

Arithmetic and Relational Operations Chapter 8

This chapter covers arithmetic and relational operations in programming, including assignment statements, arithmetic operators, data types, and logical conditions.

tamikad
Download Presentation

Arithmetic and Relational Operations Chapter 8

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arithmetic and Relational Operations Chapter 8 Chapter 8: Arithmetic and Relational Operations

  2. Format: variable = expression; variable is an identifier representing variable name expression can be: a variable [eg: min = num;] a constant [eg: age = 20;] a combination of the above [eg: ave = (a1+a2+a3/count);] Assignment Statement Assignment Statement

  3. Rules: lvalues appear on left of ‘=’ rvalues appear on right of ‘=’ lvalues may be used as rvalues, but not vice versa variable names are lvalues constants and expressions are rvalues a = 40; b = 50; b = a + b; lvalue rvalue Assignment Statement Assignment Statement

  4. Order of evaluation: right to left. Example: a = z = 123; 123 is assigned to z The assignment (z = 123) returns the value 123, which is assigned to a. An assignment statement has a ‘value’. For example, ‘count = 12’; returns the value 12, besides assigning 12 to count. Assignment Statement Assignment Statement

  5. Assigning a value to a variable of different type? int a, b; float x; char c; a = 23.5; x = 12; c = 109; b = 'n'; Assignment Statement Assignment Statement

  6. Sample program fah2cel.c. Spot the error. Arithmetic Operators Arithmetic Operators

  7. Unary plus +, unary minus - Examples: +12, -45.80, -7.2e-3 Unary Operators Unary Operators

  8. Additon: 5 + 2 is 7; 5.4 + 2.7 is 8.1 Subtraction: 5 - 2 is 3; 5.0 - 2.3 is 2.7 Multiplication: 5 * 2 is 10; 3.2 * 1.5 is 4.8 Division: What is 5 / 2? 5.0 / 2.0? 0 / 25? 19 / 0? Modulus (only for integers): What is 5 % 2? 8 % 5? 15 % 5? 17 % -7? 15 % 0? Binary Operators Binary Operators

  9. Same-type operands: result inherits the type. 7 + 3 is integer; 2.3 + 4.56 is float Mixed-type operands: result is of type that is more general. 7 + 3.0 is float; 5.0 / 2 is float What is 5 / 2? 20 / 3? Data Types of Expressions Data Types of Expressions

  10. What values are stored in these variables? float x, y; int n; x = 13 / 5; y = 9 * 0.5; n = 9 * 0.5; Data Types of Expressions Data Types of Expressions

  11. In mixed-type expressions, the value of a more restricted type is automatically promoted to a more general type. float x; x = 13 / 5.0; 13 is promoted to float before division is carried out. Promotion Promotion

  12. The cast operator (type) is used to explicitly change the type of the operand for the operation. float x; x = (float) 13 / 5; What happens without the (float) cast? Cast Cast

  13. Precedence rule for arithmetic operators, from highest to lowest: parentheses Unary operators ++, --, +, -, (type) Binary operators *, /, % Binary operators +, - Example: 3 * (12 - 7) Precedence Rule Precedence Rule

  14. For operators at the same precedence level, the associativity rule dictates the order of evaluation: Unary operators: right to left Binary operators: left to right Example: 3 * (12 - 7) % 4 - (16 / (2 + 2 * 3 - 1)) Associativity Rule Associativity Rule

  15. For statement in this form: variable = variable op expression; we may write: variableop= expression; Examples: c += 7; equivalent to c = c + 7; d -= 4; d = d - 4; e *= 5; e = e * 5; f /= 3; f = f / 3; g %= 9; g = g % 9; Compound Assignment Operators Compound Assignment Operators

  16. Is ‘j *= k + 1’ equivalent to ‘j = j * k + 1’ or ‘j = j * (k + 1)’? What is the result of this? (m + n) *= 2 Compound Assignment Operators Compound Assignment Operators

  17. ++a or a++ equivalent to ‘a = a + 1’ or ‘a += 1’ --a or a-- equivalent to ‘a = a -1’ or ‘a -= 1’ Pre-increment (pre-decrement): Increment (decrement) variable, then use its value. Post-increment (post-decrement): Use the variable’s value, then increment (decrement) it. Increment/Decrement Operators Increment/Decrement Operators

  18. Increment/Decrement Operators Increment/Decrement Operators

  19. Avoid using the ++ and -- operators in complex expressions in which the variables they are applied appear more than once: x = 5; i = 2; y = i * x + ++i; is y assigned the value 13 or 18? Increment/Decrement Operators Increment/Decrement Operators

  20. Mathematical functions are available in the math library. Some examples are: pow(): to compute powers fabs(): to return absolute values sqrt(): to compute square roots Need to include math.h file in your program, and compile with -lm option: cc -lm prog.c Study the function prototypes in math.h. Mathematical Functions Mathematical Functions

  21. Selection and repetition constructs require the use of conditions. if (condition) { statements } Conditions are formed by equality operator and relational operators. Equality and Relational Operators Equality and Relational Operators

  22. Operators: equal == x == y not equal != x != y greater than > x > y less than < x < y greater than or equal >= x >= y less than or equal <= x <= y if (x < y) printf("%f is smaller than %f\n", x, y); Equality and Relational Operators Equality and Relational Operators

  23. Do not mix up == and =. Zero -- false; non-zero values -- true. if (123) printf("Hello!\n"); if (7+3) printf("Hello!\n"); Equality and Relational Operators Equality and Relational Operators

  24. False expression returns 0; true expression returns 1. if (3 < 7) printf("Hello!\n"); a = (123 > 321); printf(”%d\n", a); Equality and Relational Operators Equality and Relational Operators

  25. Do not use == to compare equality of real numbers. Real values may not be stored accurately. if ((a/3)*3 == a) printf("Hello!\n"); To test equality of 2 real numbers, test their difference instead (use fabs()), and take them as equal if the difference is small enough. Equality and Relational Operators Equality and Relational Operators

  26. To combine conditions into more complex ones. Logical operators: Logical AND: && Logical OR: || Logical NOT (negation): ! Evaluation from left to right. Logical Operators Logical Operators

  27. Functions of logical operators. Logical Operators • What is the value of a? int a; a = (3 > 5) || (5 > 1); Logical Operators

  28. Examples: if (grader == 1 && age >= 65) ++snrFemales; if (semesterAvg >= 90 || finalExam >= 90) grade = 'A'; if !(grade == 'F') /* or (grade != 'F') */ printf("Student passed.\n"); Logical Operators Logical Operators

  29. Lazy (or short-circuit) evaluation of logical expressions: as soon as truth value can be determined, later expressions are skipped. For logical AND, if front expression is false, the rest are skipped. if (grader == 1 && age >= 65) ++snrFemales; If (grader == 1) is false, there is no need to evaluate (age >= 65) Logical Operators Logical Operators

  30. For logical OR, if front expression is true, the rest are skipped. if (semesterAvg >= 90 || finalExam >= 90) grade = 'A'; if (semesterAvg >= 90) is true, there is no need to evaluate (finalExam >= 90). Logical Operators Logical Operators

  31. Try exercises behind chapter 8. Homework Homework

More Related