Professional Documents
Culture Documents
03 Slide
03 Slide
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word
Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection Statements Chapter 4 Loops Chapter 5 Methods 19.1-19.3 in Chapter 19 Recursion
Chapter 6 Arrays
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1
Objectives
To declare boolean type and write Boolean expressions (3.2). To distinguish between conditional and unconditional && and || operators (3.2.1). To use Boolean expressions to control selection statements (3.33.5). To implement selection control using if and nested if statements (3.3). To implement selection control using switch statements (3.4). To write expressions using the conditional operator (3.5) . To display formatted output using the System.out.printf method and to format strings using the String.format method (3.6). To know the rules governing operand evaluation order, operator precedence, and operator associativity (3.7-3.8) .
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2
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 less than or equal to greater than greater than or equal to 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
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
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
Examples
System.out.println("Is " + num + " divisible by 2 and 3? " + ((num % 2 0) && (num % 3 0)));
System.out.println("Is " + num + " divisible by 2 or 3, but not both? " + ((num % 2 0) ^ (num % 3 0)));
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
10
LeapYear
Run
11
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
AdditionTutor
Run
12
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
13
14
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
15
Simple if Statements
if (booleanExpression) { statement(s); } 0) { if (radius area radius * radius * PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); }
false (radius 0)
false
true area radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area);
(A)
(B)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
16
Note
Outer parentheses required races can be omitted i the block contains a single statement quivalent
if ((i > 0) && (i < 10)) { System.out.println("i is an " + + "integer between 0 and 10"); }
(a)
if ((i > 0) && (i < 10)) System.out.println("i is an " + + "integer between 0 and 10");
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
17
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 next-line block style.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
18
true
Boolean Expression
false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
19
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
20
Equivalent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
21
animation
if (score 90.0) grade 'A'; else if (score 80.0) grade 'B'; else if (score 70.0) grade 'C'; 60.0) else if (score grade 'D'; else grade 'F';
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
22
animation
if (score 90.0) grade 'A'; else if (score 80.0) grade 'B'; else if (score 70.0) grade 'C'; 60.0) else if (score grade 'D'; else grade 'F';
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
23
animation
if (score 90.0) grade 'A'; else if (score 80.0) grade 'B'; else if (score 70.0) grade 'C'; 60.0) else if (score grade 'D'; else grade 'F';
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
24
animation
if (score 90.0) grade 'A'; else if (score 80.0) grade 'B'; else if (score 70.0) grade 'C'; 60.0) else if (score grade 'D'; else grade 'F';
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
25
animation
if (score 90.0) grade 'A'; else if (score 80.0) grade 'B'; else if (score 70.0) grade 'C'; 60.0) else if (score grade 'D'; else grade 'F';
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
26
Note
The else clause matches the most recent if clause in the same block.
int i = 1; int j = 2; int k = 3;
Equivalent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
27
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");
28
TIP
if (number % 2 == 0) even = true; else even = false;
(a) quivalent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
29
CAUTION
if (even == true) System.out.println( "It is even.");
(a) quivalent
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
30
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
31
ComputeTaxWithSelectionStatement
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Run
32
SubtractionTutor
Run
33
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Example: Guessing Birth Date The program can guess your birth date. Run to see how it works.
GuessBirthDate
Ru
34
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
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
35
ext Statement
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
36
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; }
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
37
The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
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 case statements are executed in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.
38
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
39
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
40
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
41
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
42
animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
43
animation
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
44
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
45
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
46
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
47
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
48
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
49
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
50
Conditional Operator
if (num % 2 == 0) System.out.println(num + is even); else System.out.println(num + is odd);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
51
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
52
Formatting Output
Use the new JDK 1.5 printf statement. System.out.printf(format, items); Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
53
Frequently-Used Specifiers
Example true or false 'a' 200 45.460000 4.556000e+01 "Java is cool" a boolean value a character a decimal integer a floating-point number
a number in standard scientific notation
Specifier Output %b %c %d %f %e %s
a string
int count = 5; double amount = 45.56;
items
display
54
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
55
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
56
Operator Precedence
var++, var-+, - (Unary plus and minus), ++var,--var (type) Casting ! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction) <, <=, >, >= (Comparison) ==, !=; (Equality) & (Unconditional AND) ^ (Exclusive OR) | (Unconditional OR) && (Conditional AND) Short-circuit AND || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
57
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
58
Operator Associativity
When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are leftassociative. a b + c d is equivalent to ((a b) + c) d Assignment operators are right-associative. Therefore, the expression a b + c 5 is equivalent to a (b + (c 5))
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
59
Example
Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 5 * (4 + 3) - 1 is evaluated as follows:
3 + 4 * 4 > 5 * (4 + 3) - 1 3 + 4 * 4 > 5 * 7 1 3 + 16 > 5 * 7 1
(3) multiplication (1) inside parentheses first (2) multiplication
3 + 16 > 35 1
(4) addition
19 > 35 1
(5) subtraction
19 > 34
(6) greater than
false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
60
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
61
But x becomes 2 in the following code, because ++a is evaluated to 1, then a is evaluated to 1.
int a int x 0; ++a + a;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
62
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
63
5 * (4 + 3) -
(1) 4 * 4 is the first subexpression that can be evaluated from left. (2) 3 + 16 is evaluated now. (3) 4 + 3 is now the leftmost subexpression that should be evaluated. (4) 5 * 7 is evaluated now.
19 > 34
(6) 19 34 is evaluated now.
false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
64