"For" loop.

About advanced scenes, and the Thyme scripting language used in Algodoo.

"For" loop.

Postby savask » Fri Oct 23, 2009 11:48 am

Is there a "For" loop in Algodoo or Phun?
Like in Pascal or C.
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 12:01 pm

Yes :thumbup:
Code: Select all
for(n,(i)=>{  })
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: "For" loop.

Postby savask » Fri Oct 23, 2009 12:03 pm

Thank you!
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 12:11 pm

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: "For" loop.

Postby savask » Fri Oct 23, 2009 12:16 pm

So, does this mean that I should put the variable in the round brackets?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 12:31 pm

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.
Last edited by KarateBrot on Fri Oct 23, 2009 12:35 pm, edited 1 time in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: "For" loop.

Postby savask » Fri Oct 23, 2009 12:34 pm

And if "i" will be bigger then "n", will it decrease?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 12:35 pm

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
Last edited by KarateBrot on Fri Oct 23, 2009 12:43 pm, edited 2 times in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: "For" loop.

Postby savask » Fri Oct 23, 2009 12:38 pm

Ok, thanks!
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 12:41 pm

No problem, dude :thumbup:
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: "For" loop.

Postby link0007 » Fri Oct 23, 2009 1:01 pm

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.
Link: "Surely somebody hates Walter Cronkite.."
Sonic: "Probably.. But somebody hates everyone."
:D
User avatar
link0007
 
Posts: 408
Joined: Thu Jun 11, 2009 2:45 pm

Re: "For" loop.

Postby KarateBrot » Fri Oct 23, 2009 1:24 pm

that's what i meant. sorry, sometimes it's difficult to translate it from german to english
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests