Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

64 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:

The 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 workflow

In 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:

switch case grouping by pitch workflow

A 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: