Relational Expressions in C --------------------------- Terms ----- flow of control Refers to the order in which a program's statements are executed. sequential flow Step-by-step progression of statements in a program. Normal flow of program execution unless otherwise directed. selection statements Provide the ability to select which statement will be executed next (take the flow out of sequential flow). repetition statements Provide the ability to go back and repeat a set of statements (looping control flow of statements). relational expressions Expressions used to compare operands. Simple Relational Expressions ----------------------------- age > 40 length <= 20 hours < 8 flag == done Valid Relational Expressions ---------------------------- relational meaning example operator ---------- ----------------------------- -------------------- < less than i < 32 > greater than i > 89 <= less than or equal to i <= 13 >= greater than or equal to i >= -44 == equal to i == 8 != not equal to i != 0 ---------- logical operator ---------- || logical OR a == 6 || a == 7 && logical AND b == 5 && c < 10 Invalid Relational Expressions ------------------------------ length =< 10 should be <= flag = = done should be == 3 >> 2 should be > Format of an Expression ----------------------- The simple format is of the form: operand1 relational-operator operand2 An example, again, is: a < 2 The more complex form is: (operand1 rel-operator operand2) logical-operator (operand3 rel-operator operand4) logical-operator etc... Example are: (a < 3) && (b > c) && (rad == 2) (apple == banana) || (i <= 10) ((i < 10) && (i > 5)) || (j < 10) Evaluation of Relational Expressions ------------------------------------ Given the following assignments: char key = 'm'; int i = 5; int j = 7; int k = 12; double x = 22.5; Example 1 --------- i + 2 == k - 1 5 + 2 == 12 - 1 7 == 11 False, since 7 is not equal to 11. False is represented by 0 (true us represented by anything but 0). Example 2 --------- key + 3 > 'p' 'm' + 3 > 'p' 'p' > 'p' False, since 'p' is equal to 'p'. Exercises 4.1 -- Problem #4 --------------------------- a = 5 b = 2 c = 4 d = 5 a) a==5 5==5 True b) b*d == c*c 2*5 == 4*4 10 == 16 False c) d%b*c > 5 || c%b*d < 7 5%2*4 > 5 || 4%2*5 < 7 5%8 > 5 || 4%10 < 7 5 > 5 || 4 < 7 False || True True This is True since either one is acceptable in the logical OR.