Download presentation
Presentation is loading. Please wait.
1
Relational Operations
Chapter 8 Arithmetic and Relational Operations Chapter 8: Arithmetic and Relational Operations
2
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
3
Assignment Statement Rules: lvalues appear on left of ‘=’
b = 50; b = a + b; lvalue rvalue 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
4
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
5
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
6
Arithmetic Operators Sample program fah2cel.c. Spot the error.
Chapter 8 Arithmetic Operators
7
Unary Operators Unary plus +, unary minus -
Examples: +12, , -7.2e-3 Chapter 8 Unary Operators
8
Binary Operators Additon: 5 + 2 is 7; 5.4 + 2.7 is 8.1
Subtraction: is 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? / 0? Modulus (only for integers): What is 5 % 2? 8 % 5? 15 % 5? 17 % -7? 15 % 0? Chapter 8 Binary Operators
9
Data Types of Expressions
Same-type operands: result inherits the type. 7 + 3 is integer; is float Mixed-type operands: result is of type that is more general. is float; 5.0 / 2 is float What is 5 / 2? 20 / 3? Chapter 8 Data Types of Expressions
10
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
11
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
12
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
13
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
14
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
15
Compound Assignment Operators
For statement in this form: variable = variable op expression; we may write: variable op= 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; Chapter 8 Compound Assignment Operators
16
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
17
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
18
Increment/Decrement Operators
Chapter 8 Increment/Decrement Operators
19
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
20
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
21
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
22
Equality and Relational 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); Chapter 8 Equality and Relational Operators
23
Equality and Relational Operators
Do not mix up == and =. Zero -- false; non-zero values -- true. if (123) printf("Hello!\n"); if (7+3) Chapter 8 Equality and Relational Operators
24
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
25
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
26
Logical Operators To combine conditions into more complex ones.
Logical AND: && Logical OR: || Logical NOT (negation): ! Evaluation from left to right. Chapter 8 Logical Operators
27
Logical Operators Functions of logical operators.
What is the value of a? int a; a = (3 > 5) || (5 > 1); Chapter 8 Logical Operators
28
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
29
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
30
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
31
Homework Try exercises behind chapter 8. Chapter 8 Homework
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.