# Translate 'while' loop to 'for' loop

**URL:** <https://forum.kirupa.com/t/translate-while-loop-to-for-loop/57584>\
**Category:** Uncategorized\
**Created:** [July 14, 2004, 2:05pm UTC](https://forum.kirupa.com/t/translate-while-loop-to-for-loop/57584 "2004-07-14T14:05:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fumeng](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@fumeng](https://forum.kirupa.com/u/fumeng)\
**Post date:** [July 14, 2004, 2:05pm UTC](https://forum.kirupa.com/t/translate-while-loop-to-for-loop/57584/1 "2004-07-14T14:05:07Z")

</div>

hi.

i’m create a grid-like structure and am trying to translate my old loop:

```auto

while (dayCount <= lastDay) // creates 30 text fields

```

to a ‘for’ loop where I can increment the position of each duplicated text field by i \* a specified width.

the thing is that i can’t figure out how to do that with my while loop. but i can do it with a ‘for’ loop. that’s why i’m trying to change it.

earlier in my code i use this ‘for’ loop to lay out the row above where i want to start…

```auto

for(var i:Number = 0; i < numCols; i++){
     _root.createTextField("dayHeadings" + i, i, indColWidth*i, startSecRowYPos, indColWidth, colHeight);
     _root["dayHeadings"+i].border = true;
     _root["dayHeadings"+i].text = dayHeadings*;
}

```

anyone have any ideas?

thanks to anyone for helping out. fumeng.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 3:01pm UTC](https://forum.kirupa.com/t/translate-while-loop-to-for-loop/57584/2 "2004-07-14T15:01:50Z")

</div>

> [@fumeng](#):
>
> hi.
> 
> i’m create a grid-like structure and am trying to translate my old loop:
> 
> ```auto
> 
> while (dayCount <= lastDay) // creates 30 text fields
> 
> ```
> 
> to a ‘for’ loop where I can increment the position of each duplicated text field by i \* a specified width.
> 
> the thing is that i can’t figure out how to do that with my while loop. but i can do it with a ‘for’ loop. that’s why i’m trying to change it.
> 
> earlier in my code i use this ‘for’ loop to lay out the row above where i want to start…
> 
> ```auto
> 
> for(var i:Number = 0; i < numCols; i++){
> _root.createTextField("dayHeadings" + i, i, indColWidth*i, startSecRowYPos, indColWidth, colHeight);
> _root["dayHeadings"+i].border = true;
> _root["dayHeadings"+i].text = dayHeadings*;
> }
> 
> ```
> 
> anyone have any ideas?
> 
> thanks to anyone for helping out. fumeng.

Just initialize a counter outside the while loop and increment it inside. It’ll essentially be like a for-loop.

Or since (I’m assuming) dayCount is being incremented inside the loop, just use that value as your i through mathematical manipulation.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 3:05pm UTC](https://forum.kirupa.com/t/translate-while-loop-to-for-loop/57584/3 "2004-07-14T15:05:03Z")

</div>

i didn’t even think of that because i’m so used to using counter variables like i and j in loops. now i’m using:

```auto

for (var dayCount:Number=1; dayCount <= lastDay; dayCount++){
}

```

thanks for your help.

fumeng.
