Control Structures I: Ava Rogramming
Control Structures I: Ava Rogramming
Java Programming
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.
2
Control Structures
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.
4
Relational Operators
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.
6
Some Characters of the Unicode Character Set and their Unicode Value as
a Decimal Integer
7
8
Relational Operators and Primitive Data Types
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
10
Comparing Strings
String str1 = "Hello";
String str2 = "Hi";
String str3 = "Air";
String str4 = "Bill";
String str5 = "Bigger";
11
Comparing Strings
12
//The String method compareTo
System.out.println("Line 6: " +
"str1.compareTo(str2) evaluates to "
+ str1.compareTo(str2)); //Line 6
System.out.println("Line 7: " +
"str1.compareTo(\"Hen\") evaluates to "
+ str1.compareTo("Hen"));
13
System.out.println("Line 8: " +
"str4.compareTo(str3) evaluates to "
+ str4.compareTo(str3)); //Line 8
System.out.println("Line 9: " +
"str1.compareTo(\"hello\") evaluates to "
+ str1.compareTo("hello")); //Line 9
15
/Logical operators System.out.println("!found && (hours >= 0)
evaluates to "
public class LogicalOperators + (!found && (hours >= 0)));
{ System.out.println("!(found && (hours >= 0))
public static void main(String[] args) evaluates to "
{ + (!(found && (hours >= 0))));
boolean found = true; System.out.println("hours + overTime <= 75.00
evaluates to "
double hours = 45.30;
+ (hours + overTime <= 75.00));
double overTime = 15.00;
System.out.println("(count >= 0) && (count <=
int count = 20; 100) "
char ch = 'B'; + "evaluates to "
+ ((count >= 0) && (count <= 100)));
System.out.printf("found = %b, hours = %.2f, System.out.println("('A' <= ch && ch <= 'Z')
overTime = " evaluates to "
+ "%.2f, count = %2d, ch = + ('A' <= ch && ch <= 'Z'));
%c%n%n",
}
found, hours, overTime, count, ch);
}
16
Comparing Strings
17
Comparing Strings
18
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.
19
Selection
One-way selection
Two-way selection
Compound (block of) statements
Multiple selections (nested if)
Conditional operator
switch structures
20
One-Way Selection
Syntax:
if (expression)
statement
Expression referred to as decision maker.
Statement referred to as action statement.
21
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
22
One-Way Selection
if (number < 0) //Line 4
number = -number; //Line 5
JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE); //Line 6
System.exit(0);
}
23
Two-Way Selection
Syntax:
if (expression)
statement1
else
statement2
else statement must be paired with an if.
24
Two-Way Selection
25
Two-Way Selection
Example 4-14
26
Two-Way Selection
Example 4-15
29
Compound (Block of) Statements
30
Multiple Selection: Nested if
Syntax: Else is associated with the
most recent incomplete if.
if (expression1) Multiple if statements can
statement1 be used in place of
if…else statements.
else
May take longer to
if (expression2)
evaluate.
statement2
else
statement3
31
Conditional (? :) Operator
Ternary operator
Syntax:
expression1 ? expression2 :
expression3
If expression1 = true, then the result of the
condition is expression2.
Otherwise, the result of the condition is
expression3.
33
switch Structures
switch (expression)
{ Expression is also
case value1: statements1
break;
known as selector.
case value2: statements2 Expression can be an
break;
... identifier.
case valuen: statementsn Value can only be
break;
default: statements integral.
}
34
switch Structures
35
switch Structures
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("The grade is
invalid.");
}
36
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.
39
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.
40
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.
41