Control Statements (program flow)

information about SharpBASIC language elements
Post Reply
User avatar
frank
Site Admin
Posts: 40
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 loop statements count and 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 > 0
  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 or compound 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. It is also possible to do an opposite test:

Code: Select all

when a is not 1 to 9 do
  ' ..
end;
Although the when statement tests values rather than conditions, it is possible to test against true or false:

Code: Select all

when true is not a < 10 do ' (if a less than 10 is not true)
   ' ..
end;
count statement

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

Code: Select all

count i up 0 to 9 do
  ' ..
end;
The loop has a clause that indicates if the iteration is incremental (up) or decremental (down). Example of a decremental loop:

Code: Select all

count i down 10 to 1 do
  ' ..
end;
loop statement

Next to the count 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:

Code: Select all

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

Code: Select all

loop do
  n = n + 1;
  ' ..
  until n == 10;
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.

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 count-loop.
Keep it simple!
Post Reply