Chapter 8 Arithmetic and Relational Operations Chapter 8

  • Slides: 31
Download presentation
Chapter 8 Arithmetic and Relational Operations Chapter 8: Arithmetic and Relational Operations 1

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

Assignment Statement q Format: variable = expression; q variable is an identifier representing variable

Assignment Statement q Format: variable = expression; q variable is an identifier representing variable name q expression can be: ua variable [eg: min = num; ] ua constant [eg: age = 20; ] ua combination of the above [eg: ave = (a 1+a 2+a 3/count); ] Chapter 8 Assignment Statement 2

Assignment Statement a = 40; b = 50; lvalue rvalue b = a +

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

Assignment Statement q Order of evaluation: right to left. u Example: a = z

Assignment Statement q Order of evaluation: right to left. u Example: a = z = 123; u 123 is assigned to z u The assignment (z = 123) returns the value 123, which is assigned to a. q 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 q Assigning a value to a variable of different type? int a,

Assignment Statement q 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 q Sample program fah 2 cel. c. q Spot the error. Chapter

Arithmetic Operators q Sample program fah 2 cel. c. q Spot the error. Chapter 8 Arithmetic Operators 6

Unary Operators q Unary plus +, unary minus q Examples: +12, -45. 80, -7.

Unary Operators q Unary plus +, unary minus q Examples: +12, -45. 80, -7. 2 e-3 Chapter 8 Unary Operators 7

Binary Operators q Additon: 5 + 2 is 7; 5. 4 + 2. 7

Binary Operators q Additon: 5 + 2 is 7; 5. 4 + 2. 7 is 8. 1 q Subtraction: 5 - 2 is 3; 5. 0 - 2. 3 is 2. 7 q Multiplication: 5 * 2 is 10; 3. 2 * 1. 5 is 4. 8 q Division: What is 5 / 2? 5. 0 / 2. 0? 0 / 25? 19 / 0? q 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 q Same-type operands: result inherits the type. 7 + 3

Data Types of Expressions q Same-type operands: result inherits the type. 7 + 3 is integer; 2. 3 + 4. 56 is float q 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 q What values are stored in these variables? float x,

Data Types of Expressions q 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 q In mixed-type expressions, the value of a more restricted type is automatically

Promotion q 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; q 13 is promoted to float before division is carried out. Chapter 8 Promotion 11

Cast q The cast operator (type) is used to explicitly change the type of

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

Precedence Rule q Precedence rule for arithmetic operators, from highest to lowest: u parentheses

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

Associativity Rule q For operators at the same precedence level, the associativity rule dictates

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

Compound Assignment Operators q For statement in this form: variable = variable op expression;

Compound Assignment Operators q For statement in this form: variable = variable op expression; we may write: variable op= expression; q Examples: u c += 7; u d -= 4; u e *= 5; u f /= 3; u 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 q Is ‘j *= k + 1’ equivalent to ‘j =

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

Increment/Decrement Operators q ++a or a++ equivalent to ‘a = a + 1’ or

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

Increment/Decrement Operators Chapter 8 Increment/Decrement Operators 18

Increment/Decrement Operators Chapter 8 Increment/Decrement Operators 18

Increment/Decrement Operators q Avoid using the ++ and -- operators in complex expressions in

Increment/Decrement Operators q 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 q Mathematical functions are available in the math library. Some examples are:

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

Equality and Relational Operators q Selection and repetition constructs require the use of conditions.

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

Equality and Relational Operators q Operators: u equal == u not equal != u

Equality and Relational Operators q Operators: u equal == u not equal != u greater than > u less than < u greater than or equal >= u less than or equal <= x == y x != y x>y x<y x >= y x <= y if (x < y) printf("%f is smaller than %fn", x, y); Chapter 8 Equality and Relational Operators 22

Equality and Relational Operators q Do not mix up == and =. q Zero

Equality and Relational Operators q Do not mix up == and =. q 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 q False expression returns 0; true expression returns 1. if

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

Equality and Relational Operators q Do not use == to compare equality of real

Equality and Relational Operators q Do not use == to compare equality of real numbers. q Real values may not be stored accurately. if ((a/3)*3 == a) printf("Hello!n"); q 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 q To combine conditions into more complex ones. q Logical operators: u

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

Logical Operators q Functions of logical operators. q What is the value of a?

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

Logical Operators q Examples: if (grader == 1 && age >= 65) ++snr. Females;

Logical Operators q Examples: if (grader == 1 && age >= 65) ++snr. Females; if (semester. Avg >= 90 || final. Exam >= 90) grade = 'A'; if !(grade == 'F') /* or (grade != 'F') */ printf("Student passed. n"); Chapter 8 Logical Operators 28

Logical Operators q Lazy (or short-circuit) evaluation of logical expressions: as soon as truth

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

Logical Operators q For logical OR, if front expression is true, the rest are

Logical Operators q For logical OR, if front expression is true, the rest are skipped. if (semester. Avg >= 90 || final. Exam >= 90) grade = 'A'; if (semester. Avg >= 90) is true, there is no need to evaluate (final. Exam >= 90). Chapter 8 Logical Operators 30

Homework Try exercises behind chapter 8. Chapter 8 Homework 31

Homework Try exercises behind chapter 8. Chapter 8 Homework 31