310 likes | 318 Views
This chapter covers arithmetic and relational operations in programming, including assignment statements, arithmetic operators, data types, and logical conditions.
E N D
Arithmetic and Relational Operations Chapter 8 Chapter 8: Arithmetic and Relational Operations
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
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
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
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
Sample program fah2cel.c. Spot the error. Arithmetic Operators Arithmetic Operators
Unary plus +, unary minus - Examples: +12, -45.80, -7.2e-3 Unary Operators Unary Operators
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
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
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
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
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
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
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
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
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
++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
Increment/Decrement Operators Increment/Decrement Operators
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
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
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
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
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
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
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
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
Functions of logical operators. Logical Operators • What is the value of a? int a; a = (3 > 5) || (5 > 1); Logical Operators
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
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
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
Try exercises behind chapter 8. Homework Homework