User Tools

Site Tools


4rpl:commands:switch

This is an old revision of the document!


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.

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.1668540724.txt.gz · Last modified: 2022/11/15 14:32 by Karsten75