If - else statement , while loop , Imperative block

Hi all,

I’m struggeling with a code block. Today I’m realy starting on formula’s inside code blocks.
And I’ve already created me baseline formula’s to calculate some value’s.
However now I want to use the if and else statements but the code block just say’s “error” and is red.

This is what I have so far:

Code block 1:
l = 500
r = 8750
hoh = 150

Code block 2:
z = l * 0.5;
Verdeling = Math.Round((Math.Sqrt(Math.Pow(r,2) - Math.Pow(z,2))) / hoh);
Rest = (Math.Sqrt(Math.Pow(r,2) - Math.Pow(z,2))) - (Verdeling * hoh);

if (Rest + (hoh * 0.5) < hoh)
{
Ystart1 = hoh * 0.5;
};
else
{
Ystart1 = 0;
};

And before the If statement the node worked so it must be a notation error in the if and else statements.
Isn’t this the standard notation:

if (true) {do this};
else {do the other thing};

Can someone point out my error?

2 Likes

Hi @Mike_Wellink ,

if statements can only be used in imperative blocks. Here’s an example:

9 Likes

Hi @Mostafa_El_Ayoubi,

Thanks for helping me out. It works! Yes! :smile:
Think I’m somewhat confused because I had a Python for Dynamo course last month from Autodesk and the notation in Python is different from the code block notation.

Onwards with the next peice of my puzzle!

Yes it’s sometimes confusing… I find DesignScript way less user friendly than python. Good luck to you !

1 Like

Hi @Mostafa_El_Ayoubi,

do you by any change know that the same notation apply’s to the while statement?
When I google I see alot notation that start with “def something(in0, in1)”.

Do I have to define a function first or can I just start with the while loop after setting the value’s first?

Now I have this based on the previous code block node:


StartY;
hoh;
r_rest;
r;

return = [Imperative}
{
while (StartY < r_rest)
{
x = r - (Math.Sqrt(Math.Pow(r,2) - Math.Pow(y,2));
y = StartY + hoh;
}
};


Input being:
StartY = 75
hoh = 150
r_rest = 8746,42
r = 8750

In Python it would be somthing like:
y = StartY
xList = []
While y < r_rest
x = r - √r² - y²
xList.append(x)
y = y + hoh
OUT = xList

But how is this in design script?

Okay, been modifying it a bit after some more google searching.
and got the error sorted…but it needs an y-input??

what’s wrong?

1 Like

This is how i’d do it:

def func(y,hoh,r_rest,r)
{
xlist = {};
i = 0;
return = [Imperative]
	{
	while (y < r_rest)
		{
		xlist[i] = r - Math.Sqrt(Math.Pow(r,2) - Math.Pow(y,2));
		y = y + hoh;
		i = i+1;
		}
	return = xlist;
	}
};

you can also do it without defining a function :

6 Likes

AHA!!!
Thank @Mostafa_El_Ayoubi

So in between the () of the def func I need to put the necessary values!
And when I don’t use the def function I still have to create an empty list.

But I don’t get the part : i = 0
Why is that?

Can’t you also do it like this?

2 Likes

yes you must define the inputs of your function.

I use the “i” variable so I can append values in my output list at each iteration. I initiate with i = 0 so in the first iteration I place a value at the index 0 (xlist[0]) , then I increment i by 1 (i = i+1). At the next iteration, the value is placed at xlist[1] , and so on… I hope that was clear :smile:

1 Like

Thanks @jostein_olsen,

That could work to! I’m just decrypting your setup. :grin:
Your creating a list with value’s that you feed in the formula.
So that the formula is calculating over and over all the items in the list.

Yeah…the outcome is the same. Thanks!

none the less…for me it still is good to know how the loops (while and if) work in design script (for future use).

This stuff is realy tuff but so much FUN when it works!!! :smile:

1 Like

I’m still learning and the more I use it, the more I can understand it. :grin:

Hi @Mostafa_El_Ayoubi,

when I recreate your last picture Dynamo crashes! And I get no list!
I’ve checked for type error’s and let a colleague check it to…no difference.
And yet I don’t get a result.

y = 75;
hoh = 150;
r_rest = 8746.42;
r = 8750;
d = [Imperative]
	{xlist = {};
	i = 0;
	while (y < r_rest)
		{
		xlist[i] = r - Math.Sqrt(Math.Pow(r,2) - Math.Pow(y,2));
		y = y + hoh;
		i = i+1;
		}
	return = xlist;
	};

here’s the code just to make sure we have the exact same thing. What version of dynamo/revit are you using?

For this I’m using Dynamo Studio 1.2.1.3083.
And I copyed and pasted your code and I get the same thing!

And I just opend it in Dynamo in Revit 2017…and it works???
What The…!!!
Could this be a bug??

I never used dynamo studio… Do you get the same with dynamo sandbox?

Dynamo Sandbox??
Is that a stand alone program??
I don’t know that one.

If you used the default install folder you’ll find it here :
C:\Program Files\Dynamo\Dynamo Revit\1.2

In Dynamo Sandbox I do get a list!
Is this Dynamo Sandbox the standard Dynamo without packages?
And can it run outside Revit?

yes Dynamo sandbox is standalone. You can also download packages butl calls to revit API will fail. Your problem seems to have something to do with dynamo Studio. I wish I could help but I don’t have it. You should start another topic.