Code Block Delimiter

From NWN Lexicon
Jump to: navigation, search

A code block delimiter breaks code up into different sections of statements; variables declared within these sections are scoped - that is, they are in memory and can be accessed - and can be accessed only in the section where they are declared or in sections that appear within that section (child sections). A code block begins with a left curly brace ("{") and ends with a right curly brace ("}").

Functions, loops, structs, and conditionals all make use of code block delimiters. Conditional statements and loops (do, while, and for) do not require a code block delimiter if their total number of statements (or logical lines) do not exceed one. It is also possible to use anonymous code blocks for variable scoping only.

void main()
{ // start of the main code block
    {
        // inside of an anonymous code block
        int n = 13;
    }
    // the integer n is no longer scoped
 
    int x = 0;
 
    // a single line loop doesn't require code block delimiters
    // but still may be used
    while (x < 10)
        x++;
 
    // this for loop has multiple statements, so it requires
    // code block delimiters
    for (x = 10; x <= 30; x++)
    {
        PrintInteger(x);
        if (x > 25)
            PrintString("x is greater than 25.");
        else
            PrintString("x is less than or equal to 25.");
    }
} // end of the main code block

 author: Charles Feduke, editors: Lilac Soul, Mistress, additional contributors: Frank Succardi, Ken Cotterill