Skip to main content

Control Statements

if … else … end if

if … else … end if

Conditional if statement


on controller
    if (in_range($CC_NUM, 0, 127))
        message("CC Number: " & $CC_NUM & " - Value: " & %CC[$CC_NUM])
        if ($CC_NUM = $VCC_PITCH_BEND)
            message("Pitch Bend - Value: " & %CC[$CC_NUM])
        end if

        if ($CC_NUM = $VCC_MONO_AT)
            message("Channel Pressure - Value: " & %CC[$CC_NUM])
        end if
    end if
end on

Display different text depending on various MIDI controller messages

See Also

select ()

select ()

select (<variable>) … case x … end select

Select-case statement


  • The select statement is similar to the if statement, except that it has an arbitrary number of branches. The expression after the select keyword is evaluated and matched against individual case branches, the first case branch that matches is executed.

  • The case branches may consist of either a single constant number or a number range, expressed by the term "x to y").

  • While there is no else or default case branch in KSP, one can be achieved by using case 08000000H to 07FFFFFFFH, which covers the whole range of 32-bit signed integer values, effectively catching all cases that don't have literally specified branches.


on controller
    if ($CC_NUM = $VCC_PITCH_BEND)
        select (%CC[$VCC_PITCH_BEND])
            case -8191 to -1
                message("Pitch Bend down")
            case 0
                message("Pitch Bend center")
            case 1 to 8191
                message("Pitch Bend up")
            case 080000000H to 07FFFFFFH
                message("We're not sure how you got this Pitch Bend value!")
        end select
    end if
end on

Query the state of the pitch bend wheel

See Also

if … else … end if

while ()

while(<condition>) … end while

While loop


on note

    while ($NOTE_HELD = 1)
    end while
end on

Repeating held notes at the rate of one quarter note

See Also


Events and MIDI: $NOTE_HELD

Boolean Operators

x > y

Greater than

x < y

Less than

x >= y

Greater than or equal

x <= y

Less than or equal

x = y


x # y

Not equal

in_range(x, y, z)

True if x is between (and including) y and z

not a

True if a is false and vice versa

a and b

True if a is true and b is true

a or b

True if a is true or b is true

a xor b

True only if either a or b is true, but not both


  • Boolean operators are used in if and while statements, since they return if the condition is either true or false. In the list above. x, y and z denote numerals, a and b denote Boolean values.