Nested Conditionals (If Else)


Author
Message
Kolfers
Kolfers
Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)
Group: Forum Members
Posts: 24, Visits: 137
Hi! 

I've been scratching my head a bit since I couldn't figure out why my code wasn't doing what I expected. 
After a bit of testing it seems to me that embedding IF conditionals within and IF-THEN-ELSE conditional leads to unwanted results, that is, the ELSE statement is not evaluated, could you confirm this? 

For instance, this works fine (value.g is set to 255)
 if(0) {values.r = 0; values.g = 0; values.b = 255}  else {values.r = 0; values.g = 255, values.b = 0;};

However, this doesn't work (value.g is never set)
 if(0) { if(1) {values.r = 0; values.g = 0; values.b = 255}; }  else {values.r = 0; values.g = 255, values.b = 0;};

To be clear, the code doesn't crash / break or give warnings, it's just that the else expression is never evaluated. 

Is there anything that I might be missing here? I tried different semicolon placements, but it didn't seem to make a difference.

Thanks,

Kerwin




Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 13K, Visits: 103K
The expressions parser doesn't particularly like nested conditionals along those lines, yes. It will fail to fully parse the expression when stated as you did and will not ever "see" the relevant else. You can force the parser into submission by restating to e.g.

<values>
/ r = -1
/ g = -1
/ b = -1
/ c = false
</values>

<trial set1>
/ ontrialbegin = [if(false) {values.r = 0; values.g = 0; values.b = 255} else
    {values.r = 0; values.g = 255; values.b = 0};
    ]
/ ontrialend = [values.r = 0; values.g = 0; values.b = 0]
/ stimulusframes = [1=mytext]
/ validresponse = (57)
</trial>

<trial set2>
/ ontrialbegin = [if(false) {if(true){values.r=0; values.g=0; values.b=255} else 0;} else
    {values.r = 0; values.g = 255; values.b = 0};
    ]

/ ontrialend = [values.r = 0; values.g = 0; values.b = 0]
/ stimulusframes = [1=mytext]
/ validresponse = (57)
</trial>

<text mytext>
/ items = ("R=<%values.r%>|G=<%values.g%>|B=<%values.b%>")
</text>

<block myblock>
/ trials = [1=set1; 2=set2]
</block>

In most cases, though, I would argue that there's a different and perhaps "better" way to express whatever you want to do (e.g. using if - else if - else constructs or collapsing the conditionals). Hope this helps.

Kolfers
Kolfers
Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)Distinguished Member (3.6K reputation)
Group: Forum Members
Posts: 24, Visits: 137
Thanks for the reply! 

I agree that most things can be rewritten (although sometimes it means a sizeable increase in code length), I just needed to be sure that this was indeed the problem. 

The else 0 option would provide the quickest workaround for now, thanks! 

cheers,

Kerwin
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Reading This Topic

Explore
Messages
Mentions
Search