lesson 39: conditionals #3 (w11d4)lesson 39: conditionals #3 (w11d4) balboa high school michael...

29
Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1 / 29

Upload: others

Post on 06-Oct-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Lesson 39: Conditionals #3 (W11D4)Balboa High School

Michael Ferraro

October 29, 2015

1 / 29

Page 2: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Do Now

In order to qualify for a $50k loan, the following conditions must be met:

Your annual income must be at least $26,000

Your age must be between 18 and 29 (inclusive)

Write a boolean that would serve as an appropriate condition for theif() statement below.

int income, age; //assume these values are set...

Loan myLoan = new Loan(50000);

if( _________________ ) {

myLoan.grant();

} else {

myLoan.deny();

}

2 / 29

Page 3: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Aim

Students will learn about Short-Circuit Evaluation, if()/else if, andwork on programming exercises.

3 / 29

Page 4: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation

Consider this scenario:

boolean a = false, b = true, c = true;

if ( a && ( b || c ) ) {

// statements to run if true...

}

How does Java evaluate the condition?

4 / 29

Page 5: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation

Consider this scenario:

boolean a = false, b = true, c = true;

if ( a && ( b || c ) ) {

// statements to run if true...

}

How does Java evaluate the condition?

→ In an and statement, if the left side is false, there’s no way the overalltruth value could possibly be true, so why bother to spend CPU cycles toevaluate the rest?

5 / 29

Page 6: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation

Consider this scenario:

boolean a = false, b = true, c = true;

if ( a && ( b || c ) ) {

// statements to run if true...

}

How does Java evaluate the condition?

→ In an and statement, if the left side is false, there’s no way the overalltruth value could possibly be true, so why bother to spend CPU cycles toevaluate the rest?

This shortcut is called Short-Circuit Evaluation.

6 / 29

Page 7: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for or

How might the principle of Short-Circuit Evaluation be applied to or?

7 / 29

Page 8: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for or

How might the principle of Short-Circuit Evaluation be applied to or?

Ex: You have to be at least 25 or have a military ID to rent a car.

8 / 29

Page 9: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for or

How might the principle of Short-Circuit Evaluation be applied to or?

Ex: You have to be at least 25 or have a military ID to rent a car.

int age = 19;

boolean inMilitary = true;

if ( inMilitary || age >= 25 ) {

...

}

9 / 29

Page 10: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for or

How might the principle of Short-Circuit Evaluation be applied to or?

Ex: You have to be at least 25 or have a military ID to rent a car.

int age = 19;

boolean inMilitary = true;

if ( inMilitary || age >= 25 ) {

...

}

→ In an or statement, if the left side is true, then there’s no way theentire or statement won’t be true — so don’t bother evaluating the rest.

10 / 29

Page 11: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for Safety!

Let’s say you have this condition to evaluate:

a/b > 6

11 / 29

Page 12: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for Safety!

Let’s say you have this condition to evaluate:

a/b > 6

What is the potential problem with evaluating this boolean expression?(Consider the values that the variables might have!)

12 / 29

Page 13: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for Safety!

Let’s say you have this condition to evaluate:

a/b > 6

What is the potential problem with evaluating this boolean expression?(Consider the values that the variables might have!)

→ b must not be zero or else there will be a “divide by zero” exception!

13 / 29

Page 14: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Short-Circuit Evaluation for Safety!

Let’s say you have this condition to evaluate:

a/b > 6

What is the potential problem with evaluating this boolean expression?(Consider the values that the variables might have!)

→ b must not be zero or else there will be a “divide by zero” exception!

Short-circuit evaluation prevents that case:

b != 0 && a/b > 6

14 / 29

Page 15: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

What if you need to branch in more than two directions based on acondition?

Example: The game of Blackjack

You are dealt two cards. Based on the sum of their values, here arepossible actions:

sum action21 (e.g., A♥ & K♣) Dealer pays 3:21

10 (e.g., 3♦ & 7♠) Double down<10 (e.g., 5♣ & 2♥) Hit

1$3 return/payout on $2 bet15 / 29

Page 16: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

What if you need to branch in more than two directions based on acondition?

Example: The game of Blackjack

You are dealt two cards. Based on the sum of their values, here arepossible actions:

sum action21 (e.g., A♥ & K♣) Dealer pays 3:21

10 (e.g., 3♦ & 7♠) Double down<10 (e.g., 5♣ & 2♥) Hit

1$3 return/payout on $2 bet16 / 29

Page 17: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

How one might implement the Blackjack logic from the last slide:2

int cardOneVal, cardTwoVal;

// call method to deal cards, set card values

int sum = cardOneVal + cardTwoVal;

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

}

2cases grossly oversimplified!17 / 29

Page 18: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

How one might implement the Blackjack logic from the last slide:

int cardOneVal, cardTwoVal;

// call method to deal cards, set card values

int sum = cardOneVal + cardTwoVal;

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

//2 more cases:

// * sum is 10,

// * sum is less

}

18 / 29

Page 19: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

How one might implement the Blackjack logic from the last slide:

int cardOneVal, cardTwoVal;

// call method to deal cards, set card values

int sum = cardOneVal + cardTwoVal;

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

}

// * sum is less

}19 / 29

Page 20: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

How one might implement the Blackjack logic from the last slide:

int cardOneVal, cardTwoVal;

// call method to deal cards, set card values

int sum = cardOneVal + cardTwoVal;

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else {

dealer.dealCard();

}

} 20 / 29

Page 21: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else {

dealer.dealCard();

}

}

What we have here are nested if() statements

21 / 29

Page 22: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else {

dealer.dealCard();

}

}

What we have here are nested if() statements

Such a construct is often hard to understand and debug

22 / 29

Page 23: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

if()-wrapped if()

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else {

if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else {

dealer.dealCard();

}

}

What we have here are nested if() statements

Such a construct is often hard to understand and debug

Better way: else if()

23 / 29

Page 24: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

else if()

Here’s the same logic using else if():

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

}

24 / 29

Page 25: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

else if()

Here’s the same logic using else if():

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

}

25 / 29

Page 26: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

else if()

Here’s the same logic using else if():

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else { //all other cases

dealer.dealCard();

}

26 / 29

Page 27: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

else if()

if ( sum == 21 ) {

dealer.payOut(3,2); //winner!

} else if ( sum == 10 ) {

player.doubleDown();

dealer.dealCard();

} else { //all other cases

dealer.dealCard();

}

Indentation makes the code more readable, easier to follow

You can have as many else if() statements as you need to coverall possible outcomes for which you need different behaviors

You’ll learn switch/case/break soon, which is essentially a specialform of what you see above

27 / 29

Page 28: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

Programming Exercises

You can find the PDF of Ch. 6 here.

Either alone or with a partner, write classes that include thesemethods, and place them in a new project called Lesson39.

Develop isLeapYear() as per Ch. 6, #13. Make sure you understandthe requirements for a leap year by reading the description carefully!

Develop findBestFit() as per Ch. 6, #19. A tester class isavailable here.

28 / 29

Page 29: Lesson 39: Conditionals #3 (W11D4)Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1/29

HW

Finish §3 of PS #6 by next class. Come with questions about bookproblems you’re unable to figure out!

29 / 29