0% found this document useful (0 votes)
17 views35 pages

Chapter 5

Chapter 5 focuses on decision-making in Java programming, covering the use of if, if...else, and switch statements, as well as logical operators like AND and OR. It emphasizes the importance of accurate and efficient decision-making, including pitfalls to avoid, such as misplacing semicolons and using incorrect operators. The chapter also discusses operator precedence and the use of conditional operators for concise expressions.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views35 pages

Chapter 5

Chapter 5 focuses on decision-making in Java programming, covering the use of if, if...else, and switch statements, as well as logical operators like AND and OR. It emphasizes the importance of accurate and efficient decision-making, including pitfalls to avoid, such as misplacing semicolons and using incorrect operators. The chapter also discusses operator precedence and the use of conditional operators for concise expressions.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 5: Making

Decisions
Objectives
• Plan decision-making logic
• Make decisions with the if and if…else
statements
• Use multiple statements in if and if…else
clauses
• Nest if and if…else statements
• Use AND and OR operators

Java Programming, Eighth Edition 2


Objectives (cont’d.)
• Make accurate and efficient decisions
• Use the switch statement
• Use the conditional and NOT operators
• Assess operator precedence
• Add decisions and constructors to instance methods

Java Programming, Eighth Edition 3


The if and if…else Statements
• if statement
– The simplest statement to make a decision
– A Boolean expression appears within parentheses
– No space between the keyword if and the opening
parenthesis
– Execution always continues to the next independent
statement
– Use a double equal sign ( == ) to determine equivalency

Java Programming, Eighth Edition 4


The if and if…else Statements
(cont’d.)

Java Programming, Eighth Edition 5


Pitfall: Misplacing a Semicolon in
an if Statement
• There should be no semicolon at the end of the first
line of the if statement
– if(someVariable == 10)
– The statement does not end there
• When a semicolon follows if directly:
– An empty statement contains only a semicolon
– Execution continues with the next independent statement

Java Programming, Eighth Edition 6


Pitfall: Misplacing a Semicolon in
an if Statement (cont’d.)

Java Programming, Eighth Edition 7


Pitfall: Using the Assignment Operator
Instead of the Equivalency Operator
• Attempt to determine equivalency
– Using a single equal sign rather than a double equal sign is
illegal
• You can store a Boolean expression’s value in a
Boolean variable before using it in an if statement

Java Programming, Eighth Edition 8


Pitfall: Attempting to Compare Objects
Using the Relational Operators
• Use standard relational operators to compare values
of primitive data types
– Not objects
• You can use the equals and not equals comparisons
( == and != ) with objects
– Compare objects’ memory addresses instead of values

Java Programming, Eighth Edition 9


The if…else Statement
• Single-alternative if
– Perform an action, or not
• Based on one alternative
• Dual-alternative if
– Two possible courses of action
• if…else statement
– Performs one action when a Boolean expression evaluates
as true
– Performs a different action when a Boolean expression
evaluates as false

Java Programming, Eighth Edition 10


The if…else Statement (cont’d.)
• if…else statement (cont’d.)
– A statement that executes when if is true or false
and ends with a semicolon
– Vertically align the keyword if with the keyword else
– Illegal to code else without if
– Depending on the evaluation of the Boolean expression
following if, only one resulting action takes place

Java Programming, Eighth Edition 11


The if…else Statement (cont’d.)

Java Programming, Eighth Edition 12


Using Multiple Statements
in if and if…else Clauses
• To execute more than one statement, use a pair of
curly braces
– Place dependent statements within a block
– Crucial to place the curly braces correctly
• Any variable declared within a block is local to that
block

Java Programming, Eighth Edition 13


Using Multiple Statements in if and
if…else Clauses (cont’d.)

Java Programming, Eighth Edition 14


Nesting if and if…else
Statements
• Nested if statements
– Statements in which an if structure is contained inside
another if structure
– Two conditions must be met before some action is taken
• Pay careful attention to the placement of else
clauses
• else statements are always associated with if on a
“first in-last out” basis

Java Programming, Eighth Edition 15


Nesting if and if…else
Statements (cont’d.)

