While Loop Question


#1

Can anyone please help me understand the working of a While loop? Please review the two screen captures below. By including a list as an argument for a function, the return of the functions changes, although the list input is not used anywhere in the algorithm, either in the While loop, or the Imperative block.

Thanks.

While01 While02


#2

Hi Aseem,

I believe it’s because in the first case it iterates through only a single input. In the second case it iterates through a list as well(multiple inputs), and thus gives you the result for every item in the list.( i.e. every “in” has an “out”, even if you’re not using it anywhere it in the code.) Hope that clears it up.

2015-01-07_112553


#3

Dimitar,

  1. Thank you very much for your response. I am however not able to click on the image you attached to enlarge it to read your graph. Do you think may be you can re-attach so it is clickable? If not, maybe email it directly to me at adeshpande@eskewdumezripple.com

  2. The real reason I am trying to pass multiple arguments to a function is that seems to be the only way to pass a variable into an imperative block - by nesting the imperative block into an function. All I am trying to do is to use the While loop, and the For loop in a meaningful fashion in my code on data that is generated outside of the imperative block. Do you have any suggestions for doing that?

Thanks again.

 

 


#4

The forum misbehaves sometimes. Here’s the image again:

2015-01-07_112553

 

On your second point, I believe that your approach is correct. You will need to create a definition with defined variables, so if you’re feeding a list of inputs, it might help if you use

“def Function(input:var[])” to tell the code block to iterate through the whole list instead through each member of the list individually.

I had some difficulties understanding the loop while approach in Dynamo as well. Michael Kirschner helped me with that here:

http://dynamobim.com/forums/topic/the-misterious-loopwhile-node/

 

Good luck!


#5

Thank you Dimitar!


#6

Dimitar,

Can you please give me an example of what you said -

"You will need to create a definition with defined variables, so if you’re feeding a list of inputs, it might help if you use

“def Function(input:var[])” to tell the code block to iterate through the whole list instead through each member of the list individually."

 


I am having trouble getting that to work.

Thanks.


#7

I’ll try to explain it with a quick example. It is a pretty basic function with 2 variables. I’ve repeated it 4 times and in each case the only difference is the definition of the input variables.

So in the first case the function knows to expect two lists. Everything works as expected. The while function evaluates the first list. The first list is longer and that is why the last index of the result is null - because the second list doesn’t have a corresponding index.

In the second case neither of the input parameters are defined. Therefore each element from your input lists is evaluated on its own and because it’s a non-list single element, you get null values when you try to index it.

In the last two cases either one of the two input parameters is defined as a list. The function fails in both cases because you are trying to index a non-list single element again. However it’s interesting to note that the output is formatted differently because your input parameters are not the same length and that affects the lacing of the output.

Capture

I hope I didn’t confuse you more. Maybe someone will come up with a better example than this.

 


#8

Dimitar,

That was extremely helpful! That made complete sense, and I have got my graph working that way I wanted.

I appreciate you taking the time to put that example together for me.