Go to main content

switch

The switch statement is similar to a series of IF statements on the same expression

SWITCH

NAME

switch - similar to a series of IF statements on the same expression

SYNOPSIS

switch expression do case statement1: expression endc case statement2: expression endc .... ends

DESCRIPTION

In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

Note that switch/case does loose comparison.

EXAMPLES

a = 5;
switch a do
     case 1:
     case 4:
          "case23";
     endc
     case 0:
          "case false";
     endc
ends

// output nothing

 

a = 5;
switch a do
      case 5:
          "case true";
      endc
ends

// output "case true"

 

a = 5;
switch a do
      case "5":
           "case true";
      endc
ends

// output "case true"

 

a = false;
switch a do
      case 1:
          "case true";
      endc

      default:
          "case default";
      endc
ends

// output "case default"

a = "pierre";
switch a do
     casere "ie": // regular expression comparison
          "match ie";
     endc

     default:
          "case default";
     endc
ends

// output "match ie"

 

a = "pierre";
switch a do
     casere "IE":
          "match ie";
     endc

     default:
          "case default";
     endc
ends

// output "case default"

 

a = "pierre";
switch a do
     casere:i "IE": // case insensitive comparison
           "match ie";
     endc

     default:
          "case default";
     endc
ends

// output "case default"

SEE ALSO

AUTHOR

Written by Xueyuan Peng, <xueyuanp@sednove.com>

MODIFICATIONS

1.0 2020-12-18 xueyuanp@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.