Java Programming, Eighth Edition 16


Using Logical AND and OR
Operators
• The logical AND operator
– An alternative to some nested if statements
– Used between two Boolean expressions to determine
whether both are true
– Written as two ampersands ( && )
• Include a complete Boolean expression on each side
– Both Boolean expressions that surround the operator must
be true before the action in the statement can occur

Java Programming, Eighth Edition 17


Using Logical AND and OR
Operators (cont’d.)

Java Programming, Eighth Edition 18


Using Logical AND and OR
Operators (cont’d.)
• The OR operator
– An action to occur when at least one of two conditions is
true
– Written as ||
• Sometimes called pipes

Java Programming, Eighth Edition 19


Using Logical AND and OR
Operators (cont’d.)

Java Programming, Eighth Edition 20


Making Accurate and Efficient
Decisions
• Making accurate range checks
– Range check: a series of if statements that determine
whether a value falls within a specified range
– Java programmers commonly place each else of a
subsequent if on the same line
– Within a nested if…else statement:
• It is most efficient to ask the most likely question first
• Avoid asking multiple questions

Java Programming, Eighth Edition 21


Making Accurate and Efficient
Decisions (cont’d.)

Java Programming, Eighth Edition 22


Making Accurate and Efficient
Decisions (cont’d.)
• It is most efficient to ask a question most likely to be
true first
– Avoids asking multiple questions
– Makes a sequence of decisions more efficient

Java Programming, Eighth Edition 23


Making Accurate and Efficient
Decisions (cont’d.)

Java Programming, Eighth Edition 24


Using && and || Appropriately
• Errors of beginning programmers:
– Using the AND operator when they mean to use OR
• Example: No payRate value can ever be both less than 5.65 and
more than 60 at the same time
if(payRate < LOW && payRate > HIGH)
System.out.println("Error in pay rate");
• Use pipes “||” operator instead
– Using a single ampersand or pipe to indicate a logical AND
or OR

Java Programming, Eighth Edition 25


Using the switch Statement
• switch statement
– An alternative to a series of nested if statements
– Test a single variable against a series of exact integer,
character, or string values

Java Programming, Eighth Edition 26


Using the switch Statement
(cont’d.)
• Keywords
– switch
• Starts the structure
• Followed by a test expression enclosed in parentheses
– case
• Followed by one of the possible values for the test expression and
a colon

Java Programming, Eighth Edition 27


Using the switch Statement
(cont’d.)
• Keywords (cont’d.)
– break
• Optionally terminates a switch statement at the end of each
case
– default
• Optionally is used prior to any action that should occur if the test
variable does not match any case

Java Programming, Eighth Edition 28


Using the switch Statement
(cont’d.)

Java Programming, Eighth Edition 29


Using the switch Statement
(cont’d.)
• break statements in the switch structure
– If a break statement is omitted:
• The program finds a match for the test variable
• All statements within the switch statement execute from that
point forward
• case statement
– No need to write code for each case
– Evaluate char variables
• Ignore whether it is uppercase or lowercase

Java Programming, Eighth Edition 30


Using the switch Statement
(cont’d.)
• Why use switch statements?
– They are convenient when several alternative courses of
action depend on a single integer, character, or string
value
– Use only when there is a reasonable number of specific
matching values to be tested

Java Programming, Eighth Edition 31


Using the Conditional
and NOT Operators (cont’d.)
• A Boolean expression is evaluated as true or
false
– If the value of testExpression is true:
• The entire conditional expression takes on the value of the
expression following the question mark
– If the value is false:
• The entire expression takes on the value of falseResult
• An advantage of using the conditional operator is the
conciseness of the statement

Java Programming, Eighth Edition 32


Understanding Operator
Precedence (cont’d.)

Java Programming, Eighth Edition 33


Understanding Operator
Precedence (cont’d.)
• Two important conventions
– The order in which operators are used makes a difference
– Always use parentheses to change precedence or make
your intentions clearer

Java Programming, Eighth Edition 34


Understanding Operator
Precedence (cont’d.)

Java Programming, Eighth Edition 35

You might also like