[Csnd] if statement question

Hey there,
i've a problem with the if statement, can anybody give me an insight, please?
Thanks, cheers!
Enrico

<CsoundSynthesizer>

<CsOptions>
-odac
</CsOptions>

<CsInstruments>

instr 1

ktrig init 0

  if (ktrig != 0) then
  schedule 2, 0, 1
  endif

endin

instr 2
prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
endin

</CsInstruments>

<CsScore>

i 1 0 1000
</CsScore>

</CsoundSynthesizer>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

schedule is an i-time opcode. It will be triggered at i-time even if it is inside a k-rate loop. Use one of the k-rate opcodes instead.

The "schedule" opcode is an i-time opcode and your k-time if statement
does not affect it.

In more detail: Csound code is not actually just a single program, it is
three different programs whose statements can be mixed together in the
file. Some statements run at i-time (once when the note starts); some at
k-time (once per control cycle); and some at a-time (which functionally
means once per audio sample, though the actual implementation may fake
this by processing multiple samples at once). These three programs are
separated out and run separately at the appropriate times. You can
confuse yourself by writing k-time statements in the middle of i-time
code, but that won't change their relative order of execution.

So your code is really saying:

; AT I-TIME

ktrig init 0
schedule 2, 0, 1

; AT K-TIME

if (ktrig != 0) then
  ; nothing here!
endif

I think if you put a k-time opcode like "event" inside the k-time if
statement instead of "schedule", you'll find that it does get affected by
the conditional. But you'll need to add a bit of further logic (resetting
a k-time variable as in the manual's example for "event") to prevent it
from triggering a new event on every single k-cycle.

I see, thanks!
scheduled fixed it!!!
kind regards
E

typo, i meant schedulek

thanks again
E