Download presentation
Presentation is loading. Please wait.
Published byDulcie Owen Modified over 9 years ago
1
Making Decisions Chapter 5
2
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 = π r 2 If the radius is negative, we don't want the program to compute the area. How can you deal with this situation?
3
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);
4
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
5
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
6
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
7
if (radius >= 0) area = radius * radius * Math.PI;
8
When if statement requires multiple statements to execute Enclose those multiple statements within braces { } after if (condition)
9
if (radius >= 0) { area = radius * radius * Math.PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); }
10
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) statement1 else statement2 Statement 1 executes only if condition is true Statement 2 executes only if condition is false
11
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
13
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"); }
14
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
15
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';
16
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';
17
Suppose score is 72.5 The condition is false 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'; Exit the if statement The condition is false The condition is true Grade is C
18
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 it The semicolon will prematurely end the decision
19
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");
20
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); }
21
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
22
Logical OR operator Action to occur when at least one of two conditions is true Written as || Sometimes called pipes
24
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);
25
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';
26
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
27
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.
28
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."); };
29
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
30
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.