IF Statement AND/OR

Hi All,

Long time Revit user, however I’ve only just dipped my toe into the wonderful world of dynamo…

Could anybody enlighten me into how I could use the type of formula below (from excel) in dynamo?

=IF(AND(A1=1,B1=2),1,0)

I’ve tried various things in a codeblock, ‘formula’ node and the standard IF node… but no luck!

Much appreciated!!

image
As you can see the If statement Dynamo itself offers isn’t exactly simple.
I’ll check for you if I can create a more elegant solution with a python script.

2 Likes

Hi Paul,

Thanks for the prompt response!
If the result is false, is there a way to return it into another If statement?
e.g.

=IF(AND(A1=1,B1=2),1,(IF(AND(A1=3,B1=4),2,“AND SO ON”)))

1 Like

You can, you should add a similar if node to the false input of the other if node.
You’d have to think somewhat backwards from what you’d normally do when programming.

I’d look up how to put it in a code block or added python script, which would probably be a whole lot cleaner and actually simpler. But I have to admit that my knowledge of that is pretty terrible.

If statements in designscript are fairly straightforward.

Test ? True: False;

Where
Test is a comparison returning a true/false.
True is the desired result if the Boolean test returns true.
False is the result if the Boolean test returns false.
The ? marks the end of the test.
The : marks the end of the true.
The ; ends the line of code.

An example:
Math.Ceiling(a/2) == A/2?
“This is a even number”:
“This is an odd number”;

Feed in 2 and you’ll divide 2 by 2 and round up to 1, which will equal to 2 divided by two, so your output will be “this is an even number”. Feed in 3 and the math.ceiling function will round 3/2 up to 2 which isn’t equal to 3/2, so your result will return “This is an
odd number”.

You can nest these, but make sure you have the correct format. This gets a bit tough to do from memory, but this should give you an idea to play with:

Test1 ?
Test2OnlyIfTest1True ?
Test2True :
Test2False :
Test3IfTest1False ?
Test3True :
Test3False ;

5 Likes

Condition
A1==1 && B1==2 ? 1 : (A1==3 && B1==4 ? 2 : 3);

17 Likes

just to be pedantic - this is not an if statement - it’s use of a conditional ternary operator (3 inputs) … I am a stickler for this kind of diction because it is helpful to use common terms to programming languages when moving into other languages or domains.

9 Likes

@Michael_Kirschner2 excellent points.

We should have a vocab lesson sometime and do a write up in a post. ‘Programming terms demystified’ would be very useful for the many new users being attracted as a result of the easy access provided by Dynamo - you do the teaching and I will do any back end heavy lifting I can. :smile:

11 Likes

The post heading refers to AND/OR. There only seems to be explanation on how to use &&. For those like me who were searching for OR: ||

Hello Connor,

You may want to check out my DesignScript Presentation from BILT Europe last year - as it contains a lot of this stuff!

This one in particular may be of note:

24 Likes