Skip to main content

Control Statements

Boolean Operators

x > y

Greater than.

x < y

Less than.

x >= y

Greater than or equal.

x <= y

Less than or equal.

x = y

Equal.

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.

Remarks

  • 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.

continue

while (<condition>)

    if (<condition>)

        continue

    end if

    ...

end while

Continue statement. Skips the rest of the current iteration if the condition is met.

Remarks

  • continue statement can only be used inside a while loop.

Examples

on init
    declare $i
    declare $skipped

    $i := 0
    while ($i < 5)
      inc($i)

      if ($i <= 3)
        continue
      end if

      inc($skipped)
    end while
  
    message($i & ", " & $skipped)
end on

Skipping first three iterations of the while loop. The message will print 5, 2.

See Also

while ()

if ... else ... end if

if

    <statements>

else

    <statements>

end if

Conditional if statement. else branch is not required.

Examples

on controller
    if (in_range($CC_NUM, 0, 127))
        message("CC Number: " & $CC_NUM & " - Value: " & %CC[$CC_NUM])
    else
        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 <constant>

        <statements>

    case <constant> to <constant>

        <statements>

end select

Select-case statement.

Remarks

  • This 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.

Examples

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>)

    <statements>

    <increment, decrement or wait>

end while

While loop.

Remarks

  • If a while loop does not contain any counter or a wait()/wait_ticks() command, it will be stopped and exited after 10 million iterations.

Examples

on note
    ignore_event($EVENT_ID)

    while ($NOTE_HELD = 1)
        play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, $DURATION_QUARTER / 2)
        wait($DURATION_QUARTER)
    end while
end on

Repeating held notes at the rate of one quarter note.

See Also

wait()

wait_ticks()

Events and MIDI: $NOTE_HELD