Obsidian | Level 7

## help with if and or statements

Hi.

I have the following code and clearly I am not doing something right.

The value of Rule3 is return the value of "No", therefore I want the value of "`RegOff" to be "No", but if the value of Rule3 is "Yes", then apply the and plus or statements.`

`Sorry if this is an easy one, but perhaps I've been looking at it too long and my head is going in circles.`

``````	if 'INT Type'n =1  and RGO=1 or IDF=1 then Rule3="Yes";
else Rule3="No";
if Rule3="Yes" and ADM >0 or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0 Then RegOff="Yes";
else RegOff="No";``````

Cheers

Haydn

4 REPLIES 4
Super User

## Re: help with if and or statements

IF is a statement, OR is an operator.

In logic, AND takes precedence over OR. Therefore your conditions are equivalent to this:

``````if ('INT Type'n =1  and RGO=1) or IDF=1 then Rule3="Yes";
if (Rule3="Yes" and ADM >0) or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0 then RegOff="Yes";``````
Obsidian | Level 7

## Re: help with if and or statements

Hi Kurt,

Thanks for your quick response. I copied your code and Rule 3 is blank (as desired), however RegOff is returning the value of "Yes". But given Rule 3 does not have the value of "Yes", it should not.
Super User

## Re: help with if and or statements

My examples are only there to illustrate what your conditions as posted really do.

It is now up to you to set the parentheses in a way so that the code reflects what you want to achieve.

If all the ORs in a compound condition should only play a role if the first condition is met, you need to do

`if a and (x or y or z)`

Review both of your IF statements in light of this.

Super User

## Re: help with if and or statements

Hint: if you have many numeric variables and you want to find if any (one or more) of them have a value greater than 0 you can reduce code like this (Assuming the intent is to use all of them to get one logical result)

` ADM >0 or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0`

shorter:

`max( ADM, ALC, CAR, CDM, MPR, NCP, PTR, DMA, DMC, IDD)>0`

You would have to provide values of the variables, the incorrect result and the expected result for the values to determine the actual issues in your comparison approach.

Discussion stats
• 4 replies
• 176 views
• 2 likes
• 3 in conversation