Training for Whileloop

I am training to understand how does the “Loopwhile” node works and when can I use it.

The aim of the exercise is to get a list of a decreasing range of integers beginning by 16, where the following number is the previous divided by 2 until 1.

I am quite new in Dynamo and I just use it from time to time. I do not understand what I am doing wrong. I do leave a .jpg of the visual code. The green output is what I am trying to get and the orange output is what I actually get.

I am sorry but as a new user i cannot upload .dyn attachments.

I apologise if this topic has been discussed already, please kindly leave a link of reference.

Any help will be very welcome.

Thank you for your time and knowledge.

@CTV ,

check out here designScript or Python!


OUT =[]

while i < 6:
	i += 1
	OUT.append(i)

KR

Andreas

(Sorry for the wall of text.)

First off, loops in Dynamo aren’t typically necessary (for standard workflows) due to how Dynamo handles lists automatically. By default, Dynamo will iterate through a list and continue a function for the length of the list.

The LoopWhile node applies a function (loopBody) to an object until the continueWhile condition is false. Both the continueWhile and loopBody inputs need to be valid functions for the node to work. The node does not return the output of each iteration, only the final output once the condition fails. Trying to make this node work by adding to a list (and only running on the last item in the list) will not work.

When you do need a true loop functionality, python will almost always be better, faster, and easier, even with just a little bit of python experience. Most of the time however, if you have all the values needed to create the loop, you can just solve for the output directly - no loop needed.

In your case, as in most loops, you just need to define a sequence of values that represents the individual steps in the iteration from initial value to final value. This is the part that can usually be defined implicitly (rather than relying on the loop) when you have all the appropriate inputs and values. It just takes a little outside the box thinking. Dividing by two at each step is the opposite of multiplying by 2 in the other direction. Multiplying by the same value n (2) is just the creation of squares. The inverse of squares is the square root… so we start by taking square roots of our initial value. This tells us how many times we can halve our initial value, which in turn gives us our sequence for the multiplier /divisor at each step.

Hi @Draxl_Andreas;

thank you for your answer. I used w3schools.com in the past. Very Useful.
I have checked the DesignScriptGuide.pdf, I tried to write a while loop in code block without success.
I will further study it in the future.

a = 16 ;
b = ;
c =0;
[Imperative]
{
while (a > 1)
{
b[c]=a;
a = a/2;
c = c+1;
}
}
b;

In Python was easer.

dataEnteringNode = IN
InitialValue = IN[0]
List =
while InitialValue > 1:
List.append(InitialValue)
InitialValue=InitialValue/2
OUT = List

Hi @Nick_Boyts

Never mind. Thank you to make me understand how does a “LoopWhile” node in Dynamo works and how it have to be used.

As fas as I understand “LoopWhile” node it is used when a value that does not satisfy a certain condition is needed after processing some initial data through specific nodes. The initial data will change in each iteration of the process till the condition fails. The result will be the last change of the initial data when the condition was still indeed satisfied.

So, I looks like it is not suitable when the focus is on each iteration and not only the final one. And it looks like is better not to use lists as inputs in loops as it will iterates the same times that indexes the list has.

The true lesson here for me is:

“Most of the time however, if you have all the values needed to create the loop, you can just solve for the output directly - no loop needed.”

I will think more carefully when a loop or a sequence is needed.

And yes, definitely python is faster, easer and more powerful.

Thank you for your answer.