Professional Documents
Culture Documents
Statements
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
Comparison Operators
Operator Name
<
less than
<=
>
greater than
>=
==
equal to
!=
not equal to
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
Boolean Operators
Operator Name
!
not
&&
and
||
or
exclusive or
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
!p
true
false
false
true
Example
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
p2
p1 && p2
false
false
false
false
true
false
true
false
false
true
true
true
Example
(3 > 2) && (5 >= 5) is true, because (3 >
2) and (5 >= 5) are both true.
(3 > 2) && (5 > 5) is false, because (5 >
5) is false.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
p2
p1 || p2
false
false
false
false
true
true
true
false
true
true
true
true
Example
(2 > 3) || (5 > 5) is false, because (2 > 3)
and (5 > 5) are both false.
(3 > 2) || (5 > 5) is true, because (3 > 2)
is true.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
p2
p1 ^ p2
false
false
false
false
true
true
true
false
true
true
true
false
Example
(2 > 3) ^ (5 > 1) is true, because (2 > 3)
is false and (5 > 1) is true.
(3 > 2) ^ (5 > 1) is false, because both (3
> 2) and (5 > 1) are true.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
10
Selection Statements
if Statements
switch Statements
Conditional Operators
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
11
Simple if Statements
if (booleanExpression) {
statement(s);
}
Boolean
Expression
true
Statement(s)
(A)
false
if (radius >= 0) {
area = radius * radius *
PI;
System.out.println("The
area"
+ " for the circle of
radius "
+ radius + " is " +
false
area);
(radius >= 0)
}
true
area = radius * radius * PI;
System.out.println("The area for the circle of " +
"radius " + radius + " is " + area);
(B)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
12
Note
Outer parentheses required
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
13
Caution
Adding a semicolon at the end of an if clause is
a common mistake.
if (radius >= 0); Wrong
{
area = radius*radius*PI;
System.out.println(
"The area for the circle of radius " +
radius + " is " + area);
}
This mistake is hard to find, because it is not a
compilation error or a runtime error, it is a
logic error.
This error often occurs when you use the nextline block style.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
14
The if...else
Statement
if (booleanExpression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
true
Boolean
Expression
false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
15
if...else Example
if (radius >= 0) {
area = radius * radius * 3.14159;
System.out.println("The area for the
+ circle of radius " + radius +
" is " + area);
}
else {
System.out.println("Negative input");
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
16
Multiple Alternative if
Statements
if (score >= 90.0)
grade = 'A';
else
if (score >= 80.0)
grade = 'B';
else
if (score >= 70.0)
grade = 'C';
else
if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Equivalent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
17
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
18
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
19
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
20
animation
grade is C
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
21
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
22
Note
The else clause matches the most recent if clause
in the same block.
int i = 1;
int j = 2;
int k = 3;
Equivalent
int i = 1;
int j = 2;
int k = 3;
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
(a)
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
23
Note, cont.
Nothing is printed from the preceding
statement. To force the else clause to
match the first if clause, you must add a
pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
24
TIP
if (number % 2 == 0)
even = true;
else
even = false;
(a)
Equivalent
boolean even
= number % 2 == 0;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
(b)
25
CAUTION
if (even == true)
System.out.println(
"It is even.");
(a)
Equivalent
if (even)
System.out.println(
"It is even.");
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
(b)
26
switch Statements
switch (status) {
case 0: compute taxes for single filers;
break;
case 1: compute taxes for married file jointly;
break;
case 2: compute taxes for married file separately;
break;
case 3: compute taxes for head of household;
break;
default: System.out.println("Errors: invalid
status");
System.exit(0);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
27
break
break
break
break
status is 1
status is 2
status is 3
default
Default actions
Next Statement
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
28
29
switch (switch-expression)
{
case value1:
statement(s)1;
break;
case value2:
statement(s)2;
break;
case valueN:
statement(s)N;
The case statements
are executed in
break;
sequential order, but the order of the
default: statement(s)-forcases (including the default case) does
default;
not matter.
However, it is good
programming
style to follow the logical
}
30
animation
Suppose ch is 'a':
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
31
animation
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
32
animation
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
33
animation
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
34
animation
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
35
animation
switch
case
case
case
}
(ch)
'a':
'b':
'c':
{
System.out.println(ch);
System.out.println(ch);
System.out.println(ch);
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
36
animation
Suppose ch is 'a':
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
37
animation
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
38
animation
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
39
animation
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
40
animation
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
41
Conditional Operator
if (x > 0)
y=1
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
(booleanExpression) ? expression1 : expression2
Ternary operator
Binary operator
Unary operator
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
42
Conditional Operator
if (num % 2 == 0)
System.out.println(num + is even);
else
System.out.println(num + is odd);
System.out.println(
(num % 2 == 0)? num + is even :
num + is odd);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
43
JDK 1.5
Feature
Formatting Output
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
44
Frequently-Used
Specifiers
JDK 1.5
Feature
Specifier Output
Example
%b
a boolean value
true or false
%c
a character
'a'
%d
a decimal integer
200
%f
a floating-point number
45.460000
%e
4.556000e+01
%s
a string
"Java is cool"
int count = 5;
items
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
display
45
Creating Formatted
Strings
System.out.printf(format, item1, item2, ..., item k)
String.format(format, item1, item2,
..., itemk)
String s = String.format("count is %d and amount is
%f", 5, 45.56));
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
46
Operator Precedence
How to evaluate 3 + 4 * 4 > 5 * (4 + 3) 1?
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
47
Operator Precedence
48