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.

68Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to JavaExample 3.9 A Java while statementwhile (greble != null){greble.glib();greble = treempl.morph();}Technically, the while statement c<strong>on</strong>sists of the expressi<strong>on</strong> and a singlestatement, but that single statement can be replaced by a set of statements enclosedin braces (you know, the characters { and }). We will always use braces,even if there is <strong>on</strong>ly <strong>on</strong>e statement in our while loop. Experience has shownthat it’s a safer practice that leads to code that is easier to maintain. Just treatit as if the braces were required syntax, and you’ll never forget to add themwhen you add a sec<strong>on</strong>d statement to a loop.The do-while loop. To put the terminating check at the bottom of theloop, use do-while as shown in Example 3.10. Notice the need for theterminating semicol<strong>on</strong> after the expressi<strong>on</strong>.Example 3.10 A Java do-while statementdo {greble.morph();xrof = treempl.glib();} while (xrof == null);Die-hard Pascal programmers should note that Java has no repeat-untilstatement. Sorry. Of course the logic of an until(c<strong>on</strong>diti<strong>on</strong>) is equivalentto do-while(!c<strong>on</strong>diti<strong>on</strong>).The for loop. The for loop in Java is very similar to C/C++. It c<strong>on</strong>sists ofthree parts (Example 3.11):• The initializing expressi<strong>on</strong>, d<strong>on</strong>e up fr<strong>on</strong>t before the loop begins• The c<strong>on</strong>diti<strong>on</strong>al expressi<strong>on</strong> for terminating the loop

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

Saved successfully!

Ooh no, something went wrong!