# Chapter 5 (Decisions) - Dalton State College

```Making Decisions
Chapter 5


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
Area = π r2
 If the radius is negative, we don't want the
program to compute the area.


How can you deal with this situation?

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);
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

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

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



When if statement requires multiple
statements to execute
Enclose those multiple statements within
braces { } after if (condition)
System.out.println("The area"
+ " for the circle of radius "
+ radius + " is " + area);
}

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
else
statement 2
Statement 1 executes
only if condition is true
Statement 2 executes
only if condition is false

Single-alternative if

Only perform action, or not


Dual-alternative if


Based on one alternative
Two possible courses of action
if...else statement


Performs one action when Boolean expression
evaluates true
Performs different action when Boolean expression
evaluates false
true
Statement(s) for the true case
Boolean
Expression
false
Statement(s) for the false case
System.out.println("The area for the "
" is " + area);
}
else {
System.out.println("Negative input");
}

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

 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)
else
if (score >= 80.0)
else
if (score >= 70.0)
else
if (score >= 60.0)
else
if (score >= 90.0)
else
if (score >= 80.0)
else
if (score >= 70.0)
else
if (score >= 60.0)
else
if (score >= 90.0)
else if (score >= 80.0)
else if (score >= 70.0)
else if (score >= 60.0)
else
Suppose score is 72.5
if (score >= 90.0)
else if (score >= 80.0)
else if (score >= 70.0)
else if (score >= 60.0)
else
The condition is false
The condition is false
The condition is true
Exit the if statement

Adding a semicolon at the end of an if
clause is a common mistake.
Don’t do it
The semicolon will
prematurely end the
decision

This mistake is hard to find, because it is
not a compilation error or a runtime error,
it is a logic error

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");
else
System.out.println("You lose");
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! *");
else
System.out.println("* You lose *");
System.out.println("The number was " + number);
}
}

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

Logical OR operator
Action to occur when at least one of two
conditions is true
 Written as ||


Sometimes called pipes


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);
if (score >= 90.0)
else
if (score >= 80.0)
else
if (score >= 70.0)
else
if (score >= 60.0)
else
if (score >= 90.0)
if (score >= 80.0 && score < 90.0)
if (score >= 70.0 && score < 80.0)
if (score >= 60.0 && score < 70.0)
if (score < 60.0)

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
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.
...
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 default case, which is
optional, can be used to
perform actions when none
of the specified cases
matches the switchexpression.
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.");
};

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



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

```

– Cards

– Cards

– Cards

– Cards

– Cards