AnyLogic
Expand
Font size

Block {…} and indentation

A block is a sequence of statements enclosed in braces {…}. There can be one, several, or even no statements inside a block. Block tells Java that the sequence of statements should be treated as one single statement, and therefore blocks are used with if, for, while, etc.

Java code conventions recommend to place the braces on separate lines from the enclosed statements and use indentation to visualize the nesting level of the block contents:

If the block is a part of a decision or loop statement, the braces can be placed on the same line with if, else, for, or while:

if( … ) {
    <statements>
} else {
    <statements>
}

while( … ) {
    <statements>
}

In switch statement it is recommended not to indent the lines with case:

switch( … ) {
case …:
    <statements>
    break;
case …:
    <statements>
    break;
…
}
How can we improve this article?