?c e??  ) ?3?? e?¥à Q )¨Hq?  e???¥3? )¨?ì?  e?? ¥×ˉ ) ?3i P¨1o3CSFBL?DPOUJOVF 3.1 ?y )?? e? V[?l1??? ¥3? ¨?¥ ??b )D CaC++M] Java?? ^YV @ e? ?3 ??? @[??? F?¥b )Yè??¥ f ??1 p4? 3?¥ê4? ?b1 ? ? { ?¥ ? ^?μrb )μ H?? e?? ¥×ˉ3?b1 ?]BH ?u? 100Qb 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?JG?  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? ?1 3*?a if? ?b 3*¥ ' àμK?b )B? if?  3*¨? LC×ê4b )Else0 D]B v? ?¤Kí if0 M ? ¥b 3.2.3 if? ¥ 3* )Example 3.1: P¨ 3* if?  ) è 2.2o9 ?·?~ { ”?7y B???  | M~ { ? qa M ??~ {-9 ?ê ?á-b' èL ! M ? q ? M ?M?b ) ! 3??] ? q 7 M ù 7.25% 15 M ù 8.5% 30 M ù 9%b? M ? p¤ M ? qA Uê?á-?9ê ?-b TestIfElse.java Run Class switch?  3* if?  P¨V? P??? 4b Java4  switch? μr1) ?×Hqb?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??? 4b Java4  switch? μr1) ?×Hqb?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? charabytea short  int?′ ?′ A A ?¨ ?| ?tb ?¨ ?| ?tb switch?  3* if?  P¨V? P??? 4b Java4  switch? μr1) ?×Hqb?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??? 4b Java4  switch? μr1) ?×Hqb?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??? 4b Java4  switch? μr1) ?×Hqb?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; }  a f ? default ^ Vê¥ ? ¨ ?3?·? f ???1? H ¥?Tb  a f ?9 ^C switch?  v¥Kab switch?  | è 3.1?¥ if 3*? ?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?¥2b –?ì?1×ˉ3?¥? ?1?ì8b –?ì8¥BQ3??1?ìY}b – ???ìcB??ìHq ^ e??ì83?¥? :Vr Tb ?QY}-a?1×?9 ??ìHqb –?ì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?ì¥M8b?E1 do { // Loop body; } while (continue-condition) )53??ì89 ??ìHq ?T9 ? 2T1?3??ì8 ?1L5?? do?ìb )while?ì? do?ì¥?1 uY??ìH q??ì819 ? ¨??]b do?ì @m false true Statement(s) Next Statement Continue condition? 3.3.3 for?ì for (?ìM  S? ?ìHq ???  ) { //loop body; } Example: int i; for (i = 0; i<100; i++) { System.out.println("Welcome to Java!” + i); } for?ì @m 9 ??ìM Vr T false true 9 ???Vr T ?ì8 /BH?  ?ìHq for?ì è0 ? Example3.3 P¨ for?ì ? ' è9 ?V 0.01? 1.0¥ ? -?b ?  [ 0.01?9' 0.01+0.02+0.03+……b TestSum Run Class ? Example3.4 P¨ 3*¥ for?ìb ? ' è P¨ 3*¥ for?ìu?eEVb TestMulTable Run Class 3.4 P¨1o3 break false true ?  F /BH?  ?ìHq ?  F break 3.5 P¨1o3 continue false true ?  F /BH?  ?ìHq ?  F continue P¨ break and continue ) Example 3.5: ? k break?  )'??9 ?V 1? 5¥?b )(1)?9??? 5 H?ì??b )(2)?9?v??? 6 H?ì??b TestBreak Run Class )Example 3.6: ? k continue?  )'??9 ?V 1? 5¥?? item=2 H?3?Y }b TestContinue Run Class ? S|?{S|¥ break?  ) Java? ?H?  ? V[{μS| S|9¨S M ?V Ub ){S|¥ break? K?S|S: ¥? -? ){S|¥ continue ?  V??S| S:¥?ì?  ¥? -Y}b outer: for (int i=1; i<10; i++) { inner: for (int j=1; j<10; j++) { if (i*j > 50) break outer; System.out.println(i*j); } } ?T| break outerD? break 5 ??3?$ 3.6 L èD ) Example 3.7 9 ?g r- ) g r?  ?Sg r yT l ? ?'y' ?4? M'y' 5000 ?íb4?1 è ?/ V ? Ub "S ^B M? 30000 ?íb )IB??? p ? ?¤ 30000 ?í31¥K lg r b g r- 4 ? 1 ?í ~5000 ?í 8% 5000 ?í ~10000 ?í 10% 10001 ?í[  12% FindSalesAmount Run Class 3.6 L èD ) Example 3.8 A U ?3?¥ ??? )' è ?¨ 3*?ìu?/  { 1 212 32123 4321234 543212345 PrintPyramid Run Class 3.6 L èD ) Example 3.9 A U í ? )' è 5??A U - 50? í ? ?? 10? ?b v? 1¥? ? ^ í ?' ?¥?y0oμ 1? ? 1 &b è ? 2 3 5 7 ^ í ?7 4 6 8  9? ^b )ù5s3 –  2 3 4 5 6 ……? k  ^?1 í ? – ?ó? ? ^?1 í ? – d9 í ?? ? – u? ?? í ? ?? 10? PrimeNumber Run Class ˉ5 ) 3.1 / } ? ?μ {· {2T x = 2; y = 3; if (x > 2) if (y > 2) { int z = x + y; System.out.println(“z is ” + z); } else System.out.println(“x is ” + x); ˉ5 ) 3.2 / } ? ?μ {· {2T x = 3; y = 2; if (x > 2) { if (y > 2) { int z = x + y; System.out.println(“z is ” + z); } } else System.out.println(“x is ” + x); ˉ5 ) 3.3 V[ü switch?yD??N¥ if?  ?$Q V ? V[ ?$ P¨ switch? ¥a? ^ I 1$ ) 3.4 switchM 1 p I 1 ? ??$ ?T) ? ¥ case-a à P¨1o3 break * 1/BH1 3?¥?  ^ I 1$ ) 3.5 3?/  switch? a y ^ $ x = 3; switch (x + 3) { case 6 y = 1; default: y += 1; } ˉ5 )3.6 3?/ ? a y ^ $ x = 0; Y = (x > 0) ? 1 : -1; )3.7 / ?ì8×ˉ Q$?ì { I 1$ int i = 1; while (i < 10) if (( i++ )%2 == 0) System.out.println(i); ˉ5 ) 3.8 while?ì? do?ì¥ uY ^ I 1$ ) 3.9 / ? H? ¤?¥ sum′M] ?$ for  int i = 0 i < 10; ++i { sum += i; } for  int i = 0 i < 10; i++ { sum += i; } ˉ5 ) 3.10 1o3 break¥T¨ ^ I 1$/ ?? ? 2 ? ?$ ? ?ó {2Tb int balance = 1000; while (true) { if (balance < 9) break; balance = balance - 9; } System.out.println(“balance is ” + balance); ˉ5 ) 3.11 1o3 continue¥T¨ ^ I 1$/ ?? ?2 ? ?$ ? ?ó {2Tb int balance = 1000; while (true) { if (balance < 9) continue; balance = balance - 9; } System.out.println(“balance is ” + balance); I?  ) 3.1 B? 3*¥ for?ìu?/ m? ) 3.2 ¨ while?ì p n 2 v? 12000¥Kl ? nb 1 12 123 1234 12345 public class Exercise3_2 { // Main method public static void main(String[] args) { int i = 1; while (i*i <= 12000 ) { i++; } System.out.println("This number is "+i); } } I?