switch, check

Prev Next

Function Names

switch, check

Synopsis

switch/check ( expression ) { case ( comparison expression ) statement; case ... [ else : statement; ]
switch/check ( expression ) { case ( comparison expression ) { statements }; case ... [ else { statements } ]

Description

The switch function allows to execute exactly one code block among alternatives where the condition is met.
The check function allows to execute multiple code blocks wherever the conditions are met. The subsequent block may consist of a combination of case statements as well as else. The statement(s) after else will be executed if none of the cases were executed. Following the else, additional case statements are allowed to do another comparison round.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

1

Parameters

No.TypeDescription
1
input
all types Expression

The value provided here will be calculated once and then compared in the subsequent case procedures.

Examples

               echo("Try 'switch:'");
               for (a[] = 1, a[] <= 6, a[]++)
               {
                       print ( "  ",a[], ": " );
                       switch( a [] )
                       {
                               case (>1)   print("Greater than 1;   ");
                               case (<>3)  print("Not equal to 3;   ");
                               case (4,6)  print("Equals to 4 and 6;   ");
                               else:       print ("Not matching;  ");        // else will also enable another case round
                               case (>2)   print ("Greater than 2;  ");
                               else:       print ("Else: at most 2;  ");
                               else: echo ("Done");
                       }
               }

               echo("Try 'check':");
               for (a[] = 1, a[] <= 6, a[]++)
               {
                       print ( "  ",a[], ": " );
                       check( a [] )
                       {
                               case (>1)   print("Greater than 1;   ");
                               case (<>3)  print("Not equal to 3;   ");
                               case (4,6)  print("Equals to 4 and 6;   ");
                               else:       print ("Not matching;  ");        // else will also enable another case round
                               case (>2)   print ("Greater than 2;  ");
                               else:       print ("Else: at most 2;  ");
                               else: echo ("Done");
                       }
               }

Output

Try 'switch:'
  1: Not equal to 3;   Else: at most 2;  Done
  2: Greater than 1;   Else: at most 2;  Done
  3: Greater than 1;   Greater than 2;  Done
  4: Greater than 1;   Greater than 2;  Done
  5: Greater than 1;   Greater than 2;  Done
  6: Greater than 1;   Greater than 2;  Done
Try 'check':
  1: Not equal to 3;   Else: at most 2;  Done
  2: Greater than 1;   Not equal to 3;   Else: at most 2;  Done
  3: Greater than 1;   Greater than 2;  Done
  4: Greater than 1;   Not equal to 3;   Equals to 4 and 6;   Greater than 2;  Done
  5: Greater than 1;   Not equal to 3;   Greater than 2;  Done
  6: Greater than 1;   Not equal to 3;   Equals to 4 and 6;   Greater than 2;  Done
Try it yourself: Open LIB_Function_switch.b4p in B4P_Examples.zip. Decompress before use.

See also

case