| Playing With Blocks - Grouping Statements |
|
When you used a for statement, all the statements that were
part of the loop had to be between braces ({ and }); statements before
the opening brace or after the closing brace were not part of the
loop. Such a group is called a block, and it has some special
properties. The same rules apply to the group of statements following
an if or an else, or the statements after a while. A block can be used anywhere a single
statement can be used – as you already saw, you can put either a
single statement or a block of statements after an if. The main use of the block construct is to tell the compiler that the group of statements are together in relation to other statements or groups, such as being the loop- controlled statements of a while loop: at the end of the block, the program will not continue – flow through – but will go back to the loop control part (between the parentheses after the keywords for or while) to decide if another iteration is necessary. The break and continue statements also apply only to the blocks in which they appear. continue tells the program to skip the rest of the loop block and go immediately to the beginning of the loop for another possible iteration, and break tells it to go to the end of the loop or switch block and exit it. break can only be used in a loop, either for or while, or in a switch block; continue is only allowed in a loop. |
Home Overview Outline About the Author Members Download Contact Us