User Tools

Site Tools


4rpl:commands:switch

Index
Flow Control

:!: Available in version 2.4.4 and later.

switch

switch

Description

Allow for multiple selection control in a single, more compact form that multiple if/else if/endif endif format. This is intended to improve clarity by reducing otherwise repetitive coding.

All code between switch and endswitch is evaluated in order, including code written outside of case..endcase statements. Each case expects an argument that evaluates to True or False at the time of being encountered. If True, the case is executed, and the execution of the switch is ended at the next endcase. If False, the case is skipped and the execution of the code inside the switch continues as normal.

Programming note

The case in a RPL language differs from case in say, C. The logic before a case statement ultimately can do whatever it wants. In the end it should put something on the stack that the case evaluates as true or false. But there can be any amount of code that does anything before a case statement. This could lead to, for instance, the code in the second example that will not execute either of the two `case` statements.

Examples

switch
   case(<-currentState 0 ==) 
      @HandleIdle
   endcase
 
   case(<-currentState 1 ==) 
      @HandleMove
    endcase
 
   case(<-currentState 2 ==) 
      @HandleFire
    endcase
    @HandleException # no case statement evaluated to TRUE
endswitch

Second example

true ->init 
Switch	
   Case (NOT (<-init) )
      Trace ("Case 1 is valid") 
   endCase 
   False ->init
   Case (<-init )
      Trace ("Case 2 is valid") 
   endCase
   Trace("There is no case to be made")  
endSwitch

See Also

4rpl/commands/switch.txt · Last modified: 2024/03/30 17:05 by Up-Level