13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.2 Fundamental Language Elements67Example 3.8 A switch statement in Javaswitch (rval*k+zval){case 0:myl<strong>on</strong>.reset();break;case 1:case 4:// matches either 1 or 4y = zval+engrom;myl<strong>on</strong>.kuggle(y);break;default:// all other values end up hereSystem.out.println("Unexpected value.");break;}The default case is where c<strong>on</strong>trol goes when no other case matches theexpressi<strong>on</strong>. It is opti<strong>on</strong>al—you d<strong>on</strong>’t need to have <strong>on</strong>e am<strong>on</strong>g your switch cases.Its locati<strong>on</strong> is also arbitrary; it could come first, but by c<strong>on</strong>venti<strong>on</strong> programmersput it last in the sequence of cases, as a visual “catch all.”TIPFor whichever case is last (typically default), the ending break is redundantbecause c<strong>on</strong>trol will c<strong>on</strong>tinue outside the break—but we show it here in theexample, and use it ourselves in our code. Why? Well, code gets edited—forbug fixes and for feature additi<strong>on</strong>s. It is especially important to use break in allthe cases in switch statements that have no default case, but even inthose that do, we keep the break to avoid forgetting it, should another caseever be added or this last <strong>on</strong>e relocated. We recommend that you do the same.3.2.3.2Looping and Related StatementsThe while statement. Like the while c<strong>on</strong>struct in other computer languages,the expressi<strong>on</strong> inside the parentheses is evaluated, and if true, thestatement following it is executed. Then the expressi<strong>on</strong> is evaluated again, andif still true, the looped statement is again executed. This c<strong>on</strong>tinues until theexpressi<strong>on</strong> evaluates to false (Example 3.9).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!