"For" loop.
12 posts • Page 1 of 1
"For" loop.
Is there a "For" loop in Algodoo or Phun?
Like in Pascal or C.
Like in Pascal or C.
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: "For" loop.
Yes
- Code: Select all
for(n,(i)=>{ })
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: "For" loop.
Thank you!
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: "For" loop.
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: "For" loop.
So, does this mean that I should put the variable in the round brackets?
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: "For" loop.
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:
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.
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: "For" loop.
And if "i" will be bigger then "n", will it decrease?
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: "For" loop.
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.
Do you know the sum-function in maths? for is exactly this thing.
Last edited by KarateBrot on Fri Oct 23, 2009 12:43 pm, edited 2 times in total.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: "For" loop.
Ok, thanks!
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: "For" loop.
No problem, dude
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: "For" loop.
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.
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."
Sonic: "Probably.. But somebody hates everyone."
-
link0007 - Posts: 408
- Joined: Thu Jun 11, 2009 2:45 pm
Re: "For" loop.
that's what i meant. sorry, sometimes it's difficult to translate it from german to english
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
12 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 12 guests