Download presentation
Presentation is loading. Please wait.
1
Java Programming Fifth Edition
Chapter 5 Making Decisions
2
Java Programming, Fifth Edition
3
Java Programming, Fifth Edition
4
Making Decisions with the if and if...else Structures
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 Java Programming, Fifth Edition
5
Making Decisions with the if and if...else Structures (continued)
Java Programming, Fifth Edition
6
Pitfall: Misplacing a Semicolon in an if Statement
No semicolon at end of first line of if statement if (someVariable == 10) Statement does not end there When semicolon follows if directly Empty statement contains only semicolon Execution continues with the next independent statement Java Programming, Fifth Edition
7
Pitfall: Misplacing a Semicolon in an if Statement (continued)
Java Programming, Fifth Edition
8
Pitfall: Using the Assignment Operator Instead of the Equivalency Operator
Attempt to determine equivalency Using single equal sign Rather than double equal sign Illegal Can store Boolean expression’s value in Boolean variable Before using in if statement Java Programming, Fifth Edition
9
Pitfall: Attempting to Compare Objects Using the Relational Operators
Use standard relational operators to compare values of primitive data types Not objects Can use equals and not equals comparisons (== and !=) with objects To compare objects’ memory addresses instead of values Java Programming, Fifth Edition
10
The if...else Structure Single-alternative if Dual-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 Java Programming, Fifth Edition
11
The if...else Structure (continued)
if...else statement (continued) Statement that executes when if is true or false ends with semicolon Vertically align keyword if with the keyword else Illegal to code else without if Depending on evaluation of Boolean expression following if Only one resulting action takes place Java Programming, Fifth Edition
12
The if...else Structure (continued)
Java Programming, Fifth Edition
13
Using Multiple Statements in an if or if...else Structure
Execute more than one statement Use pair of curly braces Place dependent statements within block Crucial to place curly braces correctly Any variable declared within block local to that block Java Programming, Fifth Edition
14
Java Programming, Fifth Edition
15
Nesting if and if...else Statements
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 Java Programming, Fifth Edition
16
Nesting if and if...else Statements (continued)
Java Programming, Fifth Edition
17
Using Logical AND and OR Operators
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 Java Programming, Fifth Edition
18
Using Logical AND and OR Operators (continued)
Java Programming, Fifth Edition
19
Using Logical AND and OR Operators (continued)
Logical OR operator Action to occur when at least one of two conditions is true Written as || Sometimes called pipes Java Programming, Fifth Edition
20
Using Logical AND and OR Operators (continued)
Java Programming, Fifth Edition
21
Making Accurate and Efficient Decisions
Range check Series of if statements that determine whether: Value falls within specified range Java programmers commonly place each else of subsequent if on same line Within nested if...else Most efficient to ask most likely question first Avoid asking multiple questions Java Programming, Fifth Edition
22
Making Accurate and Efficient Decisions (continued)
Java Programming, Fifth Edition
23
Using AND and OR Appropriately
Errors of beginning programmers Using AND operator when they mean to use OR Example: no payRate value can ever be both below 5.65 and over 60 at same time if(payRate < 5.65 && payRate > 60) System.out.println("Error in pay rate"); Use pipes “||” operator instead Using single ampersand or pipe To indicate logical AND or OR Java Programming, Fifth Edition
24
Using the switch Statement
Alternative to series of nested if statements Test single variable against series of exact integer or character values Java Programming, Fifth Edition
25
Using the switch Statement (continued)
Switch structure keywords switch Starts structure Followed by test expression Enclosed in parentheses case Followed by one of the possible values for test expression and colon Java Programming, Fifth Edition
26
Using the switch Statement (continued)
Switch structure keywords (continued) break Optionally terminates switch structure at end of each case default Used prior to any action if test variable does not match any case Optional Java Programming, Fifth Edition
27
Using the switch Statement (continued)
Java Programming, Fifth Edition
28
Using the switch Statement (continued)
break statements in switch structure If break statement omitted Program finds match for test variable All statements within switch statement execute from that point forward case statement No need to write code for each case Evaluate char variables Ignore whether uppercase or lowercase Java Programming, Fifth Edition
29
Using the switch Statement (continued)
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 Java Programming, Fifth Edition
30
Using the NOT Operator NOT operator Statements with NOT operator
Written as exclamation point (!) Negates result of any Boolean expression When preceded by NOT operator Any expression evaluates as: true becomes false false becomes true Statements with NOT operator Harder to read Require double set of parentheses Java Programming, Fifth Edition
31
Understanding Precedence
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 Java Programming, Fifth Edition
32
Understanding Precedence (continued)
Java Programming, Fifth Edition
33
Understanding Precedence (continued)
Two important conventions Order in which operators are used Always use parentheses Change precedence Or make your intentions clearer Java Programming, Fifth Edition
34
Understanding Precedence (continued)
Java Programming, Fifth Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.