31
Making Decisions Chapter 5

Making Decisions Chapter 5. Thus far we have created classes and performed basic mathematical operations Consider our ComputeArea.java program to

Embed Size (px)

Citation preview

Page 1: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Making Decisions

Chapter 5

Page 2: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Thus far we have created classes and performed basic mathematical operations

Consider our ComputeArea.java program to calculate the area of a circle given a specific radius: Area = π r2

If the radius is negative, we don't want the program to compute the area.

How can you deal with this situation?

Page 3: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Often in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false.

boolean b = (1 > 2);

Page 4: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Operator Name

< less than

<= less than or equal to

> greater than

>= greater than or equal to

== equal to

!= not equal to

Note that Java uses two equal signs (==) to perform equality testing:

A single equal sign (=) is used only for assignment

Page 5: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Specify a condition If condition is true one or more statements

execute If condition is false these statements skipped

Syntax

if (condition)statement statement executes

only if condition is true

Page 6: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if statement Simplest statement to make decision Boolean expression appears within parentheses Space between keyword if and opening

parentheses Execution always continues to next independent

statement Use double equal sign (==) to determine

equivalency

Page 7: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if (radius >= 0)

area = radius * radius * Math.PI;

Page 8: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

When if statement requires multiple statements to execute

Enclose those multiple statements within braces { } after if (condition)

Page 9: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if (radius >= 0) {

area = radius * radius * Math.PI;

System.out.println("The area"

+ " for the circle of radius "

+ radius + " is " + area);

}

Page 10: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

When you wish to do one thing if the condition is true But you want to do something else when the

condition is false Syntax

if (condition)statement 1

elsestatement 2

Statement 1 executes only if condition is true

Statement 2 executes only if condition is false

Page 11: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Single-alternative if Only perform action, or not

Based on one alternative Dual-alternative if

Two possible courses of action if...else statement

Performs one action when Boolean expression evaluates true

Performs different action when Boolean expression evaluates false

Page 12: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Boolean Expression

false true

Statement(s) for the false case Statement(s) for the true case

Page 13: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if (radius >= 0) { area = radius * radius * Math.PI;

System.out.println("The area for the " + "circle of radius " + radius + " is " + area);

}else {

System.out.println("Negative input");}

Page 14: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Nested if statements Statements in which if structure is contained

inside of another if structure Use when two conditions must be met before some

action is taken Pay careful attention to placement of else

clauses else statements

Always associated with if on “first in-last out” basis

Page 15: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Grade Distribution Checks for grade “A” first If it is not an “A”, then

checks for “B” If it is not a “B”, then

checks for “C” If it is not a “C”, then

checks for “D” Otherwise it is an “F”

if (score >= 90.0) grade = 'A';else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = ‘F';

Page 16: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if (score >= 90.0) grade = 'A';else if (score >= 80.0) grade = 'B';

else if (score >= 70.0) grade = 'C';else if (score >= 60.0) grade = 'D';

else

grade = ‘F';

if (score >= 90.0) grade = 'A';else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = ‘F';

Page 17: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Suppose score is 72.5

The condition is falseif (score >= 90.0) grade = 'A';else if (score >= 80.0) grade = 'B';

else if (score >= 70.0) grade = 'C';else if (score >= 60.0) grade = 'D';

else

grade = ‘F'; Exit the if statement

The condition is false

The condition is true

Grade is C

Page 18: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Adding a semicolon at the end of an if clause is a common mistake.

if (radius >= 0) ;

This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error

Don’t do itThe semicolon will

prematurely end the decision

Page 19: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Write a program to see if a user input can correctly guess a number from 1 to 10

if (guess == number)System.out.println("You win");

elseSystem.out.println("You lose");

Page 20: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

import java.util.Scanner;

class GuessingGame {public static void main(String[ ] args) {

System.out.print("Enter an integer from 1 to 10: ");Scanner input = new Scanner(System.in);int guess = input.nextInt();

int number = ((int)(Math.random() * 10) % 10 +1);

if (guess == number) System.out.println("* You Win! *");

elseSystem.out.println("* You lose *");

System.out.println("The number was " + number);}

}

Page 21: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Logical AND operator Alternative to some nested if statements Used between two Boolean expressions to

determine whether both are true Written as two ampersands (&&)

Include complete Boolean expression on each side Both Boolean expressions that surround

operator Must be true before action in statement can occur

Page 22: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Logical OR operator Action to occur when at least one of two

conditions is true Written as ||

Sometimes called pipes

Page 23: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to
Page 24: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Suppose we would like to determine if it is a leap year

Leap year follows the logic: Year is divisible by 4 but not by 100 Or it is divisible by 400

(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

Page 25: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

if (score >= 90.0) grade = 'A';if (score >= 80.0 && score < 90.0) grade = 'B';

if (score >= 70.0 && score < 80.0) grade = 'C';if (score >= 60.0 && score < 70.0) grade = 'D';

if (score < 60.0)

grade = ‘F';

if (score >= 90.0) grade = 'A';else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = ‘F';

Page 26: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

The switch statement is the only other kind of Java statement that implements multiway branching When a switch statement is evaluated, one of a

number of different branches is executed The choice of which branch to execute is determined by

a controlling expression enclosed in parentheses after the keyword switch

The controlling expression must evaluate to a char, int, short, or byte

Page 27: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

switch (Controlling_Expression){ case (Boolean Expression 1): Statement 1 break; case (Boolean Expression 2): Statement 2 break; case (Boolean Expression n): Statement n break; default: Default Statement break;}

. . .

The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed.

The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.

Page 28: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

int num = 1;

switch (num) { case 1: System.out.println("You picked checking account"); break; case 2: System.out.println("You picked savings account"); break; default: System.out.println("That was an invalid entry."); };

Page 29: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Why use switch statements? Convenient when several alternative courses

of action depend on single integer or character variable

Use only when there are reasonable number of specific matching values to be tested

Page 30: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to

Combine as many AND or OR operators as needed

Operator’s precedence How expression is evaluated Order agrees with common algebraic usage

Arithmetic done first Assignment done last AND operator evaluated before OR operator Statements in parentheses evaluated first

Page 31: Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to