Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

62 The if...else if Statement

MDN: Control Flow and Error Handling: if...else statement

The if...else if statement allows for the testing of multiple conditions. The structure simply expands on the if and if...else statements:

if ( <condition 1 is true> ) {
	<do something>
} else if ( <condition 2 is true> ) {
	<do something else>
}
if...else if workflow.

This becomes important when trying to capture different kinds of events, a common task in Scripter:

function HandleMIDI(event) {
	if (event instanceof NoteOn) {
		Trace("NoteOn " + event.pitch);
	} else if (event instanceof NoteOff) {
		Trace("NoteOff " + event.pitch);
	} else {
		Trace(event);
	}
}
if...else if workflow.

For each event, if it is either NoteOn or NoteOff, the pitch is written to the console. Otherwise, with the else statement, the event itself is written to the console. So, given a single Middle C note being played in a track, the following is displayed in the console:

***Creating a new MIDI engine with script***

Evaluating MIDI-processing script...
Script evaluated successfully!
NoteOn 60
NoteOff 60
[ControlChange channel:1 number:120 [All Sound Off] value:0]
>