Blocks

Prev Next

Introduction

Blocks are lexical structures where multiple statements are grouped together. Similar to C/C++ and Java, blocks are defined with braces { ... }.
Following rules apply:

  • Blocks may be specified any time, even just to improve code readability.
  • Loops and conditional statements decide whether to execute a succeeding single statement or a block with all statements inside.
  • Nested blocks are allowed
  • Empty blocks are allowed
  • Inside the blocks, statements are delimited with semicolons ;.

echo( One );                                 // Single statement
{
    echo( Two );                             // Multiple statements inside a block
    echo( Three );
    {                                        // Nested blocks
        echo( Four );
        { }                                  // Empty blocks are tolerated
        { {} }
        { ; ;; };;                           // Redundant semicolons are tolerated
    }
    if (true)                                // If statement
    {
        echo ( Five ); echo ( Six );
    }
    else:
    {
        echo( Six and half not shown );
    }
    {                                        // This block lies outside the if-statement
        echo( Seven );
    }
}
One
Two
Three
Four
Five
Six
Seven
Try it yourself: Open LAN_Features_Blocks.b4p in B4P_Examples.zip. Decompress before use.

See also

Statements