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

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

This doesn’t look right:

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

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

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…

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