IF statement
An if statement tests a condition and executes the block following if the boolean expression result is true:
Code: Select all
if x > 0
do
print(x);
end
Multiple if branches are possible with the else keyword.
Code: Select all
if x == 1 do
' ..
else if x == 2 do
' ..
else if x == 3 do
' ..
else do
' ..
end
Code: Select all
if x == 1 do
' ..
else do
if x == 2 do
' ..
end
end
when statement
The when statement is similar to the if statement, but instead of testing a condition, it compares values by means of one or more is branches:
Code: Select all
when x
is 1 do
' ..
is 2 do
' ..
is other do
' ..
end
Code: Select all
when x
is 1 do
x = 2;
fall;
is 2 do
' ..
end
Code: Select all
when x
is 1 to 5, 7 do
' ..
is 8 to 10 do
' ..
is other do
' ..
end
Code: Select all
if a > 0 and a < 10 do
' ..
end
when a is 1 to 9 do
' ..
end
Code: Select all
when a is not 1 to 9 do
' ..
end
Code: Select all
when true is not a < 10 do ' (if a less than 10 is not true)
' ..
end
The for statement is a fixed loop that executes the specified number of times indicated by the .. to .. range:
Code: Select all
for i = 0 to 9 :++ do
' ..
end
Code: Select all
for i = 10 to 1 :-- do
' ..
end
Next to the for statement SharpBASIC also has a variable loop indicated by the keyword loop:
Code: Select all
loop do
' ..
end
Code: Select all
loop do
while n < 10;
' ..
n = n + 1;
end
Code: Select all
loop do
n = n + 1;
' ..
until n == 10;
end
break and skip
The other two keywords that control a variable loop are break and skip. The break statement is used to exit a loop at any time. The skip statement is used to skip one or more statements by immediately returning to the beginning of the loop:
Code: Select all
loop do
n:++;
' exit loop if n is 10
if n == 10 do
break;
end
' do not print if n is 5
if n == 5 do
skip;
end
print(n);
end