Page 1 of 1

"For" loop.

PostPosted: Fri Oct 23, 2009 11:48 am
by savask
Is there a "For" loop in Algodoo or Phun?
Like in Pascal or C.

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:01 pm
by KarateBrot
Yes :thumbup:
Code: Select all
for(n,(i)=>{  })

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:03 pm
by savask
Thank you!

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:11 pm
by KarateBrot
There's an example in the Algodoo or Phun folder. A text file that's called "thyme.txt" with an example:

Code: Select all
/* for - A simple looping structure in Thyme.
Used like this: for(n, (i)->{ ... }); where n is the number of times to call the function. i will be given the values 0, 1, ..., n-2, n-1
Example:
   for(4, (n)=>{print ((n+1) + " bottles of beer on the wall.")})
Output:
   1 bottles of beer on the wall.
   2 bottles of beer on the wall.
   3 bottles of beer on the wall.
   4 bottles of beer on the wall.

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:16 pm
by savask
So, does this mean that I should put the variable in the round brackets?

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:31 pm
by KarateBrot
If you call the variables "n" and "i":
n is the number of how often for will be looped
i is the counter and it will raise by 1 every loop, starting at 0

For example if you want to add a value that's raising by 1 to a variable 3 times:

Code: Select all
for(3, (i)=>{ scene.my.variable = scene.my.variable + i })


That means:
scene.my.variable = scene.my.variable + 0;
scene.my.variable = scene.my.variable + 1;
scene.my.variable = scene.my.variable + 2;

So if scene.my.variable is 5 at the beginning it will be 8 at the end.

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:34 pm
by savask
And if "i" will be bigger then "n", will it decrease?

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:35 pm
by KarateBrot
No i can't be bigger than n because n is the limit.

Do you know the sum-function in maths? for is exactly this thing.
Image

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:38 pm
by savask
Ok, thanks!

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 12:41 pm
by KarateBrot
No problem, dude :thumbup:

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 1:01 pm
by link0007
KarateBrot wrote:No i can't be bigger than n because n is the limit.

Do you know the sum-function in maths? for is exactly this thing.
Image

No it isn't.. the capital sigma function is a loop, yes, just like a for-loop.

Re: "For" loop.

PostPosted: Fri Oct 23, 2009 1:24 pm
by KarateBrot
that's what i meant. sorry, sometimes it's difficult to translate it from german to english