Control Structures

Control structures are what shape the logic of a program.  By doing things like looping and branching, complex behavior can be encoded.  For each control structure the surrounding parentheses are optional (but often lend an extra bit of visual distinction that aides readability).

Summary
Control structures are what shape the logic of a program.
Conditionally evaluate a block of code.
Choose between two alternate and exclusive blocks of code.
A pre-condition looping construct.
A post-condition looping construct.
This construct will iterate over each element in the given vector, assigning the current value to <x> each time.
Immediately exit the inner-most loop.
Jump immediately to the loop test condition.
Return control (and possibly a value) out of the current procedure.
Test a single variable for multiple possible values, allowing for the value of the variable to control program flow through one or more blocks of code.

if..then

Conditionally evaluate a block of code.  This is a standard if statement construct.

if x > 3 then
{
do_something();
x = 0;
}

if..then..else

Choose between two alternate and exclusive blocks of code.

if x > 3 then
do_something();
else
do_something_else();

while..do

A pre-condition looping construct.  This loop tests the condition before evaluating the body of the loop, and will continue until to do so until the test expression evaluates to false.

while x < 10 do
{
debugout("X = " + x + "\n");
x += 1;
}

do..while

A post-condition looping construct.  The block of statments are executed and then the loop condition is tested.  If it’s true, the loop begins again.  Note that this form of loop will always execute at least once.

do
{
debugout("X = " + x + "\n");
x += 1;
} while( x < 10 );

for <x> in <vector> do

This construct will iterate over each element in the given vector, assigning the current value to <x> each time.  It can also do type casting between int and string types (if the vector element is of the other type, it will convert it for you automatically.

for x in [1,2,3] do
debugout("X = " + x + "\n");

(* Same example, but as strings each time. *)
for x$ in [1,2,3] do
debugout("X = " + x$ + "\n");

break

Immediately exit the inner-most loop.  This will halt the evaluation of the inner-most loop, transfering evaluation to the statement immediately after the end of the current loop.

while( 1 )do
{
if( some_condition() )then
break;
do_stuff();
}
(* Control jumps to here upon break'ing. *)

continue

Jump immediately to the loop test condition.  This will transfer control to the loop test condition, which if it evaluates to true, will cause the loop to continue looping.

while( x < 10 )do
{
x += 1;
ch$ = mid$(str$, x, 1);
if( ch$ = "#" )then
continue;
do_some_stuff();
}

return

Return control (and possibly a value) out of the current procedure.  This will jump out of the current procedure (subroutine or function) and continue executing at the place it was called.

return;

or (for functions)

return x;

switch..case..break

Test a single variable for multiple possible values, allowing for the value of the variable to control program flow through one or more blocks of code.

switch x
{
case 1:
do_something_if_1();
break;
case 2:
do_something_if_2();
case 3:
do_something_if_2_or_3();
break;
case 4:
do_something_if_4();
break;
}

Note the use of the break statement in the above example.  The break statement immediately jumps program execution to the very end of the switch statement.  Switch statements exhibit a fall through behavior where the code in each case following a “true” case is executed unless a break statement is encountered.