Expression Switch-Case Fall-Through

Using the expression switch function, is there any way to declare a fall-through for cases like with C:

switch(x){
   case a:
   case b:
   case c:
         printf("Cases a, b, & c);
   case d:
         printf("Case d");
}

You mean an expression for a binding? Here’s a way you do a switch statement

switch (
15 ,  // value
1 ,   // case 1
24 ,  // case 2
15 ,  // case 3
44 ,  // return 1
45 ,  // return 2
46 ,  // return 3
- 1 )  // default

https://docs.inductiveautomation.com/display/DOC80/switch

Though normally instead of hardcoding a value like 15 that is where you’d have some other property.

Yes, but am I able to do a fall-through like the C code I posed with a single return for cases 1-3?

No.

1 Like

You’re best off making a separate property that checks a || b || c and then having that as one of the switch cases.

1 Like

Figured that was the case.

Note that the expression language doesn’t short-circuit sub-expression evaluation. Nor does it fold common sub-expressions. Anywhere those matter, the sub-expressions should be moved to separate bindings/expression tags.

As an aside I was confused, because your initial post mentions “C” (which only later you confirm as the language), and your example code shows “case c” :crazy_face:

(and because there is no “break” statement, technically everything falls through to “d” anyway)

1 Like