Discord
Login
Community
DARK THEME

Repeat A Code A Number Times

Like Scratch,the repeat would repeat the code in it a number times.

Hmm. Good idea, it would certainly make it easier for people like me (who use while loops)

Maybe we should just create an import scratch function lol

This will be useful for drawing a lot of sprites.

There are two main options already for looping code x amount of times:

for i=1 to x
  insert code here
end

and

i=1
while i < x
  insert code here
  i += 1
end

These function identically to the repeat function in scratch, but with the added use of i being usable as a variable inside the loop (representing the amount of times looped).

Additionally, lopping for every item in a list can be done like this:

for i in list
 insert code here
end

In this case, i also represents the corresponding value in the list:

for i in [1,"word",78]
  print(i+1)
end

the result in the console:

2
"word1"
79

It can take a bit to wrap your head around for and while loops, but they can be much more powerful than a repeat function if used properly (while loops repeat as long as a condition is true, allowing for interesting uses in code, and for loops can loop through lists and objects, saving a bunch of time).

Although I guess they can't compete with the simplicity of just doing this:

repeat(x)
  insert code here
end

Ok cool.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community