What's the difference between for and repeat?

For questions about using Classic.

Post » Fri May 15, 2009 6:39 pm

They seem pretty similar to me...

for 0 to 9 = repeat 10 times (or 9 times, i forget :mrgreen: )

anything i should know about?

redundant?
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Fri May 15, 2009 6:53 pm

Repeat just repeats a number of times, and you can only use a positive whole number.

For can use positive/negative values. For instance, you can

for x = -10 to 57

or

for x = 4 to 90003

The loop appears to only run forward though, so the end value has to be higher than the start value. Loopindex returns the current value, so if you start counting on 10 then the first loopindex is 10.

It might be handy to add a Step to the For loop, as in BASIC, so you could do like so:

for x = 1 to -10 step -1

that way it would count backwards. Or you could do

for x = 5 to 20 step 5

and it would count in increments of 5.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Fri May 15, 2009 7:38 pm

Repeat is just a very simple version of For. You can nest For's and still get the current index of the outside loop - because it has a name, so you can do LoopIndex("parent loop name") - but if you nest Repeat conditions, it's impossible to retrieve the index of the outside loop.

The main reasons it exists are clarity ('Repeat 100 times' in some cases is clearer than 'For "i" from 1 to 100'), and for learning Construct. I've seen a number of new users struggle with the concept of loops in the past, and 'Repeat' is a very simple condition to explain and understand. Once they grasp that, it is much easier to explain a For loop (and other loops) in terms of a Repeat loop.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests