Java Reference
In-Depth Information
Thus, the for in the preceding program will usually be written like this:
You might want to try this. As you will see, the loop still runs exactly the same as it did
before.
Java also provides a decrement operator, which is specified as - - . This operator de-
creases its operand by one.
Create Blocks of Code
Another key element of Java is the code block . A code block is a grouping of two or more
statements. This is done by enclosing the statements between opening and closing curly
braces. Once a block of code has been created, it becomes a logical unit that can be used
any place that a single statement can. For example, a block can be a target for Java's if and
for statements. Consider this if statement:
Here, if w is less than h , both statements inside the block will be executed. Thus, the two
statements inside the block form a logical unit, and one statement cannot execute without
the other also executing. The key point here is that whenever you need to logically link two
or more statements, you do so by creating a block. Code blocks allow many algorithms to
be implemented with greater clarity and efficiency.
Here is a program that uses a block of code to prevent a division by zero:
Search WWH ::




Custom Search