Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial61 The if...else
statement
MDN: Control Flow and Error Handling: if...else statement
The if...else statement accommodates handling code to
execute when a condition is either true or false. The syntax is
very similar:
if ( <condition is true> ) {
<do something>
} else {
<do something else>
}In the above example:
Lines 1–2: These are exactly the same as the
ifstatement.Line 3: The first curly brace closes the true code block and the false code block is opened with the
elsekeyword and the curly brace.Line 4: The code to be executed when the condition is false.
Line 5: The
falsecode block, and the entireif...elsestatement, is closed with the curly brace.
if...else workflow.To expand upon the example in HandleMIDI(), because
there are many types of events:
function HandleMIDI(event) {
if (event instanceof NoteOn) {
Trace("NoteOn");
} else {
Trace(event);
}
}In the above example, the following is happening:
Line 1: Scripter calls the
HandleMIDI()function, passing anevent.Line 2: The
ifstatement evaluates whether the event is an instance ofNoteOn.Line 3: If the event is
NoteOn, then the event is written to the console.Line 4: The
ifstatement is closed with the curly brace.
if...else workflow.For a simple track with one note, the following is output to the console:
***Creating a new MIDI engine with script***
Evaluating MIDI-processing script...
Script evaluated successfully!
NoteOn
[NoteOff channel:1 pitch:61 [C#3] velocity:64]
[ControlChange channel:1 number:120 [All Sound Off] value:0]
>
In the output, note the following:
Line 5: The
NoteOnevent is captured per the condition eventinstanceof NoteOnand all that is output is the string “NoteOn”.Lines 6–7: Neither event is a
NoteOn, so the script outputs the events themselves to the console.