Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial60 The if
statement
MDN: Control Flow and Error Handling: if...else statement
The if statement is the simplest of all branching. If a
specified condition is true, then do something. The
if statement has a very simple syntax:
if ( <condition is true> ) {
<do something>
}In the above example:
Line 1: All
ifstatements begin with the reserved word and the condition is contained within parentheses. For the condition to be tested, the comparison, logical, and type operators are used to evaluate whether a condition is true or not.Line 2: The code to execute is contained within curly braces on lines 1 and 3
if statement workflow.A common example is capturing events passed to
HandleMIDI():
function HandleMIDI(event) {
if (event instanceof NoteOn) {
Trace(event);
}
}In the above example:
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
eventisNoteOn, then theeventis written to the console.Line 4: The
ifstatement is closed with the curly brace.
if statement HandleMIDI()
workflow.This outputs exactly what would be expected with any MIDI data in the track:
***Creating a new MIDI engine with script***
Evaluating MIDI-processing script...
Script evaluated successfully!
[NoteOn channel:1 pitch:61 [C#3] velocity:100]
>