# Error messages in UDO or Instr with nested if/then/else

**URL:** <https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023>\
**Category:** Newbies\
**Created:** [February 11, 2025, 11:10am UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023 "2025-02-11T11:10:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lez](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@Lez](https://forum.csound.com/u/Lez)\
**Post date:** [February 11, 2025, 11:10am UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/1 "2025-02-11T11:10:11Z")

</div>

This keeps me annoyed for some time now, because I don’t seem to be able to work out the problem.  
It would really help if there was some kind of ‘switch’ control like in C, C++, Swift, etc in the CSound language. It would make code like this a lot more readable.

I’ll add some sample code. Don’t bother about the contents, it’s about something I probably do wrong constructing this UDO. I also tried to use this construction inside an instrument, but I get the same error message for **endin**  
I checked this code a million times, but as far as I can detect there is nothing wrong with it. All if/then/else conditions are closed, no typing errors, but I get the error message:

**error: syntax error, unexpected UDOEND\_TOKEN (token “endop”) from file**

I work in CSoundQt, maybe it has something to do with that

opcode SetNewKey, 0, 0

iKeyDecision random 0, 4.999  
iKeyIntDec = int(iKeyDecision)  
iProgDecision random 0, 3.999 ;Randomly decide what ChordProgression to play  
iProgIntDec = int(iProgDecision)

;Decision structure to determine the giProgression chord order. Currently only 4 progressions are defined  
if iProgIntDec == 0 then  
giProgression = giMajMinMod1  
elseif iProgIntDec == 1 then  
giProgression = giMajMinMod2  
else if iProgIntDec == 2 then  
giProgression = giMajMinMod3  
else  
giProgression = giMajMinMod4  
endif

if giCurrentKey[1] == 0 then ;Major key modulation if CurrentKey is major (currently 6 possibilities are defined)  
if iKeyIntDec == 0 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[2]   
giGoalKey[1] = 1  
elseif iKeyIntDec == 1 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[4]  
giGoalKey[1] = 0  
elseif iKeyIntDec == 2 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[4]  
giGoalKey[1] = 1  
elseif iKeyIntDec == 3 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[6]  
giGoalKey[1] = 0  
elseif iKeyIntDec == 4 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[4]  
giGoalKey[1] = 0  
else ;if iKeyIntDec == 5 then  
giGoalKey[0] = giCurrentKey[0] + giMajorScale[7]  
giGoalKey[1] = 1  
endif  
endif   
if giCurrentKey[1] == 1 then ; In second case the CurrentKey Scale is minor  
if iKeyIntDec == 0 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[1]  
giGoalKey[1] = 0  
elseif iKeyIntDec == 1 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[2]  
giGoalKey[1] = 1  
elseif iKeyIntDec == 2 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[3]  
giGoalKey[1] = 0  
elseif iKeyIntDec == 3 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[4]  
giGoalKey[1] = 1  
elseif iKeyIntDec == 4 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[6]  
giGoalKey[1] = 0  
else ;if iKeyIntDec == 5 then  
giGoalKey[0] = giCurrentKey[0] + giMinorScale[7]  
giGoalKey[1] = 1  
endif  
endif  
endop

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 11, 2025, 11:45am UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/2 "2025-02-11T11:45:11Z")

</div>

This doesn’t look right:

> [@Lez](#):
>
> else if iProgIntDec == 2 then

---

<div class="post-metadata">

**Author:** ![Lez](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@Lez](https://forum.csound.com/u/Lez)\
**Post date:** [February 11, 2025, 1:56pm UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/3 "2025-02-11T13:56:34Z")

</div>

Thanks Rory,  
I have been concentrating on the second block of nested if/then/else and had probably a blind eye for the first part. This solved it.  
But it still is a pity that there isn’t a control function like “switch” in the CSound language. I code in other languages as well, it is a tool that I use a lot and it results in very readable code.

Thank you

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 11, 2025, 2:56pm UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/4 "2025-02-11T14:56:13Z")

</div>

I agree. `switch` would be nice. But in the meantime, you could probably benefit from formatting your code a little better 🙂 Those if/else blocks are hard enough to read with indentation, but are a complete nightmare without it.

---

<div class="post-metadata">

**Author:** ![Lez](https://forum.csound.com/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@Lez](https://forum.csound.com/u/Lez)\
**Post date:** [February 11, 2025, 4:34pm UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/5 "2025-02-11T16:34:22Z")

</div>

I’m sorry, but the layout got lost when I copied the code into the textfield. In the actual code everything is indented and every “if” statement has its own layout. For my next question I’ll pay a bit more attention to what it actually looks here…

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [February 12, 2025, 8:59am UTC](https://forum.csound.com/t/error-messages-in-udo-or-instr-with-nested-if-then-else/2023/6 "2025-02-12T08:59:25Z")

</div>

That makes sense. I’ll put my formatting police hat away now 🤣
