Presentation is loading. Please wait.

Presentation is loading. Please wait.

APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.

Similar presentations


Presentation on theme: "APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses."— Presentation transcript:

1 APS105 Deciding 1

2 Comparing 2

3 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses Evaluating a relational expr gives a value 3 > 2 // evals to 1 (i.e., true) 10 < 9 // evals to 0 (i.e., false) 3

4 Relational Operators < Less than > Greater than <= less than or equal to >= greater than or equal to != is not equal to == is equal to –DANGER: don’t use ‘=‘ to compare! –A very common error/bug, check for it! x = 5; // x is changed to hold 5 x == 5; // results in 0 or 1 4

5 Mixing with Relational Operators Example: int x = 5; double y = 5.0; x == y; // Example: 1+2 <= 10-7; // 5

6 Comparing Characters Recall: –characters encoded in ASCII have a value –Eg., ‘A’ is 65 in ASCII –Hence can compare characters to integers Example: 66 > ‘A’; // Confusing: –Note that ‘0’ zero is different than 0 letter “oh” –Eg., ‘0’ is 48 in ASCII –Ex: ‘0’ == 0; // evaluates to 0 (false) 6

7 Common ASCII Values Hence these are all true:. CharactersValues ‘ ‘ (blank)32 ‘0’..’9’48..57 ‘A’..’Z’65..90 ‘a’..’z’97..122 7

8 if Statements 8

9 I’ll Do it on One Condition… if statement –Evaluates a relational expression –Performs the next statement if result was true General form: if ( ) 9

10 if statement example Example:. 10

11 if/else General form: if ( ) else Example:. 11

12 if with Blocks How to have multiple statements inside an if: –Use blocks (a.k.a. compound statements) –{….} Example:. 12

13 if/else with Blocks if ( ) { … } else { … } 13

14 Common Mistake Can you spot it? if (income < expenses); printf(“Can I borrow some money?\n”); 14

15 Assignment in an Expression What will happen in this code? int x; if (x=3) printf(“%d\n”,x); 15

16 Logic and Evaluation 16

17 Boolean Operators Operations on boolean values (bool or 0/1) –! not (inverse) –&& and –|| or Precedence: –! then && then || 17

18 Boolean Operators: Examples ABA && BA || B false true false true A!A false true 18

19 Lazy Evaluation Lazy Evaluation: –Don’t do extra work if you don’t have to Example: 3 < 2 && 8 < 9 C implements lazy evaluation –Also know as “short-circuit evaluation” 19

20 Lazy Evaluation (cont’d) LHS && RHS –Don’t evaluate RHS if LHS is false –Since we already know result will be false LHS || RHS –Don’t evaluate RHS if LHS is true –Since we already know result will be true Lazy evaluation can be useful: (why?) if (x != 0 && 10/x > 1.0) 20

21 Useful Trick: DeMorgan’s Laws !x || !y is equivalent to !(x && y) !x && !y is equivalent to !(x || y) Who cares? –Can be very useful in rearranging expressions Ie., making them smaller or easier to read –You’ll see them again in logic/circuits classes At a restaurant: you can have soup OR salad. 21

22 DeMorgan’s Laws: Example Why might my monitor not work?. Relationships:. 22

23 Multiple Conditions 23

24 Multiple if’s General Form: if ( ) else if ( ) else if ( ) …. else 24

25 Example Assign largest of x, y, z to max. 25

26 Nested if’s General Form: if ( ) <statement else else if ( ) …. 26

27 Nested if Assign largest of x, y, z to max. 27

28 Dangling else problem Which if does the else belong to?.. if ( ) else.. 28


Download ppt "APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses."

Similar presentations


Ads by Google