# While loop in codeblocks

**URL:** <https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573>\
**Category:** Uncategorized\
**Created:** [January 3, 2017, 2:23pm UTC](https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573 "2017-01-03T14:23:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali.safiaddine](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ali.safiaddine/32/17048_2.png) [@ali.safiaddine](https://forum.dynamobim.com/u/ali.safiaddine)\
**Post date:** [January 3, 2017, 2:23pm UTC](https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573/1 "2017-01-03T14:23:45Z")

</div>

Hi guys,

I am creating a script and I need a while loop in a code block  
I don’t know how to do that but I have a bit knowledge of coding so I reached the following loop;

![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/7/e70499e009597ee4f262b1d374606daed8076442.JPG)

Can anyone please help me with the syntax and the wrong phrases

Thank you

---

<div class="post-metadata">

**Author:** ![Thomas\_Mahon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_mahon/32/64209_2.png) [@Thomas\_Mahon](https://forum.dynamobim.com/u/Thomas_Mahon)\
**Post date:** [January 3, 2017, 2:34pm UTC](https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573/2 "2017-01-03T14:34:32Z")

</div>

Have you read the Dynamo DesignScript documentation: [Dynamo DesignScript Learning Resources](https://forum.dynamobim.com/t/dynamo-designscript-learning-resources/6359)

I would start there and create a really simple while loop then scale it up. Just reviewing your code (also, please post a copy of your code as you’ll get more answers):

1. `LI` instantiation appears to be wrong, use `LI={};`
2. `P=List.AddItemToEnd(P,LI)` is also wrong as you are assigning a list object to P (which is a Polycurve). You should be adding this to LI
3. Returning LI with nothing assigned to it will return…nothing!

---

<div class="post-metadata">

**Author:** ![ali.safiaddine](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ali.safiaddine/32/17048_2.png) [@ali.safiaddine](https://forum.dynamobim.com/u/ali.safiaddine)\
**Post date:** [January 3, 2017, 2:48pm UTC](https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573/3 "2017-01-03T14:48:43Z")

</div>

Thank you @Thomas_Mahon for replying 🙂

I had a look at the first pdf before , and the second one is very helpful thank you

I fixed these problems you mentioned but I still receive error in the codeblock

this is my script:

def ST(N,L,P1,P2,S,V1,V2)  
{  
return=[Imperative]  
{  
n=0;  
LI={};  
while(n\<N)  
{  
PT1=P1.Translate(V1,(L[n]-1)\*S);  
PT2=P1.Translate(V1,(L[n+1]-1)\*S);  
PT3=P2.Translate(V2,(L[n]-1)\*S);  
PT4=P2.Translate(V2,(L[n+1]-1)\*S);  
L1=Line.ByStartPointEndPoint(PT1,PT2);  
L2=Line.ByStartPointEndPoint(PT2,PT3);  
L3=Line.ByStartPointEndPoint(PT3,PT4);  
L4=Line.ByStartPointEndPoint(PT4,PT1);  
P=PolyCurve.ByJoinedCurves({L1,L2,L3,L4});  
LI=List.AddItemToEnd(P,LI);

n=n+2;  
}  
return=LI;  
}};

---

<div class="post-metadata">

**Author:** ![ali.safiaddine](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ali.safiaddine/32/17048_2.png) [@ali.safiaddine](https://forum.dynamobim.com/u/ali.safiaddine)\
**Post date:** [January 3, 2017, 3:24pm UTC](https://forum.dynamobim.com/t/while-loop-in-codeblocks/8573/4 "2017-01-03T15:24:20Z")

</div>

It seems that I cannot use L and an input parameter when I changed it to LL it worked perfectly but I don’t know why!!

Anyway thank you 🙂
