SharpBASIC has several statements to control the flow of a program. There is the conditional statement if, the comparison/switch statement when, and the loop statements for and loop.
An if statement tests a condition, which is a boolean expression, and executes the block following if the result of the boolean expression is true:
if x > 0
do
print(x);
end
Note the delimiters do..end, which mark the block to be executed following the boolean expression. Unlike many programming languages, SharpBASIC does not have the keyword then.
Multiple if branches are possible with the keyword else:
if x == 1 do
' ..
else if x == 2 do
' ..
else if x == 3 do
' ..
else do
' ..
end
Although two keywords are used for an else if statement, it actually behaves like elseif, so this syntax does not result in nested if-statements. Nested statements are always within do..end blocks:
if x == 1 do
' ..
else do
if x == 2 do
' ..
end
end
The when-statement is similar to the if-statement, but instead of testing a condition, it compares an identifiers value to one or more expressions by means of is-branches:
when x
is 1 do
' ..
is 2 do
' ..
is other do
' ..
end
The when-statement is similar to a switch statement in other languages. As such it supports fall-through, but only when explicitly stated:
when x
is 1 do
x = 2;
fall;
is 2 do
' ..
end
A mix of values and ranges can be tested per branch:
when x
is 1 to 5, 7 do
' ..
is 8 to 10 do
' ..
is other do
' ..
end
The when-statement can be used to test a range similar to an if-statement, for example:
if a > 0 and a < 10 do
' ..
end
when a is 1 to 9 do
' ..
end
It is also possible to do an opposite or not-comparison:
when a is not 1 to 9 do
' ..
end
Another example where the when-statement can be used instead of an if-statement:
if x <> 0 and y <> 0 do
' ..
end
' versus
when null is not (x and y) do
' ..
end
The when-statement can only be used with identifiers. It cannot compare an immediate:
when 0 is not (x and y) do ' ERROR
' ..
end
Because SharpBASIC translates all immediates into constants, it is good practice to define commonly used values as constants. Some values are predefined, such as true and false:
when true is not a < 10 do ' equivalent to "if a >= 10 do"
' ..
end
The for-statement, commonly known in other languages as the 'for-loop' is a fixed loop that executes the specified number of times. In the following example the loop will run 10 times, whereby the variable i starts with value 0 and is incremented by 1 each time the loop is repeated:
for i = 0 to 9 :++ do
' ..
end
Note the operator :++, which tells the direction of the iteration. The direction can be up (increment operator) or down (decrement operator):
for i = 10 to 1 :-- do
' ..
end
When a for-statement has ended without interruption, the iteration variable will be the maximum value plus one after an incremental iteration and the minimum value minus one after a decremental iteration. However, if the value is not within the range of the iterator's data type, the value is rotated:
dim i: uitr; ' unsigned
main do
for i = 9 to 0 :-- do
' ..
end
print(i); ' 4294967295
end
The loop-statement is SharpBASIC's variable loop:
loop do
' ..
end
In this form the loop would execute forever. Be careful! But SharpBASIC has some keywords to control the exit of a (variable) loop. The loop statement can be made conditional at the beginning with the keywords while and until:
loop do
while x > 1;
' ..
end
loop do
until x == 0;
' ..
end
Likewise, the loop can be made conditional at the end:
loop do
' ..
while x > 1;
end
loop do
' ..
until x == 0;
end
Note that the while and until condition statements are inside the do..end block. A condition statement at the beginning must always come before any other statement in a loop do ..end block. Similarly, a condition statement at the end must always be the last statement in a loop do ..end block. Both while and until are allowed at the beginning or at the end. However, only one condition statement is allowed.
Both the for-statement and the loop-statement can be influenced by the statements break and skip. The break-statement exits a loop unconditionally and continues the program with the first statement after the loop. The skip-statement immediately continues with the next loop skipping any remaining statements in a loop-block.
n = 0;
loop do
n:++;
' exit loop if n is 10
if n == 10 do
break;
end
' print empty line instead of 5
if n == 5 do
print();
skip;
end
print(n);
end
SharpBASIC supports jumps to labels (label statements). Label statements are addresses for the jump statement to jump to. Labels must start with @ followed by a name consisting of letters and/or numbers and underscores.
jump @MyLabel;
' ...
@MyLabel;
' ...
Jump statements are only allowed in subroutines and functions.
In the following example, the jump statement is used to exit a loop-statement.
sub sub1()
dim n:int = 0;
do
loop do
if n == 6 do
jump @here;
end
n:++;
end
@here;
print(n);
end
The next example demonstrates how to jump out of multiple for statements.
sub sub1()
dim i, j, k:itr;
do
for i = 1 to 9 :++ do
for j = 1 to 5 :++ do
for k = 1 to 3 :++ do
if i == 6 do
jump @final;
end
end
end
end
@final;
print(i);
end
The final way to exit a loop is with a return statement. In a subroutine return is used (without return value) to immediately exit the routine. In a function return can optionally return a value.
sub sub1()
dim i,j,k:itr;
do
for i = 1 to 3 :++ do
for j = 1 to 4 :++ do
for k = 1 to 5 :++ do
if k == 3 do
return;
end
end
end
end
end