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.
Script request: switch/case statement
-
- Regular
- Posts:297
- Joined:24 Jan 2012 18:22
Re: Script request: switch/case statement
Would definitly come in handy.
Re: Script request: switch/case statement
Feel like this has been asked for before but, please
Re: Script request: switch/case statement
Hey guys - can someone perhaps post a less trivial example than above to illustrate the mechanism / technique further please?
Cheers!
Cheers!