? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 1
Chapter 6
Selection Statements
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 2
Chapter 6 Objectives
After you have read and studied this chapter,you should be able to
Implement selection control in a program using if
statements.
Implement selection control in a program using switch
statements.
Write boolean expressions using relational and boolean
operators.
Evaluate given boolean expressions correctly.
Nest an if statement inside another if statement’s then or
else part correctly.
Choose the appropriate selection control statement for a
given task.
Write applications using the ListBox class from javabook
and the Color class from the standard java.awt package.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 3
The if Statement
//Assume messageBox and inputBox are declared and created
//Assume testScore is declared
testScore = inputBox.getInteger("Enter test score:");
if (testScore < 70)
messageBox.show("You did not pass");
else
messageBox.show("You did pass");
This statement is
executed if the testScore
is 70 or higher.
This statement is
executed if the testScore
is less than 70.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 4
if ( testScore < 70 )
messageBox.show("You did not pass");
else
messageBox.show("You did pass");
Syntax for the if Statement
if ( <boolean expression> )
<then block>
else
<else block>
Then Block
Else Block
Boolean Expression
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 5
Control Flow
messageBox.show
("You did pass");
false testScore <
70?
messageBox.show
("You did not pass");
true
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 6
testScore < 80
testScore * 2 >= 350
30 < w / (h * h)
x + y != 2 * (a + b)
2 * Math.PI * radius <= 359.99
Relational Operators
< //less than
<= //less than or equal to
== //equal to
!= //not equal to
> //greater than
>= //greater than or equal to
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 7
if (testScore < 70)
{
messageBox.show("You did not pass");
messageBox.show("Try harder next time");
}
else
{
messageBox.show("You did pass");
messageBox.show("Keep up the good work");
}
Compound Statements
Use braces if the <then> or <else> block has
multiple statements.
Then Block
Else Block
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 8
if ( <boolean expression> ) {

}
else {

}
Style Guide
if ( <boolean expression> )
{

}
else
{

}
Style 1
Style 2
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 9
The if-then Statement
if ( testScore >= 95 )
messageBox.show("You are an honor student");
if ( <boolean expression> )
<then block>
Then Block
Boolean Expression
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 10
Control Flow of if-then
testScore >=
95?
false
messageBox.show
("You are an honor student");
true
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 11
Boolean Expressions and Variables
A B A && B A || B !A
false false false false true
false true false true true
true false false true false
true true true true false
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 12
Short-Circuit Evaluation
Consider the following boolean expression:
x > y || x > z
The expression is evaluated left to right,If x > y is true,then there’s no need to evaluate x > z because
the whole expression will be true whether x > z is true or not.
To stop the evaluation once the result of the whole expression is known is called short-circuit evaluation.
What would happen if the short-circuit evaluation is not done for the following expression?
z == 0 || x / z > 20
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 13
Operator Precedence Rules
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 14
The Nested-if Statement
The then and else block of an if statement can contain any valid
statements,including other if statements,An if statement
containing another if statement is called a nested-if statement,
if (testScore >= 70) {
if (studentAge < 10) {
messageBox.show("You did a great job");
}
else {
messageBox.show("You did pass"); //test score >= 70
} //and age >= 10
}
else { //test score < 70
messageBox.show("You did not pass");
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 15
Control Flow of Nested-if Statement
messageBox.show
("You did not
pass");
false inner if
messageBox.show
("You did pass");
false
testScore >=
70?
true
studentAge <
10?
messageBox.show
("You did a great
job");
true
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 16
Writing a Proper if Control
if (num1 < 0)
if (num2 < 0)
if (num3 < 0)
negativeCount = 3;
else
negativeCount = 2;
else
if (num3 < 0)
negativeCount = 2;
else
negativeCount = 1;
else
if (num2 < 0)
if (num3 < 0)
negativeCount = 2;
else
negativeCount = 1;
else
if (num3 < 0)
negativeCount = 1;
else
negativeCount = 0;
negativeCount = 0;
if (num1 < 0)
negativeCount++;
if (num2 < 0)
negativeCount++;
if (num3 < 0)
negativeCount++;
The statement
negativeCount++;
increments the variable by one
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 17
if – else if Control
if (score >= 90)
messageBox.show("Your grade is A");
else if (score >= 80)
messageBox.show("Your grade is B");
else if (score >= 70)
messageBox.show("Your grade is C");
else if (score >= 60)
messageBox.show("Your grade is D");
else
messageBox.show("Your grade is F");
Test Score Grade
90? score A
80? score? 90 B
70? score? 80 C
60? score? 70 D
score? 60 F
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 18
Matching else
if (x < y)
if (x < z)
messageBox.show("Hello");
else
messageBox.show("Good bye");
A
if (x < y)
if (x < z)
messageBox.show("Hello");
else
messageBox.show("Good bye");
B
Are and different?A B
if (x < y) {
if (x < z) {
messageBox.show("Hello");
}
else {
messageBox.show("Good bye");
}
}
Both and means…A B
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 19
ListBox
The ListBox class provides a list of items the user can
select.
It is considered a better user interface to allow the user to
enter only valid values instead of detecting invalid entries
after the user entered them.
First we create a ListBox object as
MainWindow mainWindow = new MainWindow( );
ListBox colorList = new ListBox( mainWindow,
“Select Color” );
We can create colorListas
… = new ListBox( mainWindow );
if we do not need a customized dialog title,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 20
ListBox – Add Items
After we create a ListBox object,we add items to it as
colorList.addItem("Magenta");
colorList.addItem("Cyan");
colorList.addItem("Red");
colorList.addItem("Blue");
colorList.addItem("Green");
The items will be added to
the list in the order
addItem is executed,
Then we execute
selection = colorList.getSelectedIndex();
to make the colorList appear on the screen…
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 21
ListBox – Items in colorList
The getSelectedIndex method returns the index value of
the selected choice.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 22
ListBox Methods
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 23
ListBox – Processing the User Selection
selection = colorList.getSelectedIndex();
if (selection == ListBox.NO_SELECTION)
messageBox.show("You made no selection");
else if (selection == ListBox.CANCEL)
messageBox.show("You canceled the ListBox");
else if (selection == 0)
messageBox.show("You selected Magenta");
else if (selection == 3)
messageBox.show("You selected Blue");
else if (selection == 4)
messageBox.show("You selected Green");
The use of public
constants
NO_SELECTION and
CANCEL makes the code
more readable,It also
makes the modification of
code easier.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 24
The switch Statement
int gradeLevel;
gradeLevel = inputBox.getInteger("Grade (Frosh-1,Soph-2,...):" );
switch (gradeLevel) {
case 1,outputBox.printLine("Go to the Gymnasium");
break;
case 2,outputBox.printLine("Go to the Science Auditorium");
break;
case 3,outputBox.printLine("Go to Harris Hall Rm A3");
break;
case 4,outputBox.printLine("Go to Bolt Hall Rm 101");
break;
}
This statement
is executed if
the gradeLevel
is equal to 1.
This statement
is executed if
the gradeLevel
is equal to 4.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 25
Syntax for the switch Statement
switch ( gradeLevel ) {
case 1,outputBox.printLine("Go to the Gymnasium");
break;
case 2,outputBox.printLine("Go to the Science Auditorium");
break;
case 3,outputBox.printLine("Go to Harris Hall Rm A3");
break;
case 4,outputBox.printLine("Go to Bolt Hall Rm 101");
break;
}
switch ( <arithmetic expression> ) {
<case label 1>,<case body 1>

<case label n>,<case body n>
}
Case
Body
Arithmetic Expression
Case
Label
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 26
switch With No break Statements
switch ( N ) {
case 1,x = 10;
case 2,x = 20;
case 3,x = 30;
}
x = 10;
false
trueN ==
1?
x = 20;
x = 30;
N ==
2?
N ==
3?
false
false
true
true
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 27
switch With break Statements
switch ( N ) {
case 1,x = 10;
break;
case 2,x = 20;
break;
case 3,x = 30;
break;
}
x = 10;
false
trueN ==
1?
x = 20;
x = 30;
N ==
2?
N ==
3?
false
false
true
true
break;
break;
break;
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 28
switch With the default Block
switch (ranking) {
case 10:
case 9:
case 8,messageBox.show("Master");
break;
case 7:
case 6,messageBox.show("Journeyman");
break;
case 5:
case 4,messageBox.show("Apprentice");
break;
default,messageBox.show("Input error,Invalid Data");
break;
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 29
Sample Program,Drawing Shapes
Problem Statement
Write an application that will draw a geometric shape
specified by the user,The user can specify the shape’s
position,size,and color,The program can draw lines,
rectangles,and circles in the colors magenta,cyan,red,
blue,or green.
Major Tasks
1,Get the shape the user wants to draw.
2,Get the color the user wants to use.
3,Get the position and size of the selected shape.
4,Draw the selected shape as specified
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 30
DrawShape – Design
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 31
DrawShape – Object Diagram
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 32
Loan Calculator – Development Steps
1,Start with a program skeleton,Define the
DrawShapeMain and the DrawShape classes.
2,Add code to allow the user to select a shape.
3,Add code to allow the user to select a color.
4,Add code to allow the user to specify the size and location of the selected shape.
5,Add code to draw the selected shape,
6,Finalize the code by tying up loose ends.