![]() |
<Control Structures> |
[ if / if-else | while | for | return | exit | switch ]
The if / if-else statement provides the simplest form of conditional execution. The expression (usually called the condition) that must be of type bool is evaluated. If it evaluates to true, the statement immediately following the expression in parentheses is executed. It is usually called the then-statement. If the expression evaluates to false, the statement following the else (if any) is executed. This statement is commonly referred to as the else-statement. An else will always be bound to the closest else-less previous if statement.
The simplest form of iteration (or looping) is provided by the while statement. The expression (called the condition) that must be of type bool is evaluated. If it evaluates to false, the while statement has been executed. If on the other hand it evaluates to true, the while statement (usually called the body of the while), is executed. Once executed, control is passed once again to the entire while statement, and the procedure is repeated.
The for statement is a variant of while. The three expressions (called the initialization, condition, and increment) are all optional. If present, the condition must be of type bool. Initially, the initialization expression is evaluated for its side-effects. Hereafter, as with while, the condition is evaluated and if false, the for statement has been executed. If the condition evaluates to true, the statement (called the body of the for) is executed. Once the statement has been executed, the increment expression is evaluated for its side-effects and control is passed again to the entire for statement and the procedure is repeated, ignoring the initialization expression. The statements "for ( E1; E2; E3 ) S" and "{ E1; while ( E2 ) { S; E3; } }" are completely equivalent.
The return statement serves two purposes in <bigwig>. The return without an expression is used for returning from procedures (functions with return type void). The return statement has the effect that control is passed to the point immediately following the point that called the procedure (function). The second is used for returning values from functions. The type of the expression, which is required to be the same as the return-type of the function, is evaluated and returned to the point where the function was called. Hereafter control resumes at this point. Another use of the return statement is in conjunction with the factor statement.
The exit statement causes the session to terminate. If an expression is supplied, it is required to be either of type html or string. If the type is html, the document resulting from the evaluation of the expression will be shown to the client just prior to termination. If the type is string, the expression is assumed to evaluate to an URL. The page shown will contain a jump to the specified URL. If on the other hand, no expression is supplied, a default termination message will be shown.
The switch statement is basically a multi-dimensional if statement. The expression in parentheses (called the switch-expression) is evaluated to a value. Then, the case-expressions are evaluated, in order, one by one, until the value of one of them is equal to the switch-expression. At this point the rest of the case-expressions are ignored and control is past to the corresponding case's statement after which the switch statement has completed. All the involved expressions must have the same basic type. The switch statement works with all basic types, except html and the expressions involved do not have to be constant (as in, for instance C or Java). A switch statement is allowed to have maximum one default branch and if such a branch exists, it must be specified after all the case branches. If none of the expressions matched the switch-expression and there is a default branch, the default-statement is executed. The break keyword is required at the end of each branch.
|
bigwig@brics.dk Last updated: November 2, 2001 |
|