HomeContentsContents

A statement is a complete instruction to the interpreter. Statements are composed of expressions or constructs. Statements can be simple (such as comments), or they can be complex (such as loops and conditionals). Most statement types in ColdC have simple structures. Because statements can include other statements (using a compound statement) they can be used to build complex directives for the interpreter. Statements are grouped into four categories:

Simple Statements

Simple statements evaluate one instruction, once. There are four different simple statements in ColdC:

Comment

The comment statement does nothing. A comment statement is used for including notes within ColdC code. It is formatted as two forward slashes followed by any characters up to the end of the line, such as:


        // This is a comment.
    

The interpreter ignores comment statements completely; they are for the benefit of human readers. Note that comments in ColdC are actual statements, unlike comments in other languages, which are usually discarded by the preprocessor.

Up

Expression

The expression statement evaluates a complete expression, discarding the result. The syntax of an expression statement is an expression followed by a semicolon, such as:


        expression;
    

Examples:


        "this";
        y = x + 50;
    
Up

Compound

The compound statement is used to group one or more statements as a single statement. The compound statement is formatted as any number of statements contained within left and right curly braces, such as:


        {
            statement1;
            statement2;
            ...
            statementN;
        }
    

The interpreter evaluates each statement in the compound block in turn.

Up

Return

The return statement stops executing the current method and (optionally) returns a specific value. The return statement can have either of the following two forms:


        return expression;
        return;
    

If expression exists, the interpreter evaluates and returns its result from the method. Otherwise, if no expression is given the current object is returned.

Up Top

Conditional Statements

Conditional statements execute other statements if a test condition evaluates true. There are three types of conditional statements in ColdC:

Because the if statement and the if-else statement are similar, they can sometimes be ambiguous. The following code will produce unexpected results:


        if (a)
            if (b)
                c;
        else
            d;
    

The indentation suggests that the else clause should apply to the first if clause, but in fact it applies to the more recent one. Ambiguities like this can be avoided by using braces to create compound statements out of conditional and looping statements, even if there is only one statement. A less ambiguous way to write the above is:


        if (a) {
            if (b)
                c;
        } else {  
            d;
        }
    

if

The if statement is used to selectively execute code. It will only execute the code if the condition expression evaluates true. It has the following syntax:


        if (condition_expression) 
            statement
    

When the interpreter encounters the if statement it will first evaluate the condition expression. If the value of the condition expression is true then it will evaluate the statement. If it is not true it will skip the statement. The compound statement can be used to group multiple statements.

Up

if-else

The if-else statement is similar to the if statement. It further extends the if statement's syntax to include a second statement that is evaluated when the condition expression evaluates false. It has the following syntax:


        if (condition_expression) 
            true_statement
        else 
            false_statement
    

When the interpreter encounters the if-else statement it will first evaluate the condition expression. If the value of the condition expression is true, it will execute the true expression, otherwise it will execute the false expression. The compound statement can be used to group multiple statements.

Up

switch

The switch statement is used to compare one value against a series of other values. The switch statement is the most complicated statement in ColdC, and does vary from it's counterpart in C/C++ and some other programming languages, so rather than using abstract explanations we will start with an example:


        switch (val) {
            case 0:
                .message("The value is zero.");
            case 1 .. 10:
                .message("The value is between one and ten inclusive.");
            case 11 .. a:
                .message("The value is between eleven and 'a' inclusive.");
            case "foo", "bar".."baz":
                .message("The value is \"foo\" or between \"bar\" and \"baz\"");
            case a .. b, c .. d, 42:
                count = count + 1;
                .message("The value is in the counted area.");
            case ~perm:
                .message("Permission denied while getting the value.");
            default:
                .message("Did not recognize value.");
        }
    

This example illustrates all of the capabilities of the switch statement. The expression given by val in the example is the controlling expression and is compared against each of the cases inside the switch body until a match is found. Each case has a value or list of values to compare against. The values can be of any type, and need not be constant expressions. Ranges are specified using two dots (..) to separate the lower and upper bounds. The keyword default specifies an action to perform if no cases were matched by the controlling expression.

Here is a more formal description of the syntax of the switch statement:


        switch (controlling_expression) { 
            case expr_or_range, expr_or_range, ...: 
                statement
            case expr_or_range, expr_or_range, ...: 
                statement
            ...
            default: 
                default_statement
        }
    

When executing a switch statement, the interpreter scans through the list of cases and compares the controlling expression against each of the cases, evaluating the case from left to right until there is a match. When using a range, the lower and upper bounds must be of the same type and must be either integers or strings. If they are not the error ~type is thrown. When the interpreter finds a match, it will execute the statement for that case. The interpreter will not continue checking cases after a match.

