Download Malik_4E_CH04

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Programming: From Problem
Analysis to Program Design, 4e
Chapter 4
Control Structures I: Selection
Chapter Objectives
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
(Boolean) expressions
• Learn how to use the selection control
structures if, if…else, and switch in
a program
Java Programming: From Problem Analysis to Program Design, 4e
2
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 repetition of statement(s)
Java Programming: From Problem Analysis to Program Design, 4e
3
Flow of Execution
Java Programming: From Problem Analysis to Program Design, 4e
4
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: expression that has a
value of either true or false
Java Programming: From Problem Analysis to Program Design, 4e
5
Relational Operators in Java
Java Programming: From Problem Analysis to Program Design, 4e
6
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, 4e
7
Java Programming: From Problem Analysis to Program Design, 4e
8
Java Programming: From Problem Analysis to Program Design, 4e
9
Relational Operators and the
Unicode Collating Sequence
Java Programming: From Problem Analysis to Program Design, 4e
10
Logical (Boolean) Operators
Java Programming: From Problem Analysis to Program Design, 4e
11
Logical (Boolean) Operators
(continued)
Java Programming: From Problem Analysis to Program Design, 4e
12
Logical (Boolean) Operators
(continued)
Java Programming: From Problem Analysis to Program Design, 4e
13
Logical (Boolean) Operators
(continued)
Java Programming: From Problem Analysis to Program Design, 4e
14
Precedence of Operators
Java Programming: From Problem Analysis to Program Design, 4e
15
Precedence of Operators
(continued)
Java Programming: From Problem Analysis to Program Design, 4e
16
Short-Circuit Evaluation
• Definition: 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, 4e
17
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, 4e
18
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, 4e
19
One-Way Selection (continued)
Java Programming: From Problem Analysis to Program Design, 4e
20
One-Way Selection (continued)
Example 4-10
//Program to 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, 4e
21
One-Way Selection (continued)
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, 4e
22
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, 4e
23
Two-Way Selection (continued)
Java Programming: From Problem Analysis to Program Design, 4e
24
Two-Way Selection (continued)
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, 4e
25
Two-Way Selection (continued)
Example 4-14 (continued)
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
• Since there is no separate else statement in Java, this code
generates a syntax error
Java Programming: From Problem Analysis to Program Design, 4e
26
Compound (Block of) Statements
• Syntax
Java Programming: From Problem Analysis to Program Design, 4e
27
Compound (Block of) Statements
(continued)
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, 4e
28
Multiple Selection: Nested if
• Syntax
if (expression1)
statement1
else if (expression2)
statement2
else
statement3
• Else associated with
most recent incomplete
if
• Multiple if statements
can be used in place of
if…else statements
• May take longer to
evaluate
Java Programming: From Problem Analysis to Program Design, 4e
29
Conditional (? :) Operator
• Ternary operator
• Syntax
expression1 ?
expression2
:
expression3
• If expression1 = true, then the result of the
condition is expression 2; otherwise, the
result of the condition is expression 3
Java Programming: From Problem Analysis to Program Design, 4e
30
switch Structures
Java Programming: From Problem Analysis to Program Design, 4e
31
switch Structures (continued)
• In Java, switch, case, break, and default are
reserved words
• In a switch structure, the expression is evaluated
first
• The value of the expression is then used to perform
the actions specified in the statements that follow the
reserved word case
• The expression is usually an identifier
• The value of the identifier or the expression can be
only of type int, byte, short, or char
Java Programming: From Problem Analysis to Program Design, 4e
32
switch Structures (continued)
• The expression is sometimes called the selector;
its value determines which statements are
selected for execution
• A particular case value must appear only once
• One or more statements may follow a case label,
so you do not need to use braces to turn multiple
statements into a single compound statement
Java Programming: From Problem Analysis to Program Design, 4e
33
switch Structures (continued)
• The break statement may or may not appear
after each statements1, statements2, ...,
statementsn
• A switch structure may or may not have the
default label
Java Programming: From Problem Analysis to Program Design, 4e
34
switch Structures (continued)
Java Programming: From Problem Analysis to Program Design, 4e
35
switch Structures (continued)
Java Programming: From Problem Analysis to Program Design, 4e
36
switch Structures (continued)
Example 4-23
switch (grade)
{
case 'A':
System.out.println("The grade is A.");
break;
case 'B':
System.out.println("The grade is B.");
break;
case 'C':
System.out.println("The grade is C.");
break;
Java Programming: From Problem Analysis to Program Design, 4e
37
switch Structures (continued)
case 'D':
System.out.println("The grade is D.");
break;
case 'F':
System.out.println("The grade is F.");
break;
default:
System.out.println("The grade is invalid.");
}
Java Programming: From Problem Analysis to Program Design, 4e
38
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
case of business customers)
• Output: customer’s account number and the
billing amount
Java Programming: From Problem Analysis to Program Design, 4e
39
Programming Example:
Cable Company Billing (continued)
• Solution
– Prompt user for information
– Use switch statements based on customer’s type
– Use an if statement nested within a switch
statement to determine amount due by each
customer
Java Programming: From Problem Analysis to Program Design, 4e
40
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 str1 is equal to string str2
an integer  0 if string str1  str2

Java Programming: From Problem Analysis to Program Design, 4e
41
Comparing Strings (continued)
String
String
String
String
String
str1
str2
str3
str4
str5
=
=
=
=
=
"Hello";
"Hi";
"Air";
"Bill";
"Bigger";
Java Programming: From Problem Analysis to Program Design, 4e
42
Comparing Strings (continued)
Java Programming: From Problem Analysis to Program Design, 4e
43
Comparing Strings (continued)
Java Programming: From Problem Analysis to Program Design, 4e
44
Chapter Summary
• Control structures are used to process programs
• Logical expressions and order of precedence of
operators are used in expressions
• If statements
• if…else statements
• switch structures
• Proper syntax for using control statements
• Compare strings
Java Programming: From Problem Analysis to Program Design, 4e
45