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
if
statements 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
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
if
statement evaluates whether the event is an instance ofNoteOn
.Line 3: If the
event
isNoteOn
, then theevent
is written to the console.Line 4: The
if
statement is closed with the curly brace.
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]
>