User Tools

Site Tools


fall

Table of Contents

fall

fall through - a statement to indicate that the next is branch of a when statement must be tested

syntax

fall;

See also when

details

By default, the when statement, which can be considered SharpBASIC's switch statement, does not support implicit fall-through, so the fall statement must be used to force an explicit fall-through to the next branch.

A when statement can exist of multiple is branches, each having one or more conditions. Normally, if a condition of one branch is met, the block following that branch is executed after which the when statement is left. In order to also test the next branch, the fall statement can be included at the end of the execution block.

So the fall statement prevents an early exit of the when statement and forces testing of the next branch. But the next branch or branches are skipped if no further condition is met, except for the default branch, indicated by the keyword other. The example below illustrates this.

A fall statement is not allowed in the default branch. This is because the when statement is always left after the default branch is executed.

example

' SharpBASIC when with fall-through programming example
' -----------------------------------------------------
option strict;

incl "lib/sys.sbi";

dim n: int = 2;

main do

  when n
    is 1 do
      print("this is one");
      fall;
    is 2 do
      print("this is two");
      n = 3;
      fall;
    is 3 do
      print("this is three");
      fall;
    is 4 do
      print("this is four");
      fall;
    is 5 do
      print("this is five");
    is other do
      print("this is default");
  end

end
Output:

this is two

this is three

this is default

fall.txt · Last modified: 2023/07/05 20:47 by admin