Looping


native! loop  Red-by-example    MyCode4fun

Executes a block a given number of times.

Red[]


loop 3 [print "hello!"]


hello!
hello!
hello!
>>

native! repeat  Red-by-example  

repeat is the same as loop, but it has an index that gets incremented each loop

Red[]


repeat i 3 [print i]


1
2
3
>>

native! forall  Red-by-example    MyCode4fun

Executes a block as it moves forward in a series.

Red[]


a: ["china" "japan" "korea" "usa"]

forall a [print a]


china japan korea usa
japan korea usa
korea usa
usa
>>

native! foreach  Red-by-example    MyCode4fun

Executes a block for each element of the series.

Red[]


a: ["china" "japan" "korea" "usa"]

foreach i a [print i]


china
japan
korea
usa
>>

native! while  Red-by-example    MyCode4fun

Executes a block while a condition is true.

Red[]


i: 1

while [i < 5] [

print i

       i: i + 1

]


1
2
3
4
>>

native! until  Red-by-example    MyCode4fun

Evaluates a block until the block returns a true value.

Red[]


i: 4

until [

       print i

       i: i - 1

       i < 0        ;  <= condition

]


4
3
2
1
0
>>

native! break  Red-by-example    MyCode4fun

Forces an exit from the loop.

/return

Forces the exit and sends a given value, like a code or a message, as a return value.

native! forever  Red-by-example    MyCode4fun

Creates a loop that never ends.


< Previous topic                                                                                          Next topic >