Or with and


(Moteb Marei) #1

Hi guys,
I want to use if with or and "and" fuctions like this:
if(not(selected(${nutrition.services1},'c.1')) and (not(selected(${nutrition.services1},'c.2'))),"0%",if((selected(${nutrition.services1},'c.1') or selected(${nutrition.services1},'c.2')) and (${calculate.nutrition.services1}=1),"10%",0)).
that logic works only when i select c.2 option c.1 option didn't work with me. is there anything to do?
i mean this :selected(${nutrition.services1},'c.2')) and (${calculate.nutrition.services1}=1) working. but this : selected(${nutrition.services1},'c.1')) and (${calculate.nutrition.services1}=1) didn't work


(A N M AL IMRAN (Imran)) #2

Hi,

Do not use if for the if statements on Xform, I don't think xform interprets conventional style of if statements, instead write as exp: selected(${a}, 'x') and (not(selected(${b}, 'y')) and not(selected(${b}, 'z)) and not(selected(${b}, 'w'))

Thanks


(Dr. Gareth S. Bestor) #3

Your function looks syntatically valid, but I'm not clear what you mean by "didnt work"... Could you perhaps give a couple examples of different combinations of values for nutrition.services1 and calculate.nutrition.services1, and what result you want in each case.

BTW, I might gently recommend not using periods '.' in your names, and use an underscore instead. Although valid, periods in XML element names is not advised:

Best Naming Practices
...
Avoid ".". If you name something "first.name", some software may think that "name" is a property of the object "first".


(jules rugwiro) #4

Hello @Moteb_Marei,

Based on your example, you are returning either 0% or 10%, not sure if this is just a display value or something you may use later e.g.: to calculate discount or so, either way, a single if statement would have been enough to solve the puzzle.

if( ( selected(${nutrition.services1},'c.1') or selected(${nutrition.services1},'c.2') ) and ${calculate.nutrition.services1}=1, "10%","0%" )

In case you need more if segments, then you could nest it with more arguments and maybe use text editors like Notepadd++ to quickly tell if you brackets are open or closed.

I strongly agree with @Xiphware, the use of "." may cause serious headache especially when exporting your data to other software.

Best regards,
Jules R