If the interpreter does not find a match, it will instead execute the default statement. A default statement does not need to be defined. If a default statement is not defined nothing in the switch is executed.

Programmers experienced with C/C++ or other programming languages with similar switch statements should note that switch statements in ColdC differ from those in C/C++ in several respects. Because case values do not have to be constants, they may conflict, in which case the first match will take precedence. Also, there is no fall-through in ColdC switch statements; only the statements corresponding to the matching case will be executed. Because there is no fall-through, the break statement does not apply to switch statements. Finally, the default case must be placed last in the list of cases if it is given.

Up Top

Error Handling

The catch statement is used to catch and handle errors. The catch statement has the following syntax:


        catch error_code_1, error_code_2, ..., error_code_N {
            body_statement
        } with {
            handler_statement
        }
    

The keyword any can be substituted for the list of errors to to catch all errors. The handler statement is optional, and does not need to be defined.

If an error listed in the error list is thrown inside the body statement, the interpreter will catch the error and start executing the handler statement rather than aborting the method. After the handler is done, the interpreter continues executing from the point following the catch statement, as if the body statement had completed with no errors.

Inside the handler statement, the function error() can be used to retrieve the error code which triggered the handler and the function traceback() can be used to retrieve the propagated error stack. The function rethrow() is used to continue propagating an error. The function throw() can be used at any time to throw an error.

Here is an example of how a catch statement could intelligently handle a ~methodnf error thrown from the list_method() function:


        catch ~methodnf {
            code = list_method(method_name);
        } with {
            .message(strfmt("There is no method named %s.", tostr(method_name)), 'system);
        }
    

Note that critical expressions inside the body statement will override the behavior of the catch statement.

Top

Looping Statements

ColdC provides three kinds of looping statements. These statements are used to traverse a list, dictionary, or a range of numbers, or to execute a statement as long as a certain condition is true. ColdC also provides two kinds of jump statements which are used to prematurely exit from a loop or continue onto the next iteration of a loop.

for-list

The for-list statement is used to traverse a list. It has the following syntax:


        for variable in (what) 
            statement
    

The iteration variable must be a local variable (it cannot be an object variable), and what must be an expression resulting in a list or dictionary type. The interpreter executes statement once for each element in what, assigning the current element to variable for the iteration. Here is an example that uses a for-list statement with a list:


        for x in (["foo", "bar", "baz"])
            .message(x);
    

When executed, the method .message() is called three times, the first time with an argument of "foo" (the first element in the list), the second time with "bar" (the second element in the list), and the third time with "baz" (the last element in the list).

When using a dictionary as what, the interpreter assigns each association in the dictionary to variable (see Dictionary). For example, if the dictionary #[['count, 21], ['name, "foo"]] were to be used as what, the first iteration would set variable to ['count, 21] and the second iteration would set it to ['name, "foo"].

Both variable and what are assigned by value. This means that if a new value is assigned to either in the for-list statement, this assignment will not change the status of the loop. The interpreter will continue to iterate through what based on its original assignment. The interpreter also remembers where it is at in what and will continue as normal even if variable is reassigned in the for-list statement.

Up

for-range

The for-range statement is used to traverse a range of integers. The range is specified by separating the lower and upper bounds of the range with the range operator (..) inside square brackets ([ and ]). A for-list statement of this nature would look like:


        for variable in [lower .. upper] 
            statement
    

Both lower and upper must be expressions resulting in an integer type (unlike a range in a switch). The interpreter executes statement once for each number from lower to upper, with variable being assigned the current number in the range. An example of using a range in a list is:


          for i in [1 .. 10]
              .message(strfmt("iteration = %s", i));
    

Assigning a new value to variable within a for-range statement will not change the status of the loop; the interpreter remembers where it is at in the range and will continue as normal.

Up

while

The while statement is used to execute code as long as the condition expression is true. The while statement has the following syntax:


        while (condition_expression) 
            statement
    

The interpreter continually evaluates condition_expression and executes statement until the return value of condition_expression is false. Here is an example using a while statement:


        a = 1;
        while (a < 35)
            a *= 2;   
        .message(strfmt("total: %s", a));
    
Up

break

The break statement is used to exit a looping statement prematurely. The break statement has the following syntax:


        break
    

The interpreter jumps to the end of the innermost for-list, for-range, or while statement and continues executing. It is important to note this, as when executing multiple nested looping statements the break statement will only break out of the innermost loop.

Up

continue

The continue statement is used to jump to the next iteration of a loop. The continue statement has the following syntax:


        continue
    

The interpreter skips the remainder of the loop body and begins another iteration of the innermost looping statement. As with the break statement the continue statement only is relevant to the innermost looping statement.

Up Top

Contents Previous Next Functions

Valid XHTML 1.0 Strict