Control Statements (program flow)

information about SharpBASIC language elements
Post Reply
User avatar
frank
Site Admin
Posts: 27
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Control Statements (program flow)

Post by frank »

SharpBASIC has four statements to control the flow of a program. Two of them are the conditional statements if and when. The other two are the conditional loop statement for and the variable statement loop.

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 == 1 do
  print x;
end;
Note the do..end block, which marks 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 else keyword.

Code: Select all

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:

Code: Select all

if x == 1 do
  ‘ ..
else do
  if x == 2 do
    ‘ ..
  end;
end;
Note that if .. do .. end; is called an enclosing statement, just as sub .. do .. end; or func .. do .. 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;
As you can see, the when statement is SharpBASIC’s switch statement. As such it supports fall-through, but only when explicitly stated:

Code: Select all

when x
  is 1 do
    x = 2;
    fall;
  is 2 do
    ‘ ..
end;
A mix of values and ranges can be tested per branch:

Code: Select all

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:

Code: Select all

if a > 0 and a < 10 do
  ‘ ..
end;

when a is 1 to 9 do
  ‘ ..
end;
In this case the when statement is more efficient because it produces less code.

for statement

The for statement is a fixed loop that executes the number of times indicated by the .. to .. range:

Code: Select all

for i = 0 to 9 do
  ‘ ..
end;
loop statement

Next to the for statement, which is a fixed loop running a predefined number of times, SharpBASIC also has a variable loop indicated by the keyword loop:

Code: Select all

loop do
  ‘ ..
end;
In this form the loop would execute forever. Be careful! But SharpBASIC has four control keywords to influence the variable loop. The loop can be made conditional at the beginning with the while statement:

Code: Select all

loop do
  while n < 10;
  ‘ ..
  n = n + 1;
end;
Likewise, the loop can be made conditional at the end with the until statement:

Code: Select all

loop do
  n = n + 1;
  ' ..
  until n == 10;
end;
Note that the while and until statements are inside the do..end block. A while statement must always come before any other statement in a loop do ..end block. Similarly, a until statement must always be the last statement.

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 = n + 1;
  ‘ 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;
The break and skip statements can also be used in a for loop.
Keep it simple!
Post Reply