The Alternative Csound Reference Manual
Previous |  | Next

# if

if -- Branches conditionally at initialization or during performance time.

## Description

if...igoto -- conditional branch at initialization time, depending on the truth value of the logical expression ia R ib. The branch is taken only if the result is true.

if...kgoto -- conditional branch during performance time, depending on the truth value of the logical expression ka R kb. The branch is taken only if the result is true.

if...goto -- combination of the above. Condition tested on every pass.

if...then -- allows the ability to specify conditional if/else/endif blocks. All if...then blocks must end with an endif statement. elseif and else statements are optional. Any number of elseif statements are allowed. Only one else statement may occur and it must be the last conditional statement before the endif statement. Nested if...then blocks are allowed.

| Note
| Note that if the condition uses a k-rate variable (for instance, "if kval > 0"), the if...goto or if...then statement will be ignored during the i-time pass. This allows for opcode initialization, even if the k-rate variable has already been assigned an appropriate value by an earlier init statement.

## Syntax

if ia R ib igoto label

if ka R kb kgoto label

if ia R ib goto label

if xa R xb then

where label is in the same instrument block and is not an expression, and where R is one of the Relational operators (<, =, <=, ==, !=) (and = for convenience, see also under Conditional Values).

## Examples

Here is an example of the if...igoto combination. It uses the files igoto.orc and igoto.sco.

Example 1. Example of the if...igoto combination.

    /* igoto.orc */
    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Get the value of the 4th p-field from the score.
      iparam = p4

      ; If iparam is 1 then play the high note.
      ; If not then play the low note.
      if (iparam == 1) igoto highnote
        igoto lownote

    highnote:
      ifreq = 880
      goto playit

    lownote:
      ifreq = 440
      goto playit

    playit:
      ; Print the values of iparam and ifreq.
      print iparam
      print ifreq

      a1 oscil 10000, ifreq, 1
      out a1
    endin
    /* igoto.orc */

    /* igoto.sco */
    ; Table #1: a simple sine wave.
    f 1 0 32768 10 1

    ; p4: 1 = high note, anything else = low note
    ; Play Instrument #1 for one second, a low note.
    i 1 0 1 0
    ; Play a Instrument #1 for one second, a high note.
    i 1 1 1 1
    e
    /* igoto.sco */

Its output should include lines like this:

    instr 1:  iparam = 0.000
    instr 1:  ifreq = 440.000
    instr 1:  iparam = 1.000
    instr 1:  ifreq = 880.000

Here is an example of the if...kgoto combination. It uses the files kgoto.orc and kgoto.sco.

Example 2. Example of the if...kgoto combination.

    /* kgoto.orc */
    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Change kval linearly from 0 to 2 over
      ; the period set by the third p-field.
      kval line 0, p3, 2

      ; If kval is greater than or equal to 1 then play the high note.
      ; If not then play the low note.
      if (kval >= 1) kgoto highnote
        kgoto lownote

    highnote:
      kfreq = 880
      goto playit

    lownote:
      kfreq = 440
      goto playit

    playit:
      ; Print the values of kval and kfreq.
      printks "kval = %f, kfreq = %f\\n", 1, kval, kfreq

      a1 oscil 10000, kfreq, 1
      out a1
    endin
    /* kgoto.orc */

    /* kgoto.sco */
    ; Table #1: a simple sine wave.
    f 1 0 32768 10 1

    ; Play Instrument #1 for two seconds.
    i 1 0 2
    e
    /* kgoto.sco */

Its output should include lines like this:

    kval = 0.000000, kfreq = 440.000000
    kval = 0.999732, kfreq = 440.000000
    kval = 1.999639, kfreq = 880.000000

## Examples

Here is an example of the if...then combo. It uses the files ifthen.orc and ifthen.sco.

Example 3. Example of the if...then combo.

    /* ifthen.orc */
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Get the note value from the fourth p-field.
      knote = p4

      ; Does the user want a low note?
      if (knote == 0) then
        kcps = 220
      ; Does the user want a middle note?
      elseif (knote == 1) then
        kcps = 440
      ; Does the user want a high note?
      elseif (knote == 2) then
        kcps = 880
      endif

      ; Create the note.
      kamp init 25000
      ifn = 1
      a1 oscili kamp, kcps, ifn

      out a1
    endin
    /* ifthen.orc */

    /* ifthen.sco */
    ; Table #1, a sine wave.
    f 1 0 16384 10 1

    ; p4: 0=low note, 1=middle note, 2=high note.
    ; Play Instrument #1 for one second, low note.
    i 1 0 1 0
    ; Play Instrument #1 for one second, middle note.
    i 1 1 1 1
    ; Play Instrument #1 for one second, high note.
    i 1 2 1 2
    e
    /* ifthen.sco */

## See Also

elseif, else, endif, goto, igoto, kgoto, tigoto, timout

## Credits

Examples written by Kevin Conder.

Added a note by Jim Aikin.

February 2004. Added a note by Matt Ingalls.

Previous | Home | Next
iexprand | Up | igauss