Summary


Selection and Iteration Statement Selection Table

The following table provides a quick summary of the Java selection and iteration statements. Feel free to copy it and keep it close by your computer until you’ve mastered their use.

Table 7-1: Java Selection And Iteration Statement Selection Guide

Statement

Execution Diagram

Operation

When To Use

if

Provides an alternative program execution path based on the results of the conditional expression. If conditional expression evaluates to true its body statements are executed. If it evaluates to false they are skipped.

Use the if statement when you need to execute an alternative set of program statements based on some condition.

if/else

Provides two alternative program execution paths based on the results of the conditional expression. If the conditional expression evaluates to true the body of the if statement executes. If it evaluates to false the statements associated with the else clause execute.

Use the if/else when you need to do one thing when the condition is true and another when the condition is false.

switch

The switch statement evaluates char, byte, short, and int values and executes a matching case and its associated statement block. Use the break keyword to exit each case statement to prevent case statement fall-through. Provide a default case.

Use the switch statement in place of chained if/else statements when you are evaluating char, byte, short, or int values.

while

The while statement repeatedly executes its statement block based on the results of the conditional expression evaluation. The conditional expression will be evaluated first. If true, the statement body will execute and the condition expression will again be evaluated. If it is false the statement body will be skipped and processing will continue as normal.

Use the while loop when you want to do something over and over again while some condition is true.

do/while

The do/while statement operates much like the while statement except its body statements will be executed at least once before the conditional expression is evaluated.

Use the do/while statement when you want the body statements to be executed at least once.

for

The for statement operates like the while statement but offers a more compact way of initializing, comparing, and incrementing counting variables.

Use the for statement when you want to iterate over a set of statements for a known amount of time.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net