Chapter 8 Arithmetic and Relational Operations Chapter 8: Arithmetic and Relational Operations 1 Assignment Statement 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);] Chapter 8 Assignment Statement 2 Assignment Statement a = 40; b = 50; lvalue rvalue b = a + b; 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 Chapter 8 Assignment Statement 3 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. Chapter 8 Assignment Statement 4 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'; Chapter 8 Assignment Statement 5 Arithmetic Operators Sample program fah2cel.c. Spot the error. /* Converts degrees Fahrenheit to degrees Celsius * Author: Garfield Date: 25 July 1997 * Note: This program contains a subtle error too! */ #include <stdio.h> int main (void) { float fah, /* degrees in Fahrenheit */ cel; /* degrees in Celsius */ printf ("Enter degrees in Fahrenheit: "); scanf ("%f", &fah); cel = 5/9 * (fah - 32); printf ("That equals %f degrees in Celsius.\n\n", cel); return 0; } Chapter 8 Arithmetic Operators 6 Unary Operators Unary plus +, unary minus Examples: +12, -45.80, -7.2e-3 Chapter 8 Unary Operators 7 Binary 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? Chapter 8 Binary Operators 8 Data Types of Expressions 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? Chapter 8 Data Types of Expressions 9 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; Chapter 8 Data Types of Expressions 10 Promotion 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. Chapter 8 Promotion 11 Cast 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? Chapter 8 Cast 12 Precedence Rule Precedence rule for arithmetic operators, from highest to lowest: parentheses Unary operators ++, --, +, -, (type) Binary operators *, /, % Binary operators +, - Example: 3 * (12 - 7) Chapter 8 Precedence Rule 13 Associativity 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)) Chapter 8 Associativity Rule 14 Compound Assignment Operators For statement in this form: variable = variable op expression; we may write: variable op= expression; Examples: c += 7; d -= 4; e *= 5; f /= 3; g %= 9; Chapter 8 equivalent to c = c + 7; d = d - 4; e = e * 5; f = f / 3; g = g % 9; Compound Assignment Operators 15 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 Chapter 8 Compound Assignment Operators 16 Increment/Decrement 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. Chapter 8 Increment/Decrement Operators 17 Increment/Decrement Operators /* Pre-incrementing and post-incrementing */ #include <stdio.h> int main(void) { int c; c = 9; printf("%d\n", c); printf("%d\n", c++); printf("%d\n\n", c); c = 9; printf("%d\n", c); printf("%d\n", ++c); printf("%d\n", c); return 0; /* post-increment */ 9 9 10 9 10 10 /* pre-increment */ /* successful termination */ } Chapter 8 Increment/Decrement Operators 18 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? Chapter 8 Increment/Decrement Operators 19 Mathematical Functions 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. Chapter 8 Mathematical Functions 20 Equality and Relational Operators Selection and repetition constructs require the use of conditions. if (condition) { statements } Conditions are formed by equality operator and relational operators. Chapter 8 Equality and Relational Operators 21 Equality and Relational Operators Operators: equal == not equal != greater than > less than < greater than or equal >= less than or equal <= x == y x != y x>y x<y x >= y x <= y if (x < y) printf("%f is smaller than %f\n", x, y); Chapter 8 Equality and Relational Operators 22 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"); Chapter 8 Equality and Relational Operators 23 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); Chapter 8 Equality and Relational Operators 24 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. Chapter 8 Equality and Relational Operators 25 Logical Operators To combine conditions into more complex ones. Logical operators: Logical AND: && Logical OR: || Logical NOT (negation): ! Evaluation from left to right. Chapter 8 Logical Operators 26 Logical Operators Functions of logical operators. expr1 expr2 expr1 && expr2 expr1 || expr2 ! expr1 0 0 nonzero nonzero 0 nonzero 0 nonzero 0 0 0 1 0 1 1 1 1 1 0 0 What is the value of a? int a; a = (3 > 5) || (5 > 1); Chapter 8 Logical Operators 27 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"); Chapter 8 Logical Operators 28 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) Chapter 8 Logical Operators 29 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). Chapter 8 Logical Operators 30 Homework Try exercises behind chapter 8. Chapter 8 Homework 31