# What's the difference between for and repeat?

### » 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 )

redundant?
B
49
S
10
G
9
Posts: 1,224
Reputation: 8,480

### » 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

### » 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
414
S
245
G
92
Posts: 25,198
Reputation: 200,327