Let .. on .. is Statement

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:

Let .. on .. is Statement

Post by frank »

SharpBASIC has a special assignment statement called let .. on, which has the following syntax:

Code: Select all

let <variable> on <condition> is
  <expression> [else <expression>];
end;
The statement allows a numeric or string variable to be assigned conditionally, whereby the optional else-clause can be used to assign a different value if the condition is not met (false).

The let-statement allows a variable to be tested against different conditions using multiple on-branches:

Code: Select all

let <variable>
  on <condition> is
    <expression>;
  on <condition> is
    <expression>;
  on <condition> is
    <expression> [else <expression>];
  end;
As soon as one condition is met, the let-statement is left, similar to the switch statement when. If no condition is met, the value in the else-clause of the last branch (if present) will be assigned. Multiple else-branches are not allowed. An else-branch must always be the only or last branch of a let-statement.

The let-statement differs from the if and when-statements in that it can only be used for assignment. It is the only statement with is .. end; blocks allowed inside do .. end; blocks.

Example:

Code: Select all

const is
  hot = 1;
  cold = 0;
  frozen = -1;
  red = "red";
  blue = "blue";
  white = "white";
end;

dim water: int8 = cold;
dim color: str;

main do

  let color
    on water == frozen is
      white;
    on water == hot is
      red else blue;
    end;

  print(color);

end;
Output: blue
Keep it simple!
Post Reply