Intro to Java Web-Notes_Part34

Intro to Java Web-Notes_Part34 - if [sales }= EDD] prize =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if [sales }= EDD] prize = "camput ElSE { if [sales }= lDDJn prize = "DVD player!"; ElSE { if [sales }= ED] _ r { ..: w Notice that the if-else conditions keep getting checked until a true one is encountered, and once a true condition is encountered then the rest of the cascading else-if is skipped. Therefore, many programmers like to indent their cascading else-ifs so that it is very easy to visually scan down the list of conditions, looking for the first true one. With that in mind, it appears that the current preferred style for writing cascading else-ifs, among Java programmers is as follows (this is the same cascading else-if as show 11 above, just indented differently): if (sales >= 500) prize : "computerl"; else if (sales >: 100) prize : "DVD player!"; else if (sales >= 50) prize = "radio"; else if (sales >= 20) prize : "wristwatch"; else prize = "nothing"; Actually, the curly braces were removed from the else clauses (these were not required in the original version of the cascading else-if -- I had added them just to clarify where each else clause started and ended). The final else is not required. If the final else is removed from the above example: if (sales >= 500) prize = "computerl"; else if (sales >: 100) prize : "DVD player!"; else if (sales >= 50) prize = "radio"; else if (sales >= 20) prize : "wristwatch"; // No trailing else after this if then it is possible that not a single statement in the cascading else-if is performed. In the above example nothing happens (no statement is performed) if the sales amount is less than twenty. Which If For This Else? It’s easy to tell that this else goes with the if before it... int num = ?; if (num > 10) System.out.println("Big"); else System.out.println("Small"); But which if does this else pair up with? if (num > 6) if (num < 12) System.out.println("close! "); else System.out.println("Sorryl "); GENERAL RULE: An else is always paired with the nearest possible if. Therefore, the above code is indented very badly. It would be better to indent it this way: if (num > 6) if (num < 12) System.out.println("closel"); else System.out.println("Sorry"); For the code shown above, what output is generated if num is 3‘? If num is 10? Some curly braces could be added just to clarify the bounds of the first if statement: if (num > 6) i if (num < 12) System.out.println("close!"); else System.out.println("Sorry"); l If we wanted the else to be associated with the first if, that can be accomplished with some well-placed curly braces: if (num > 6) { if (num < 12) System.out.println("close! "); else System.out.println("Sorryl "); Some Sample Problems For each of the code segments shown below, give the output of that code if mm is: Problem 1 if (num <= 10) i if (num :: 10) System.out.println("exactly"); else System.out.println("less"); else System.out.println("greater"); ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part34 - if [sales }= EDD] prize =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online