Selection Control Structures

advertisement
Chapter 4: Selection Control
Structures
ΠRelational and logical operators.
ΠLogical (Boolean) expressions.
ΠThe selection control structures if,
if…else, and switch.
Java Programming: From Problem Analysis to Program Design, Second Edition
1
Control Structures
ΠThree methods of processing a program:
ΠIn sequence
ΠBranching
ΠLooping
ΠBranch: Altering the flow of program
execution by making a selection or choice.
ΠLoop: Altering the flow of program
execution by repeating statements.
Java Programming: From Problem Analysis to Program Design, Second Edition
2
1
Control Structures
Java Programming: From Problem Analysis to Program Design, Second Edition
3
Relational Operators
ΠRelational operator:
ΠAllows you to make comparisons in a program.
ΠBinary operator.
ΠCondition is represented by a logical
expression in Java.
ΠLogical expression: An expression that has
a value of either true or false.
Java Programming: From Problem Analysis to Program Design, Second Edition
4
2
Relational Operators
Java Programming: From Problem Analysis to Program Design, Second Edition
5
Relational Operators and
Primitive Data Types
ΠCan be used with integral and floating-point
data types.
ΠCan be used with the char data type.
ΠUnicode Collating Sequence.
Java Programming: From Problem Analysis to Program Design, Second Edition
6
3
Relational Operators and Primitive Data Types
Java Programming: From Problem Analysis to Program Design, Second Edition
7
Comparing Strings
Πclass String
ΠMethod compareTo
ΠMethod equals
ΠGiven string str1 and str2
⎧an integer< 0 if string str1< str2
⎪
str1.compareTo(str2)= ⎨0 if string str1is equal to stringstr2
⎪an integer> 0 if string str1 > str2
⎩
Java Programming: From Problem Analysis to Program Design, Second Edition
8
4
Comparing Strings
String
String
String
String
String
str1
str2
str3
str4
str5
=
=
=
=
=
"Hello";
"Hi";
"Air";
"Bill";
"Bigger";
Java Programming: From Problem Analysis to Program Design, Second Edition
9
Comparing Strings
Java Programming: From Problem Analysis to Program Design, Second Edition
10
5
Comparing Strings
Java Programming: From Problem Analysis to Program Design, Second Edition
11
Comparing Strings
Java Programming: From Problem Analysis to Program Design, Second Edition
12
6
Comparing Strings
Java Programming: From Problem Analysis to Program Design, Second Edition
13
Short-Circuit Evaluation
ΠA process in which the computer evaluates
a logical expression from left to right and
stops as soon as the value of the expression
is known.
Java Programming: From Problem Analysis to Program Design, Second Edition
14
7
Selection
Œ
Œ
Œ
Œ
Œ
Œ
One-way selection
Two-way selection
Compound (block of) statements
Multiple selections (nested if)
Conditional operator
switch structures
Java Programming: From Problem Analysis to Program Design, Second Edition
15
One-Way Selection
ΠSyntax:
if (expression)
statement
ΠExpression referred to as decision maker.
ΠStatement referred to as action statement.
Java Programming: From Problem Analysis to Program Design, Second Edition
16
8
One-Way Selection
Example 4-11
//Determine the absolute value of an integer
import javax.swing.JOptionPane;
public class AbsoluteValue
{
public static void main(String[] args)
{
int number;
int temp;
String numString;
numString =
JOptionPane.showInputDialog
("Enter an integer:");
//Line 1
number = Integer.parseInt(numString); //Line 2
temp = number;
//Line 3
Java Programming: From Problem Analysis to Program Design, Second Edition
17
One-Way Selection
if (number < 0)
number = -number;
//Line 4
//Line 5
JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE);
//Line 6
System.exit(0);
}
Java Programming: From Problem Analysis to Program Design, Second Edition
18
9
Two-Way Selection
ΠSyntax:
if (expression)
statement1
else
statement2
Πelse statement must be paired with an if.
Java Programming: From Problem Analysis to Program Design, Second Edition
19
Two-Way Selection
Java Programming: From Problem Analysis to Program Design, Second Edition
20
10
Two-Way Selection
Example 4-14
if (hours > 40.0)
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;
Java Programming: From Problem Analysis to Program Design, Second Edition
21
Two-Way Selection
Example 4-15
if (hours > 40.0);
//Line 1
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0); //Line 2
else
//Line 3
wages = hours * rate;
//Line 4
Because a semicolon follows the closing parenthesis of the if
statement (Line 1), the else statement stands alone. The
semicolon at the end of the if statement (see Line 1) ends the
if statement, so the statement at Line 2 separates the else
clause from the if statement. That is, else is by itself.
Because there is no separate else statement in Java, this code
generates a syntax error.
Java Programming: From Problem Analysis to Program Design, Second Edition
22
11
Compound (Block of) Statements
Syntax:
{
statement1
statement2
.
.
.
statementn
}
Java Programming: From Problem Analysis to Program Design, Second Edition
23
Compound (Block of) Statements
if (age > 18)
{
System.out.println("Eligible to vote.");
System.out.println("No longer a minor.");
}
else
{
System.out.println("Not eligible to vote.");
System.out.println("Still a minor.");
}
Java Programming: From Problem Analysis to Program Design, Second Edition
24
12
Conditional (? :) Operator
ΠTernary operator
ΠSyntax:
expression1 ?
expression3
expression2
:
ΠIf expression1 = true, then the result of the
condition is expression2.
Otherwise, the result of the condition is
expression3.
Java Programming: From Problem Analysis to Program Design, Second Edition
25
Multiple Selection: Nested if
ΠSyntax:
ΠElse is associated with the
most recent incomplete if.
ΠMultiple if statements can
if (expression1)
be used in place of
statement1
if…else statements.
else
ΠMay take longer to
if (expression2)
evaluate.
statement2
else
statement3
Java Programming: From Problem Analysis to Program Design, Second Edition
26
13
switch Structures
switch (expression)
{
case value1: statements1
break;
case value2: statements2
break;
...
case valuen: statementsn
break;
default: statements
}
ΠExpression is also
known as selector.
ΠExpression can be an
identifier.
ΠValue can only be
integral.
Java Programming: From Problem Analysis to Program Design, Second Edition
27
switch Structures
Java Programming: From Problem Analysis to Program Design, Second Edition
28
14
switch Structures
Example 4-24
switch (grade)
{
case 'A': System.out.println("The
break;
case 'B': System.out.println("The
break;
case 'C': System.out.println("The
break;
case 'D': System.out.println("The
break;
case 'F': System.out.println("The
break;
default: System.out.println("The
invalid.");
}
grade is A.");
grade is B.");
grade is C.");
grade is D.");
grade is F.");
grade is
Java Programming: From Problem Analysis to Program Design, Second Edition
29
Programming Example:
Cable Company Billing
Œ Input: Customer’s account number,
customer code, number of premium
channels to which customer subscribes,
number of basic service connections (in the
case of business customers).
Œ Output: Customer’s account number and the
billing amount.
Java Programming: From Problem Analysis to Program Design, Second Edition
30
15
Programming Example:
Cable Company Billing
Solution:
1. Prompt user for information.
2. Use switch statements based on customer’s
type.
3. Use an if statement nested within a switch
statement to determine the amount due by
each customer.
Java Programming: From Problem Analysis to Program Design, Second Edition
31
Chapter Summary
ΠControl structures are used to process programs.
ΠLogical expressions and order of precedence of
operators are used in expressions.
ΠCompare strings.
ΠIf statements.
Œ if…else statements.
Πswitch structures.
ΠProper syntax for using control statements.
Java Programming: From Problem Analysis to Program Design, Second Edition
32
16
Download