



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-
Chapter 1 Introduction to Computers, Programs,
and Java
Basic computer skills such as using Windows,Internet Explorer, and Microsoft WordChapter 2 Primitive Data Types and OperationsChapter 3 Control StatementsChapter 4 MethodsChapter 5 Arrays
Prerequisites for Part I
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-
)
)
)
)
)
)
)
)
)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
BooleanExpressiontrue Statement(s)
false
(radius >= 0)true
area = radius * radius * PI;System.out.println("The area for the circle of " +"radius " + radius + " is " + area);
false
(A)
(B)
if (booleanExpression) { statement(s); }
if (radius >= 0) { area = radius * radius * PI;System.out.println("The area" “ for the circle of radius "+ "radius is " + area); }
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
if
(
(i > 0) && (i < 10)
)^
{
System.out.println("i is an " +
(a)
Equivalent
(b)
if
(
(i > 0) && (i < 10)
)
System.out.println("i is an " +
Outer parentheses required
Braces can be omitted if the block contains a singlestatement
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
if
(booleanExpression)
statement(s)-for-the-true-case; } else
statement(s)-for-the-false-case; }
BooleanExpression
false
true
Statement(s) for the false case
Statement(s) for the true case
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
if (radius >= 0) {
area = radius * radius * 3.14159;System.out.println("The area for the “
} else {
System.out.println("Negative input"); }
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
if (i > k)
System.out.println("A");
else
System.out.println("B");
(a)
Equivalent
( b )
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, Fifth Edition, (c) 2005 Pearson Education, Inc. All
int
i
int
j
int
k
if
(i
j)
if
(i
k)
System.out.println("A");
} else
System.out.println("B");
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
if (even == true)
System.out.println(
"It is even.");
(a)
Equivalent
if (even)
System.out.println(
"It is even.");
(b)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
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, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
status is 0
Compute tax for single filers
break
Compute tax for married file jointly
break
status is 1
Compute tax for married file separatly
break
status is 2
Compute tax for head of household
break
status is 3
Default actions
default
Next Statement
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-
The keyword break is optional,but it should be used at the end ofeach case in order to terminate theremainder of the switchstatement. If the break statementis not present, the next casestatement will be executed.
switch (switch-expression) {case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
… case valueN: statement(s)N;
break;
default: statement(s)-for-default;}
The default case, which isoptional, can be used to performactions when none of thespecified cases matches theswitch-expression.
The case statements are executed in sequentialorder, but the order of the cases (including thedefault case) does not matter. However, it is goodprogramming style to follow the logical sequenceof the cases and place the default case at the end.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-