Skip to content

switch

Branches conditionally at initialization or during performance time.

Syntax

switch expr
case case-const
...
case case-const
...
default
...
endsw

where expr is evaluated at init or perf-time and if its result is matched by a case-const, then the code between this case and the next is executed. The default section is executed if no case is selected.

Examples

Here is an example of switch. It uses the file switch.csd.

Examples of switch-case expressions.
<CsoundSynthesizer>
<CsInstruments>

// accepts p-fields
instr 1
  switch p4
    case 1
      prints "pass\n"
    default
     prints "fail\n"
  endsw
endin

// accepts expression
instr 2
  switch 1 + 1
    case 2
      prints "pass\n"
    default
     prints "fail\n"
  endsw
endin

// accepts DRY multi match cases
instr 3
  switch 3
    case 0, 2
      prints "fail\n"
    case 1, 3
      prints "pass\n"
    default
     prints "fail\n"
  endsw
endin

// accepts expression in case
instr 4
  switch 3
    case 1 + 2
      prints "pass\n"
    default
     prints "fail\n"
  endsw
endin

// operates on performance rate
instr 5
  kl = line(0, p3 - 1/kr, 1)
  switch int(kl)
    case 1
      printks2 "pass %d\n", kl
  endsw
endin

</CsInstruments>
<CsScore>
i 1 0 0 1
i 2 0 0
i 3 0 0
i 4 0 0
i 5 0 0.1
</CsScore>
</CsoundSynthesizer>

See also

Program Flow Control

Credits

Author: Hlodver Sigurdsson New in Csound 7