CSO Gaddis Java Chapter03 7e
CSO Gaddis Java Chapter03 7e
7th Edition
Chapter 3
Decision Structures
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Chapter Topics (1 of 2)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Chapter Topics (2 of 2)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The if Statement
• The if statement decides whether a section of
code executes or not.
• The if statement uses a boolean to decide
whether the next statement or block of
statements executes.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Flowcharts (1 of 2)
Yes
Is it cold
outside?
if (coldOutside)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Flowcharts (2 of 2)
• A block if statement may be modeled as:
if (coldOutside)
{ Is it cold
Yes
wearCoat(); outside?
wearGloves();
Wear a hat.
}
Note the use of curly Wear gloves.
braces to block several
statements together.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Relational Operators
• In most cases, the boolean expression, used
by the if statement, uses relational operators.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Boolean Expressions
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if Statements and Boolean Expressions
if (x > y)
System.out.println("X is greater than Y");
if(x == y)
System.out.println("X is equal to Y");
if(x != y)
{
System.out.println("X is not equal to Y");
x = y;
System.out.println("However, now it is.");
}
Example: AverageScore.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Programming Style and if Statements
(1 of 2)
• An if statement can span more than one line;
however, it is still one statement.
grade = ′A′;
is functionally equivalent to
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Programming Style and if Statements
(2 of 2)
• Rules of thumb:
– The conditionally executed statement should be on
the line after the if condition.
– The conditionally executed statement should be
indented one level from the if condition.
– If an if statement does not have the block curly
braces, it is ended by the first semicolon
encountered after the if condition.
if (expression) No semicolon here.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Block if Statements (1 of 2)
• Conditionally executed statements can be
grouped into a block by using curly braces {}
to enclose them.
• If curly braces are used to group conditionally
executed statements, the if statement is
ended by the closing curly brace.
if (expression)
{
statement1;
statement2;
} Curly brace ends the statement.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Block if Statements (2 of 2)
• Remember that when the curly braces are not
used, then only the next statement after the if
condition will be executed conditionally.
if (expression)
statement1; Only this statement is conditionally executed.
statement2;
statement3;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Flags
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Comparing Characters
• Characters can be tested with relational operators.
• Characters are stored in memory using the Unicode
character format.
• Unicode is stored as a sixteen (16) bit number.
• Characters are ordinal, meaning they have an order in
the Unicode character set.
• Since characters are ordinal, they can be compared to
each other.
char c = ′A′;
if(c < ′Z′)
System.out.println("A is less than Z");
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if-else Statements
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if-else Statement Flowcharts
No
Yes
Is it cold
outside?
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Nested if Statements (1 of 2)
• If an if statement appears inside another if
statement (single or block) it is called a nested if
statement.
• The nested if is executed only if the outer if
statement results in a true condition.
• See example: LoanQualifier.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Nested if Statement Flowcharts
No
Yes
Is it cold
outside?
Wear shorts.
Is it Yes
No
snowing?
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Nested if Statements (2 of 2)
if (coldOutside)
if (snowing)
wearParka();
else
wearJacket();
}
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if-else Matching
• Curly brace use is not required if there is only
one statement to be conditionally executed.
• However, sometimes curly braces can help
make the program more readable.
• Additionally, proper indentation makes it much
easier to match up else statements with their
corresponding if statement.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Alignment and Nested if Statements
if (coldOutside)
else
wearJacket();
}
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if-else-if Statements (1 of 2)
if (expression_1)
{ If expression_1 is true these statements are executed, and the rest of the structure is
statement; ignored.
statement;
etc.
Otherwise, if expression_2 is true these statements are executed, and the rest of the structure is
}
ignored.
else if (expression_2)
statement;
statement;
etc.
These statements are executed if none of the
}
if-else-if Statements (2 of 2)
• Nested if statements can become very
complex.
• The if-else-if statement makes certain
types of nested decision logic simpler to write.
• Care must be used since else statements
match up with the immediately preceding
unmatched if statement.
• See example: TestResults.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
if-else-if Flowchart
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Logical Operators (1 of 2)
• Java provides two binary logical operators (&&
and ||) that are used to combine boolean
expressions.
• Java also provides one unary (!) logical
operator to reverse the truth of a boolean
expression.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Logical Operators (2 of 2)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The && Operator
• The logical AND operator (&&) takes two operands that
must both be boolean expressions.
• The resulting combined expression is true if (and only
if) both operands are true.
• See example: LogicalAnd.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The || Operator
• The logical OR operator (||) takes two operands that
must both be boolean expressions.
• The resulting combined expression is false if (and only
if) both operands are false.
• Example: LogicalOr.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The ! Operator
• The ! operator performs a logical NOT operation.
• If an expression is true, !expression will be false.
Expression 1 !Expression1
true false
false true
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Short Circuiting
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Order of Precedence (1 of 2)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Order of Precedence (2 of 2)
Order of
Operators Description
Precedence
1 (unary negation) ! Unary negation, logical NOT
2 * / % Multiplication, Division, Modulus
3 + - Addition, Subtraction
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Comparing String Objects
• In most cases, you cannot use the relational
operators to compare two String objects.
• Reference variables contain the address of the
object they represent.
• Unless the references point to the same object,
the relational operators will not return true.
• See example: StringCompare.java
• See example: StringCompareTo.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Ignoring Case in String Comparisons
• In the String class the equals and
compareTo methods are case sensitive.
• In order to compare two String objects that
might have different case, use:
– equalsIgnoreCase, or
– compareToIgnoreCase
• See example: SecretWord.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Variable Scope
• In Java, a local variable does not have to be
declared at the beginning of the method.
• The scope of a local variable begins at the point
it is declared and terminates at the end of the
method.
• When a program enters a section of code
where a variable has scope, that variable has
come into scope, which means the variable is
visible to the program.
• See example: VariableScope.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The Conditional Operator (1 of 4)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The Conditional Operator (2 of 4)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The Conditional Operator (3 of 4)
• Example:
z = x > y ? 10 : 5;
• This line is functionally equivalent to:
if(x > y)
z = 10;
else
z = 5;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The Conditional Operator (4 of 4)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The switch Statement (1 of 4)
• The if-else statement allows you to make
true / false branches.
• The switch statement allows you to use an
ordinal value to determine how a program will
branch.
• The switch statement can evaluate an integer
type or character type variable and make
decisions based on the value.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The switch Statement (2 of 4)
• The switch statement takes the form:
switch (SwitchExpression)
{
case CaseExpression:
// place one or more statements here
break;
case CaseExpression:
// place one or more statements here
break;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The switch Statement (4 of 4)
• Each case statement will have a
corresponding CaseExpression that must be
unique.
case CaseExpression:
// place one or more statements here
break;
• If the SwitchExpression matches the
CaseExpression, the Java statements between
the colon and the break statement will be
executed.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The case Statement
• The break statement ends the case statement.
• The break statement is optional.
• If a case does not contain a break, then program
execution continues into the next case.
– See example: NoBreaks.java
– See example: PetFood.java
• The default section is optional and will be executed if
no CaseExpression matches the SwitchExpression.
• See example: SwitchDemo.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (1 of 15)
• You can use the System.out.printf
method to perform formatted console output.
• The general format of the method is:
System.out.printf(FormatString,
ArgList);
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (2 of 15)
System.out.printf(FormatString, ArgList);
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (3 of 15)
• A simple example:
System.out.printf("Hello World\n");
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (4 of 15)
• Another example:
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (5 of 15)
The %d format specifier indicates that a decimal integer The contents of the hours variable will be printed in the
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (6 of 15)
• Another example:
int dogs = 2, cats = 4;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (7 of 15)
• Another example:
double grossPay = 874.12;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (8 of 15)
• Another example:
The %f format specifier indicates that a floating-point The contents of the grossPay variable will be printed in
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (9 of 15)
• Another example:
double grossPay = 874.12;
grossPay);
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method
(10 of 15)
• Another example:
double grossPay = 874.12;
The %.2f format specifier indicates that a floating-point value will be printed,
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method
(11 of 15)
• Another example:
double grossPay = 5874.127;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method
(12 of 15)
• Another example:
String name = "Ringo";
printed.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method
(13 of 15)
• Specifying a field width:
int number = 9;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method
(14 of 15)
• Another example:
double number = 9.76891;
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The System.out.printf Method (15 of 15)
• See examples:
– Columns.java
– CurrencyFormat.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The String.format Method (1 of 3)
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The String.format Method (2 of 3)
• The general format of the method is:
String.format(FormatString,ArgumentList);
format string.
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
The String.format Method (3 of 3)
• See examples:
– CurrencyFormat2.java
– CurrencyFormat3.java
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved
Copyright
This work is protected by United States copyright laws and is provided solely
for the use of instructions in teaching their courses and assessing student
World Wide Web) will destroy the integrity of the work and is not permit-
ted. The work and materials from it should never be made available to
restrictions and to honor the intended pedagogical purposes and the needs of
Copyright © 2019, 2016, 2013 Pearson Education, Inc. All Rights Reserved