# Conditions and Functions in Code Block

**URL:** <https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363>\
**Category:** DesignScript\
**Created:** [May 22, 2014, 12:03am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363 "2014-05-22T00:03:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [May 22, 2014, 12:03am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/1 "2014-05-22T00:03:11Z")

</div>

Conditions and Functions created in the **Code Block** aren’t red flagged, but the output is always _‘null’_.

Same is true with some of the Imperative code that I pasted from the Design Script manual.

Some pointers or advice for beginners on what not to try at this stage?

Currently using 0.7.0.26262

Thanks.

Regards,

Vikram Subbaiah

---

<div class="post-metadata">

**Author:** ![Eduardo\_P\_Roca](https://avatars.discourse-cdn.com/v4/letter/e/d6d6ee/32.png) [@Eduardo\_P\_Roca](https://forum.dynamobim.com/u/Eduardo_P_Roca)\
**Post date:** [May 22, 2014, 1:30am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/2 "2014-05-22T01:30:35Z")

</div>

I am using extensively functions in 0.7. Take into account:

1.Imperative code seems to have issues . Loops are not working at all for example (at least the last time i checked). I would avoid it by the moment.

2.There have been some changues in the lenguaje. And some of then are related to quite usual tasks. If you have experience before and you take a look to the library in 0.7, you will figure out quite fast what is going on. But if not, if you are starting to learn now using the manual for autocad DS, it can be very confusing. I suppose that we will have a new updated manual soon.

1. About conditions, as I said in 1, better to avoid imperative code. No issues until now with conditions in associative code.

I would recommend you try first with a very simple function. Maybe you can post here a specific sample that is giving you trouble.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [May 22, 2014, 4:27am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/3 "2014-05-22T04:27:56Z")

</div>

Thanks Eduarado!

Conditional statements (If-Else, while) do not seem to work

![20140522-1](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/1X/9e689f1f7441c6c4e2017ba2fc03ec8dd725cebc.jpg)

However, what works is something in the format …

s = (a == b ? “Are equal” : “Are not equal”);

I’m a beginner, but, the Design Script manual still seems like a decent place to start. Some of the changes in the language, I figure out by referring to the names of the nodes while some others with a little imagination (BSplines now seem to be Nurbs) and then there are some I just can’t figure out. But since I’m presently just exploring the language, it’s alright.

&nbsp;

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [May 22, 2014, 4:32am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/4 "2014-05-22T04:32:30Z")

</div>

Functions work (when not Imperative)

I was probably doing something wrong earlier

![20140522-2](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/1X/8c113a96dd1b6d93bfc3a77a14c316df3eba49d8.jpg)

---

<div class="post-metadata">

**Author:** ![Eduardo\_P\_Roca](https://avatars.discourse-cdn.com/v4/letter/e/d6d6ee/32.png) [@Eduardo\_P\_Roca](https://forum.dynamobim.com/u/Eduardo_P_Roca)\
**Post date:** [May 22, 2014, 5:20am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/5 "2014-05-22T05:20:40Z")

</div>

If, else and while are from imperative code.

(condition) ? (what happens if true) : (what happen in false) ; …is asociative code . Associative code is just the normal code you can use directly in CBN (imperative you have to start with var=[Imperative] and put the imperative block between {} ).

For example, You have a curve (Cu) and you want to create a Point in the middle only when its length is bigger than D. If not, a point in one third of the distance

NewPoint=Cu.Length\>D ? Cu.PointAtParameter(0.5): Cu.PointAtParameter(1/3) ;

This sample points one of the difference between AutocadDS and Dynamo

Autocad DS—Points (and Planes and Coordinate systems) related to curves or surfaces ---- “create” methods , in this case

Point.AtParameter (Cu,0.5)

Dynamo —Points (and Planes and Coordinate systems) related to curves or surfaces —“action” methods, in this case

Cu.PointAtParameter(0.5)

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [May 22, 2014, 5:43am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/6 "2014-05-22T05:43:14Z")

</div>

There was a mistake in the code I pasted earlier.

Below is the corrected version

![20140522-2](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/1X/88ce58733444aba12dee06e2e05dac39209b6a7b.jpg)

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [May 22, 2014, 5:46am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/7 "2014-05-22T05:46:46Z")

</div>

Thanks Eduarado.

I’ll stay away from exploring Imperative code for now.

---

<div class="post-metadata">

**Author:** ![Eduardo\_P\_Roca](https://avatars.discourse-cdn.com/v4/letter/e/d6d6ee/32.png) [@Eduardo\_P\_Roca](https://forum.dynamobim.com/u/Eduardo_P_Roca)\
**Post date:** [May 22, 2014, 6:40am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/8 "2014-05-22T06:40:33Z")

</div>

Yep, by the moment it is better to avoid to use imperative code at all

Anyway, for simple conditional statements, normal associative code is far more direct and convenient to use.

Regards,

Eduardo

---

<div class="post-metadata">

**Author:** ![Eduardo\_P\_Roca](https://avatars.discourse-cdn.com/v4/letter/e/d6d6ee/32.png) [@Eduardo\_P\_Roca](https://forum.dynamobim.com/u/Eduardo_P_Roca)\
**Post date:** [July 2, 2014, 2:02am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/9 "2014-07-02T02:02:41Z")

</div>

It seems that imperative code is working now (use last 0.7.1 builds)  
 ![01](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/1X/536a23d405b28e1f381308742c5c76d8986c1ced.jpg)

The main use of imperative code is loops and conditionals with complex definition of what to do in every option

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [July 7, 2014, 10:50am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/10 "2014-07-07T10:50:10Z")

</div>

Thanks for the update Eduardo.

---

<div class="post-metadata">

**Author:** ![Monik\_Prabhu](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@Monik\_Prabhu](https://forum.dynamobim.com/u/Monik_Prabhu)\
**Post date:** [July 10, 2014, 6:30am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/11 "2014-07-10T06:30:40Z")

</div>

Some cases in Imperative we have fixed in recent builds - try the following with latest daily builds.

_if you trying conditional - you can try inline conditional -_

**a=5;**  
**b=a==5?1:2;**

OR

**b=[Imperative]**  
**{**  
**a=1;**  
**if(true)**  
**{**  
**return =2;**  
**}**  
**else**  
**{**  
**return=3;**  
**}**  
**};**

_e.g. for while_

**c=[Imperative]**  
**{**  
**i=0;**  
**b;**  
**while(i\<2)**  
**{**  
**b=i+10;**  
**i=i+1;**  
**}**  
**return=b;**  
**};**

E.g. for loop

**c=[Imperative]**  
**{**  
**a={1,2,3,4,5};**  
**sum=0;**  
**for (i in a)**  
**{**  
**sum=sum+i;**  
**}**  
**return=sum;**  
**};**

This is still in testing stages. So give it a try !

Monika

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [July 10, 2014, 9:16am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/12 "2014-07-10T09:16:53Z")

</div>

Is there a way to reference within, variables and collections declared outside the Imperative block?

Doesn’t feel right, but I am currently pasting copies of the same collection within multiple Imperative code blocks.

Thanks.

Regards,

Vikram Subbaiah

---

<div class="post-metadata">

**Author:** ![Eduardo\_P\_Roca](https://avatars.discourse-cdn.com/v4/letter/e/d6d6ee/32.png) [@Eduardo\_P\_Roca](https://forum.dynamobim.com/u/Eduardo_P_Roca)\
**Post date:** [July 11, 2014, 3:03am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/13 "2014-07-11T03:03:03Z")

</div>

I am not 100% sure. But I think it is bug. I just posted this problem in github

> <https://github.com/DynamoDS/Dynamo/issues/1936>
>
> It is great that the imperative code is working now
> But It seems that except it is inside of a function, it...

---

<div class="post-metadata">

**Author:** ![Ritesh\_Chandawar](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@Ritesh\_Chandawar](https://forum.dynamobim.com/u/Ritesh_Chandawar)\
**Post date:** [July 11, 2014, 5:02am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/14 "2014-07-11T05:02:40Z")

</div>

Hello [Vikram Subbaiah](http://dynamobim.org/forums/users/testingwaters/ "View Vikram Subbaiah's profile"),

\<span style=“line-height: 1.5em;”\>We are working on the fix and will get back to you once it is available in Daily Build.\</span\>

Keep using Dynamo!

Thanks,

Ritesh

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [July 14, 2014, 10:58am UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/15 "2014-07-14T10:58:16Z")

</div>

Thanks Ritesh!

I have been actively working on designing with Design Script over the last few weeks and at the moment it seems like it’s potential is only limited by my ignorance of various utilities and features that probably already exist.

However, it is inconsistent. Geometry doesn’t generate sometimes (The code block turns yellow with warning), but on restarting Dynamo, the same code works.

Regards,

Vikram Subbaiah

&nbsp;

---

<div class="post-metadata">

**Author:** ![Thomas\_Trinelle](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_trinelle/32/10258_2.png) [@Thomas\_Trinelle](https://forum.dynamobim.com/u/Thomas_Trinelle)\
**Post date:** [August 6, 2015, 6:10pm UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/16 "2015-08-06T18:10:25Z")

</div>

Hi all,

Sorry for reviving an old burried thread but I have been trying to do a If-ElseIf-Else Statement in a Design Script format without using a Imperative Code. Is that now feasible in the current version on Dynamo ? If yes can someone post an example, I haven’t been able to find the right syntax.

Apologies for my ignorance… Been using this for a few days.

Cheers,

Thomas

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [August 6, 2015, 8:30pm UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/17 "2015-08-06T20:30:29Z")

</div>

The associative DS syntax for the IF function is: _test ? true\_value : false\_value_

If you want to simulate an IF - ElseIf -Else functionality, you’ll need to nest one if function inside the other. Another nice approach is to use the “Formula” node to do the same:

![2015-08-07_09-57-03](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex022/uploads/dynamobim/original/2X/c/ccf5ec9c31d4060e1a2c67ea68adeddc1842d684.png)

---

<div class="post-metadata">

**Author:** ![Thomas\_Trinelle](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_trinelle/32/10258_2.png) [@Thomas\_Trinelle](https://forum.dynamobim.com/u/Thomas_Trinelle)\
**Post date:** [August 7, 2015, 12:13pm UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/18 "2015-08-07T12:13:09Z")

</div>

Hi Dimitar,

Thanks for your answer ! You’re crazy fast at answering these posts, pretty impressive…

I had seen the “Formula” node but I don’t find it super convenient as you cannot add several lines to it, like a Code Block. I had notice that it worked a bit like a Excel If Statement were you could nest Ifs inside an If statement. Didn’t realize you could do the same with Design Script.

Thanks !

---

<div class="post-metadata">

**Author:** ![Nick\_Curtis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_curtis/32/24343_2.png) [@Nick\_Curtis](https://forum.dynamobim.com/u/Nick_Curtis)\
**Post date:** [September 26, 2015, 2:12pm UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/19 "2015-09-26T14:12:07Z")

</div>

Hey guys, did we ever reach resolution on Vikram’s inquiry?:

“Is there a way to reference within, variables and collections declared outside the Imperative block?” And please pardon my limited knowledge of design script at this point (I don’t believe I’ve written an “Imperitive” block yet, but maybe this is the same question:

Is it possible to reference variables and/or collections declared outside a typical code block defined function?:

 ![outside_reference](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/1/1e7a304c67024a979b2e304db7aab49d3a526f4e.jpg)

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Nick\_Curtis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_curtis/32/24343_2.png) [@Nick\_Curtis](https://forum.dynamobim.com/u/Nick_Curtis)\
**Post date:** [September 26, 2015, 2:15pm UTC](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363/20 "2015-09-26T14:15:34Z")

</div>

Better picture:

&nbsp;

 ![outside_reference](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/1/1e7a304c67024a979b2e304db7aab49d3a526f4e.jpg)

[Next page](https://forum.dynamobim.com/t/conditions-and-functions-in-code-block/363.md?page=2)
