Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals & Boolean Expressions

Similar presentations


Presentation on theme: "Conditionals & Boolean Expressions"— Presentation transcript:

1 Conditionals & Boolean Expressions
Lecture 4 Conditionals & Boolean Expressions Richard Gesick

2 Topics Flow of Control Flow Diagram bool definition
Relational Operators if statements else statements

3 A Flow Diagram Start End Ask for $$ Blame it on someone else
Lay low and keep hidden Did you break anything? Yes Yes No Have your parents asked about it? Yes Are you lying? No Are you passing your classes? No No End Ask for $$ Yes

4 The Boolean A bool is a something that resolves to true or false
You can get Boolean values several different ways Simple Complex These bools will be used (later) to make decisions

5 Relational Operators (for primitive data types)
> greater than < less than == equals != not equal >= greater than or equal <= less than or equal Notice that all of these return us a true or false value

6 Relational Operator Examples
Literal Example 5 > // true 6 != 6 // false true == (4 <= 2) // false ‘c’ != ‘b’ // true !false // true Variable Example int num1 = 5; short num2 = 7; bool result = num2 > num1; //Note: result is now true

7 && and || Operators These operators check for multiple conditions
&& (AND) needs both the left and the right to be true in order to return true || (OR) needs either one (or both) to be true to return true Examples: ( (6 > 5) && ( ‘c’ == ‘b’) ) // false ( (6 > 5) && ( 7 < 9) ) // true ( (6 > 5) || ( ‘c’ == ‘b’) ) // true ( (6 > 6) || ( ‘c’ == ‘b’) ) // false

8

9

10 int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y x + 2 < y x
int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y x + 2 < y x != y x + 3 >= y y == x y == x+2 true false

11 EXPRESSION VALUE 7 == 7 13 < 100 -17. 32. = -17. 32 -3. 0 == 0
true false

12 Suppose we have three ints x, y, and z, and we want to test if x is less than both y and z. A common error is to express the condition this incorrect way: x < y && z // compiler error Each operand of a logical operator must be a boolean expression. This is correct: x < y && x < z

13 Short-Circuit Evaluation
For any logical operator, the operands are evaluated left to right If the result of the logical operation can be determined after evaluating the first operand, the second operand is not evaluated. If the first operand of an || is true, the result will be true If the first operand of an && is false, the result will be false

14 int age = 75; bool test; test = ( age > 18 && age < 65 ); C
int age = 75; bool test; test = ( age > 18 && age < 65 ); C.Wln( age + " > 18 && " + age + " < 65 is " + test ); // short circuitry with AND test = ( age < 65 && age > 18 ); C.Wln( age + " < 65 && " + age + " > 18 is " + test ); // short circuitry with OR test = ( age > 65 || age < 18 ); C.Wln( age + " > 65 || " + age + " < 18 is " + test ); // AND has higher precedence than OR test = ( age > 65 || age < 18 && false ); C.Wln( age + " > 65 || " + age + " < 18 " + " && false is " + test ); // use of parentheses to force order of execution test = ( ( age > 65 || age < 18 ) && false ); C.Wln( "( " + age + " > 65 || " + age + " < 18 ) " + "&& false is " + test );

15 What is the value? float x = 3.0, y = 4.0, z = 2.0; EXPRESSION VALUE 1. (x > z) && (y > z) 2. (x + y / z) <= (z > x) || (z > y) 4. !(y == z) 5. (x == 1.0) || (x == 3.0) 6. (z < x) && (x < y) 7. (x <= z) || (x >= y) 8. !(x > y) || y + z >= x – z 9. !((x > y) || ((y + z) >= (x – z))) true false

