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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

66Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to Javacomment c<strong>on</strong>sists of everything from the opening /* to the closing */sequence.3.2.3.1 C<strong>on</strong>diti<strong>on</strong>al Executi<strong>on</strong>An experienced programmer probably <strong>on</strong>ly needs to see examples of if andother such statements to learn them. It’s <strong>on</strong>ly a matter of syntax. Java breaksno new ground here; it adds no new semantics to c<strong>on</strong>diti<strong>on</strong>al executi<strong>on</strong>c<strong>on</strong>structs.The if-else statement. The if can take a single statement without anybraces, but we always use the braces as a matter of good style (Example 3.7).Example 3.7 A compound Java if-else statementif (x < 0) {y = z + progo;} else if (x > 5) {y = z + hmr<strong>on</strong>;myl<strong>on</strong>.grebzob();} else {y = z + engrom;myl<strong>on</strong>.kuggle();}TIPAn important thing to remember about the Java if statement (and all otherc<strong>on</strong>diti<strong>on</strong>al tests, such as while, do-while, and for) is that, unlike C/C++,its expressi<strong>on</strong> needs to evaluate to a boolean. In C/C++, numeric expressi<strong>on</strong>sare valid, any n<strong>on</strong>zero value being c<strong>on</strong>sidered true, but not so in Java.The switch statement. For a multiway branch Java, like C/C++, has aswitch statement, though the Java versi<strong>on</strong> of switch is a bit more restrictive.Example 3.8 shows the syntax.In Java, the expressi<strong>on</strong> in the switch statement must evaluate to eitheran int or a char. Even short and l<strong>on</strong>g are not allowed.As in C/C++, be sure to put the break statement at the end of each case,or else c<strong>on</strong>trol will flow right into the next case. Sometimes this is the desiredbehavior—but if you ever do that deliberately, be sure to add a comment.

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

Saved successfully!

Ooh no, something went wrong!