| 7 |
Switch/Case Statement (Decision) |
| 7.1 |
A switch statement takes a single variable or function and evaluates it. This value is then compared against the value contained in any number of case statements. If the value matches the case, that case is run.
<CFSCRIPT>
switch(variable)
{
case statements
}
</CFSCRIPT>
|
|
| 7.2 |
The case statements associated with a switch are always contained within curly braces ({}).
|
| 7.3 |
A switch statement must have at least one case or default statement within it.
<CFSCRIPT>
switch(variable)
{
case "michael":
{
operation;
break;
}
}
</CFSCRIPT>
|
|
| 7.4 |
Each case statement contains a single simple value (text or numeric) that will be compared against the switch value.
|
| 7.5 |
Each case must contain at least one operation that will occur if that case is 'run'.
|
| 7.6 |
Each case should also contain a break statement within the curly brackets
of the case. If not, then the code will not stop and will run each additional
case statement. |
| 7.7 |
A default statement exists that will run if no case has a value equal to the switch expression value. This follows all the rules concerning a case statement. While the break is not needed in a default statement, it is used for proper coding practices.
<CFSCRIPT>
switch(variable)
{
case "michael":
{
operation;
break;
}
default:
{
operation;
break;
}
}
</CFSCRIPT>
|
|