Coldfusion cfscript, switch / case, where the case is between a range

I have a problem when I want to do one of three things ... if the value of x is 1-5 (inclusive), then A, if x is between 6-13 (inclusive) do B, and if x is between 14-16 do C.

I realized that in the case of the switch, everything will be fine, although, I think, I could use a simple IF / ELSE IF, however, since I encoded it, I cannot help but think that there is a more elegant way to declare it. switch / case (just in case, I come across a similar need, which has more than three options).

here is what i have:

switch ( x ) { case 1:case 2:case 3:case 4:case 5: // DO A break; case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13: // DO B break; case 14:case 15:case 16: // DO C break; } 

is there any way to specify β€œbetween” (inclusive or exclusive) in case of?

thanks

+4
source share
1 answer

Nope. The Switch statement is designed to work with single constants . If the comparison is such that the value can be changed in accordance with this rule , the only parameters are what you have already written or using if/else if/else , AFAIK. In most cases, the latter is cleaner than a bunch of hard-coded case IMO case .

+6
source

All Articles