? c e??
) ?3?? e?¥àQ
)¨Hq? e???¥3?
)¨?ì? e?? ¥×ˉ
) ?3i
P¨1o3 C S F B L? D P O U J O V F
3.1 ?y
)?? e? V[?l1??? ¥3?
¨?¥
?? b
)D C aC++M] Java??
^YV
@ e? ?3
???
@[??? F?¥ b
)Yè??¥f ??1p4? 3?¥ê4?
? b1??
{?¥
?
^?μr b
)μ
H?? e?? ¥×ˉ3? b1?]BH
?u? 100Q b
3.1 ?y
)Hq?
–if and if...else
–3* if?
–switch?
–HqVr
T
)?ì?
–?ì : while, do, and for
–3*?ì
–break and continue
3.2 Hq?
)'?o
+?Hq?
–if and if...else
–3* if?
–switch?
–HqVr
T
e? J G?
e? if? oμHq1?
H?3? b?E1
if (booleanExpression)
{
statement(s);
}
Example:
if ((i >= 0) && (i <= 10))
{
System.out.println("i is an “ +
“integer between 0 and 10");
}
If?
@m
?:Vr
T
?
F
/BH?
true
false
3.2.2 if...else?
?E1
if (booleanExpression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}
if...else Example
?T??v? 059
?iA
U?¥
??54
Uír
{? b
if (radius >= 0)
{
area = radius*radius*PI;
System.out.println("The area for the “
+ “circle of radius " + radius +
" is " + area);
}
else
{
System.out.println("Negative input");
}
If…Else?
@m
?:Vr
T
?
F 1
/BH?
true false
?
F 2
3.2.3 if? ¥3*
)If if…else? ?¥? V[
^?i?
E¥ Java? —— ?
e if if…else?
b
)=a¥ if? ?13*?a if? ? b
3*¥
'
àμK? b
)B? if? 3*¨?
LC×ê4 b
)Else0 D]B v? ?¤Kí if0 M?
¥ b
3.2.3 if? ¥3*
)Example 3.1:
P¨3* if?
) è 2.2 o9
?·?~ { ”?7y
B???
|M~ { ?
q aM
??~ {-9
?ê
?á- b' èL
!M ?
q
?M
?M? b
)
! 3??] ?
q 7Mù 7.25% 15Mù 8.5% 30M
ù 9% b? M
?p¤M ?
qA
Uê?á-?9ê
?- b
TestIfElse.java Run Class
switch?
3* if?
P¨V?
P???4 b Java4
switch? μr1) ?×Hq b?E1
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;
}
switch?
3* if?
P¨V?
P???4 b Java4
switch? μr1) ?×Hq b?E1
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;
}
?Vr
TA??9
?
?Vr
TA??9
?
B?
B?
char abyte a
short
int?′
?′
A
A
?¨ ?| ?t b
?¨ ?| ?t b
switch?
3* if?
P¨V?
P???4 b Java4
switch? μr1) ?×Hq b?E1
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;
}
value1……valueNA?
D switch-expression′
μM]
? ???
switch-expression′D
case? ¥′?¥
H
3?? case? ?¥?
b
switch?
3* if?
P¨V?
P???4 b Java4
switch? μr1) ?×Hq b?E1
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;
}
1o3 break
^ Vê
¥ b Break? ??
?? switch? b?
break? ?i
/B? case? |$
3? b
switch?
3* if?
P¨V?
P???4 b Java4
switch? μr1) ?×Hq b?E1
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;
}
af ? default
^ Vê¥
?
¨ ?3?·?f ???1?
H
¥?T baf ?9
^C
switch? v¥Ka b
switch?
| è 3.1?¥ if3*? ?1 switch?
switch (year)
{
case 7: annualInterestRate = 7.25;
break;
case 15: annualInterestRate = 8.50;
break;
case 30: annualInterestRate = 9.0;
break;
default: System.out.println(
"Wrong number of years, enter 7, 15, or
30");
}
switch?
@m
numOfYears
annulInterestRate
=7.25
/BH?
7
default
System.out.println
(“Wrong number
of years”);
annulInterestRate
=8.50
annulInterestRate
=9.0
15 30
3.2.5 HqVr
T
HqVr
T9?1?"
?? b?E1
variable = booleanExpression ?
expression1 : expression2;
if (x > 0) y = 1
else y = -1;
y = (x > 0) ? 1 : -1;
3.3 ?ì?
)?ì
^ e?? v×ˉ3?¥2 b
–?ì?1×ˉ3?¥? ?1?ì8 b
–?ì8¥BQ3??1?ìY} b
–
???ìcB??ìHq
^ e??ì83?¥?
:Vr
T b
?QY}-a?1×?9
??ìHq b
–?ìHq1?×ˉ3??ì8?5?ì?? b
)?ì?
–while
–do
–for
–break and continue
3.3.1 while?ì
while (continue-condition)
{
// loop-body;
}
)Example 3.2:
P¨ while ?ì
)' è ?B"
?
?i9
??
{? 05V
U
{?2
? b
Run Class
TestWhile
while?ì
@m
false
true
Statement(s)
Next Statement
Continue condition?
3.3.2 do?ì
do?ì
^ while?ì¥M8 b?E1
do
{
// Loop body;
} while (continue-condition)
)53??ì89
??ìHq?T9
?
2T1?3??ì8?1L5??
do?ì