Script request: switch/case statement

Send us your feedback or ask for features.
Post Reply
oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19
Script request: switch/case statement

Post by oldgearguy » 16 Apr 2014 10:56

I'm not a big fan of large if...then...else if...then...else constructs. Most other programming languages have the concept of switch/case/break.

I would be OK if you limited it like other languages - integer values, 128 or 256 maximum case branches.

For those not familiar with it, instead of coding this:

if (myVal == 1) {
// do stuff related to 1
} else if (myVal == 2) {
//do stuff related to 2
} else if (myVal == 3) {
// do stuff related to 3
} else
// do other things

You would have:

switch (myVal)
{
case 1: // do stuff for 1
break;

case 2: // do stuff for 2
break;

case 3: // do stuff for 3
break;

default: // do other stuff
};

The above example is trivial, so the differences aren't huge, but the switch statement tends to read cleaner and allows you to do things like execute the code for case 3 either when case 2 is true or when case 3 is true by simply removing the break statement after case 2. I find that I tend to make less logic errors (and not have to worry about matched/correctly nested parentheses) with switch.

electrofux
Regular
Posts:297
Joined:24 Jan 2012 18:22

Re: Script request: switch/case statement

Post by electrofux » 16 Apr 2014 22:02

Would definitly come in handy.

Traxus
Regular
Posts:216
Joined:30 Nov 2012 06:19
Location:Detroit
Contact:

Re: Script request: switch/case statement

Post by Traxus » 17 Apr 2014 08:10

Feel like this has been asked for before but, please

Joe Soap
Regular
Posts:475
Joined:07 Jul 2012 15:04

Re: Script request: switch/case statement

Post by Joe Soap » 17 Apr 2014 17:27

Hey guys - can someone perhaps post a less trivial example than above to illustrate the mechanism / technique further please?

Cheers!

Post Reply