Command SWITCH / SELECT - CASE method
 
Description It is possible to emulate the SELECT CASE or SWITCH CASE function found in other languages.
This is used to test the contents of  a variable and selectively process data according to its value.

The method used in the Itron TFT modules is different since it can directly jump to functions located anywhere in the program
provided they use a common naming method.

It makes use of the ability to compile a function name in a variable and then use the RUN(variable); command
The example combines "case_" with "DC" to form a function name "case_DC".
The program can then contain functions to serve all the input options

This removes the need for multiple IF statements.
 
Example
A typical 'c' example is as follows

switch
( input )
       
{
           
case "DC":  
            
   DCfunc();
               
break;
           
case "DCT":         
               
DCTfunc()
               
break;
           
case "C":        
               
Cfunc();
               
break;
           
case "D":       
               
Dfunc();
               
break;
            default :
                defunc();
       
}

public DCfunc() {..................}
public DCTfunc() {..................}
public Cfunc() {..................}
public Dfunc() {..................}
public defunc() {................}
The equivalent method is shown below 

LOAD(chkstr , "," , input , ",");  
CALC(tmp, ",C,D,DC,DCT," , chkstr, "FIND");
IF(tmp< 0 ? case_default : [LOAD(chkstr,"case_",input); RUN(chkstr);]);


This 3 line technique adds "," to front and end of the input value and loads into chkstr
If input="DC" then chkstr=",DC,"

A CALC command compares the chkstr with a list to identify if the input value exists
The existing commands are defined by ",C,D,DC,DCT,"

If tmp is -1 the input does not exist and the default function "case_default" is RUN.

If tmp is 0+n, the command exists and a prefix is added to input and RUN.
Where input = "DC", it exists so the function name "case_DC" is created and RUN

tmp and chkstr are predefined variables type S8 and TXT.


FUNC(case_DC) {..............}

FUNC(case_DCT) {..............}
FUNC(case_C) {..............}
FUNC(case_D) {..............}
FUNC(case_default) { ............}
 
Update Information