Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 4:
Control Structures I
Java Programming:
Program Design Including Data Structures
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: Program Design Including Data Structures
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
repeating statements
Java Programming: Program Design Including Data Structures
3
Control Structures (continued)
Java Programming: Program Design Including Data Structures
4
Relational Operators
Relational operator:
Allows you to make comparisons in a program
Binary operator ( equal to, greater than, … )
Condition is represented by a logical expression in
Java
Logical expression: An expression that has a value
of either true or false
Java Programming: Program Design Including Data Structures
5
Relational Operators (continued)
Java Programming: Program Design Including Data Structures
6
Relational Operators and
Primitive Data Types
Can use with
Integral data types
Floating-point data types
char data type
Unicode Collating Sequence
Java Programming: Program Design Including Data Structures
7
Relational Operators and Primitive
Data Types (continued)
Java Programming: Program Design Including Data Structures
8
Relational Operators and Primitive
Data Types (continued)
Fragment of the UNICODE table
Java Programming: Program Design Including Data Structures
9
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: Program Design Including Data Structures
10
Comparing Strings (continued)
String
String
String
String
String
str1
str2
str3
str4
str5
=
=
=
=
=
"Hello";
"Hi";
"Air";
"Bill";
"Bigger";
Java Programming: Program Design Including Data Structures
11
Comparing Strings (continued)
Java Programming: Program Design Including Data Structures
12
Comparing Strings (continued)
Java Programming: Program Design Including Data Structures
13
Comparing Strings (continued)
Java Programming: Program Design Including Data Structures
14
Comparing Strings (continued)
Java Programming: Program Design Including Data Structures
15
Short-Circuit Evaluation
Computer evaluates a logical expression from left
to right and stops as soon as the value of the
expression is known
Java Programming: Program Design Including Data Structures
16
Selection
One-way selection
Two-way selection
Compound (block of) statements
Multiple selections (nested if)
Conditional operator
switch structures
Java Programming: Program Design Including Data Structures
17
One-Way Selection
Syntax:
if (expression)
statement
Expression called decision maker
Statement called action statement
Java Programming: Program Design Including Data Structures
18
One-Way Selection
Example
String letterGrade = “P”;
Int grade;
…
if
(grade < 75)
letterGrade = “F”;
…
Java Programming: Program Design Including Data Structures
19
One-Way Selection (continued)
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: Program Design Including Data Structures
20
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: Program Design Including Data Structures
21
Two-Way Selection
Syntax:
if (expression)
statement1
else
statement2
else statement must be paired with an if.
Java Programming: Program Design Including Data Structures
22
Two-Way Selection (continued)
Java Programming: Program Design Including Data Structures
23
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: Program Design Including Data Structures
24
Compound (Block of) Statements
Syntax:
{
statement1
statement2
.
.
.
statementn
}
Java Programming: Program Design Including Data Structures
25
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: Program Design Including Data Structures
26
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: Program Design Including Data Structures
27
Multiple Selection: Nested if
Example:
if (studentStatus = “AUDIT”)
letterGrade = “*”;
else
if (grade > 75 )
letterGrade = “P”;
else
letterGrade = “F”;
Java Programming: Program Design Including Data Structures
28
Conditional (? :) Operator
Ternary operator
Syntax:
expression1 ? expression2 : expression3
If expression1 = true, then the result of the
condition is expression2
Otherwise, the result is expression3
Java Programming: Program Design Including Data Structures
29
Conditional (? :) Operator
Example
letterGrade = (finalGrade > 75) ? “Pass” : “Fail”;
Same as
if (finalGrade > 75 )
letterGrade = “Pass”;
Else
letterGrade = “Fail”;
Java Programming: Program Design Including Data Structures
30
switch Structures
switch (expression)
{
case value1: statements1
break;
case value2: statements2
break;
...
case valuen: statementsn
break;
default: statements
}
Java Programming: Program Design Including Data Structures
Expression is also
known as selector
Expression can be an
identifier
Value can only be
integral
31
switch Structures (continued)
Java Programming: Program Design Including Data Structures
32
switch
Structures
(continued)
Example 4-24
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;
case 'D': System.out.println("The grade is D.");
break;
case 'F': System.out.println("The grade is F.");
break;
default: System.out.println("Grade is invalid.");
}
Java Programming: Program Design Including Data Structures
33
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: Program Design Including Data Structures
34
Programming Example:
Cable Company Billing
(continued)
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: Program Design Including Data Structures
35
Chapter Summary
Control structures are used to process programs
Logical expressions and order of precedence of
operators are used in expressions
Comparing strings
If statement
if…else statements
switch structures
Proper syntax for using control statements
Java Programming: Program Design Including Data Structures
36