Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial64 The switch
statement
MDN: Control Flow and Error Handling: switch statement
The switch statement provides the same functionality as
the if...else if...else statement, but offers a different
way to test a particular condition and provides a couple more features
to make the script’s logic manageable. The switch statement
is useful for needing to manage a lot of possibilities for a single
condition. The decision to use switch or if is
as much a matter of style as it is need. In Scripter,
switch is very useful for managing parameter control
changes.
Here is the basic syntax:
switch( <condition> ) {
case <result 1>:
// code block
break;
case <result 2>:
// code block
break;
default:
// code block
}In the above example:
Line 1: The condition is wrapped in parenthesis, just like with the
ifstatement, and evaluated only once for all of the subsequent cases.Line 2: The value of the condition is compared with each case provided.
Line 3: If there is a match with the first case, then the code for that case is executed.
Line 4: The
breakkeyword causes theswitchstatement to stop evaluating and the script moves on to any code which comes after the entire switch statement.Line 5: If the value for the condition does not match the first case, then the
switchstatement moves onto the next case. Otherwise, the code block is executed.Line 8: If none of the cases match, then the default code block is executed, if any. Typically, a good idea here is to display a warning to the console if none of the conditions are met.
switch workflow.Cases can also be grouped for common actions by listing cases one right after the other:, shown in lines 5 and 6:
switch( <condition> ) {
case <result 1>:
// code block
break;
case <result 2>:
case <result 3>:
// code block
break;
default:
// code block
}
switch case grouping workflowIn lines 5 and 6, the same code block is shared between two different
values. An example of both the basic structure and the combining of code
blocks is shown in the following example, showing which white key is
being played regardless of octave (shortened for clarity; a full
switch statement for all possible MIDI notes would be
hundreds of lines long):
switch (event.pitch) {
case 48:
case 60:
case 72:
Trace("C");
break;
case 50:
case 63:
case 74:
Trace("D");
break;
case 52:
case 65:
case 76:
Trace("E");
break;
default:
Trace(event.pitch);
}In the above example:
Line 1: The event’s pitch is evaluated.
Lines 2–6: If the event’s pitch is 48, 60, or 72, then "C" is output to the console and the switch statement is stopped.
Lines 7–11, 12–16: The same happens for the notes D and E and their respective pitches.
Lines 17–18: If the event’s pitch does not meet any of the conditions, then the pitch itself is output to the console.
switch case grouping by pitch
workflowA very common use of the switch statement is to handle
changes to controls in the Parameter Window. Parameter controls are
stored in the Scripter-sourced array PluginParameters and
changes are handled in the Scripter-sourced function
ParameterChanged().
Please note that this example gets into functions and parameter
controls, which are covered in a later section, and while lines 34–-42
contain the switch statement, the entire script helps to
provide the context for what is being done. Everything else in the
example has been covered in the previous sections:
var PluginParameters = [];
PluginParameters.push(
{
name:"Example Pulldown Menu",
type:"menu",
valueStrings:[
"Menu Item 1",
"Menu Item 2",
"Menu Item 3"
]
}
);
PlugParameters.push(
{
name:"Example Checkbox",
type:"checkbox",
defaultValue:0
}
);
PluginParameters.push(
{
name:"Select Group With",
type:"menu",
valueStrings:[
"Menu Item 1",
"Menu Item 2"
]
}
);
function ParameterChanged(param, value) {
switch (param) {
case 0:
// this control changed, so do something with the value
break;
case 1:
// this control changed, so do something with the value
default:
Trace("Error In ParameterChanged()");
}
}In the above example:
Line 1: The
PluginParametersarray is declared.Lines 2–31: Two parameter controls are pushed to the
PluginParametersarray as objects.Line 33: The
ParameterChangedfunction is declared with two arguments: the parameter’s index number in theparamvariable and the selected value in thevaluevariable.Lines 34–41: The
paramvariable, which is the parameter control’s array index, is evaluated for the two possible cases. If a value is passed which is not expected according to what is in thePluginParametersarray, then the error is output to the console.