Skill-Building Exercises


Summary

Program control-flow statements are an important part of the Java programming language because they allow you to alter the course of program execution while the program is running. The program control-flow statements presented in this chapter fall into two general groups: 1)selection statements, and 2) iteration statements.

Selection statements are used to provide alternative paths of program execution. There are three types of selection statements: if, if/else, and switch. The conditional expression of the if and if/else statements must evaluate to a boolean value of true or false. Any expression that evaluates to boolean true or false can be used. You can chain together if and if/else statements to form complex program logic.

The switch statement evaluates a char, byte, short, or int value and executes a matching case and its associated statements. Use the break keyword to exit a switch statement and prevent case fall through. Always provide a default case.

Iteration statements are used to repeat blocks of program code based on the result of a conditional expression evaluation. There are three types of iteration statements: while, do/while, and for. The while statement evaluates its conditional expression before executing its code block. The do/while statement executes its code block first and then evaluates the conditional expression. The for statement provides a convenient way to write a while statement as it combines loop variable counter declaration and initialization, conditional expression evaluation, and loop counter variable incrementing in a compact format.

The break and continue keywords provide finer-grained control over iteration statements. In addition to exiting switch statements, the break statement is used to exit for, while, and do loops. There are two forms of break statement: unlabeled and labeled. Unlabeled break exits its immediate containing loop; labeled break is used to exit a labeled iteration statement. The continue keyword terminates the current iteration statement loop and forces the start of a new iteration. Unlabeled continue will force a new iteration of its immediate containing loop; labeled continue will force a new iteration of a labeled iteration statement.




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