Previous Page TOC Next Page



11. Control Structures

The remaining group of statements, not described so far, are control structures. Control structures are:

• conditional

• multi branch

• loop

BNF:

statement   ::=   ...
      | conditional 
      | selection 
      | loop

11.1 Conditional: The If Statement

BNF:

conditional   ::=   if condition then
         statement-list
      { elseif expression then
         statement-list }
      [ else
         statement-list ]
      end if
condition   ::=   boolean-expression
statement-list   ::=   { statement }

Examples:

if val>0 then

handle_positiv
elseif val=0 then
handle_zero
else
print ("The value was negative!")
handle_error
end if if n < 0 then
n := -n
end if

The if statement is similar to conditionals in many other languages. Note that the if and else parts contain statement lists. This makes an explicit grouping symbol at the beginning of the statement list unnecessary. The "end if" is always required.

11.2 Selection: The Case Statement

The case statement allows a multi-way branch depending on an expression. It is similar to the case statement in Pascal or the switch statement in C. The case labels are set constants and selection of a case is done by applying an in operation to the expression and the case labels. The expression is evaluated and the resulting value is checked for membership in the label sets. The statements at the first matching label are executed. Only the statements between the label and the next label (if any) are executed. Then execution resumes after the end case instruction. The else part is executed if the value was not a member of any of the label sets. If no set contains the value and the case statement does not contain an else part, no nested instructions are executed and execution continues after the case statement.

BNF:

selection   ::=   case expression of
         { set_expr ":" statement-list }
         [ else statement-list ]
      end case

Examples:

case colour of

{red}:
label := "danger" {yellow}:
label := "warning" {green}:
label := "okay" else
handle_error end case case value of
{0..33}:
print ("low") {34..66}:
print ("medium") {67..99}:
print ("high") {100}:
print ("top") end case case ch of
{" ", "\n", "\t"}:
handle_whitespace {"a".."z", "A".."Z", "_"}:
handle_letter {"0".."9"}:
handle_number else
handle_error end case

If the sets used as case labels overlap and the test value is a member of more than one of the sets, only the instructions at the first match are executed.

11.3 Iteration: The Loop Statement

There is only one kind of loop structure in Blue. The loop is defined with the keywords loop ... end loop.

Exit from the loop is explicit by using the exit on keyword, which is part of the loop. Every loop must have at least one exit. Multiple exits are possible. By placing the exit at the beginning or end of the loop, behaviour of a pre-test or posttest loop (while and repeat in Pascal) can be implemented.

iteration   ::=   loop

statement-list
exit on condition
statement-list
{ exit on condition
statement-list }
end loop

Note that a statement list can be empty.

Examples:

The first example shows a loop with the exit instruction at the beginning. This construct has semantics similar to a while loop (where the condition in the while loop would be the negation of the exit condition in this example).

list.first

loop
exit on list.atEnd
print (list.current)
list.advance
end loop

Similarly, a post-test loop (repeat in Pascal) can be constructed by placing the exit instruction at the end of the loop.

In Blue, however, exit instructions can appear at any stage in the loop, increasing flexibility:

loop

readInt (i)
exit on i>0
print ("The number has to be greater than 0. Enter again.")
end loop

The last example shows the use of multiple exit instructions.

loop

s := getSelection
exit on s=0
error := process (s)
exit on error
end loop

A loop without an exit statement causes a compile time error. Exit statements are illegal outside a loop. In nested loops, exit always exits only the innermost loop. It is not possible to exit an outer loop from within a nested loop.

Previous Page TOC Next Page