16 Now Let’s Do Something! Using the if statement, we can decide whether or not to execute some code Has format: if (<boolean value>) { // all the code that’s in here will only execute // if and only if the boolean above is true }

17 “if” Example class Skeleton { public static void Main ( ) {
int start = 5; int end = 19; if (start < end ) { Console.WriteLine ( “A” ); } Console.WriteLine ( “B” );

18 “if” Example A B class Skeleton { public static void Main ( ) {
int start = 5; int end = 19; if (start < end ) { Console.WriteLine ( “A” ); } Console.WriteLine ( “B” ); Output A B

19 “if” Example class Skeleton { public static void Main ( ) {
int start = 5; int end = 19; if (start > end ) { Console.WriteLine ( “A” ); } Console.WriteLine ( “B” );

20 “if” Example B class Skeleton { public static void Main ( ) {
int start = 5; int end = 19; if (start > end ) { Console.WriteLine ( “A” ); } Console.WriteLine ( “B” ); Output B

21 The “else” statement if has a counterpart – the else statement
If the if clause didn’t execute, the else clause will! Has format: if (<boolean value>) { // statements that execute if the boolean is true } else { // statements that execute if the boolean is false Notice only one set of statements executes, no matter what

22 “else” Example class Skeleton { public static void Main ( ) { int start = 5; int end = 19; if (start > end ) { Console.WriteLine ( “A” ); } else { Console.WriteLine ( “B” );

23 “else” Example class Skeleton { public static void Main ( ) { int start = 5; int end = 19; if (start > end ) { Console.WriteLine ( “A” ); } else { Console.WriteLine ( “B” ); Output B

24 “else” Example class Skeleton { public static void Main ( ) { int start = 5; int end = 19; if (start < end ) { Console.WriteLine ( “A” ); } else { Console.WriteLine ( “B” );

25 “else” Example class Skeleton { public static void Main ( ) { int start = 5; int end = 19; if (start < end ) { Console.WriteLine ( “A” ); } else { Console.WriteLine ( “B” ); Output A

26 else if’s Selecting one from many
As soon as one is true, the rest are not considered Has format: if (<boolean value>) { // statements that execute if the above boolean is true } else if (<boolean value>){ else { // something that executes if nothing matched above

27 Combining into “else if”s (Selecting one from many)
class Skeleton { public static void Main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { Console.WriteLine ( “A” ); } else if (start < end) { Console.WriteLine ( “B” );

28 Combining into “else if”s (Selecting one from many)
class Skeleton { public static void Main ( ) { int start = 5; int middle = 8; int end = 19; if (start < middle ) { Console.WriteLine ( “A” ); } else if (start < end) { Console.WriteLine ( “B” ); Output A

29 else catch-all Example
class Skeleton { public static void Main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { Console.WriteLine ("A"); } else if (start > end) { Console.WriteLine ("B"); else { Console.WriteLine ("C");

30 else catch-all Example
class Skeleton { public static void Main ( ) { int start = 5; int middle = 8; int end = 19; if (start > middle ) { Console.WriteLine ("A"); } else if (start > end) { Console.WriteLine ("B"); else { Console.WriteLine ("C"); Output C

31 Conclusion Boolean values can be generated several ways
Use the if statement to decide which path to take Use the else to take the alternate path The else-if statements allow for selecting one from many

32 Indentation The statement controlled by the if statement is indented to indicate that relationship The use of a consistent indentation style makes a program easier to read and understand Although it makes no difference to the compiler, proper indentation is crucial "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding

33 Indentation 2 Remember that indentation is for the human reader, and is ignored by the computer if (total > MAX) C.O.Wln ("Error!!"); errorCount++; Despite what is implied by the indentation, the increment will occur whether the condition is true or not

34 Do not put a semicolon after the condition
Do not put a semicolon after the condition. Doing so indicates that the true block is empty and can cause a logic error at run time.

35 What went wrong? This is only supposed to display “HEALTHY AIR” if the air quality index is between 50 and 80. But when you tested it, it displayed “HEALTHY AIR” when the index was 35. int AQIndex ; AQIndex = 35 ; if (50 < AQIndex < 80) C.Wln(“HEALTHY AIR“) ;

36 Analysis of Situation AQIndex = 35; According to the precedence chart, the expression (50 < AQIndex < 80) means (50 < AQIndex) < 80 because < is Left Associative (50 < AQIndex) is false (false < 80) is ???

37 Corrected Version AQIndex = 35 ;
int AQIndex ; AQIndex = 35 ; if ( (50 < AQIndex) && (AQIndex < 80) ) C.Wln(“HEALTHY AIR“) ;

38 What happens if you omit braces?
if ( (carDoors == 4 ) && (driverAge > 24) ) premium = ; C.Wln(“ LOW RISK “); else premium = ; C.Wln(“ HIGH RISK ”); monthlyPayment = premium / ; COMPILER ERROR OCCURS. The “if clause” is the single statement following the if.

39 Braces can only be omitted when each clause is a single statement
if ( lastInitial <= ‘K’ ) volume = 1; else volume = 2; C.Wln(“Look it up in volume # “ + volume + “ of NYC phone book”;

40 If-Then-Else for a mail order
Assign value .25 to discountRate and assign value to shipCost if purchase is over Otherwise, assign value .15 to discountRate and assign value 5.00 to shipCost Either way, calculate totalBill

41 These braces cannot be omitted
if ( purchase > ) { discountRate = .25 ; shipCost = ; } else discountRate = .15 ; shipCost = 5.00 ; totalBill = purchase * (1.0 - discountRate) + shipCost ;


Download ppt "Conditionals & Boolean Expressions"

Similar presentations


Ads by Google