Euphoria - The for statement


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

A for statement sets up a special loop that has its own loop variable. The loop variable starts with the specified initial value and increments or decrements it to the specified final value.

A for loop is useful when you know the exact number of times a task is required to be repeated.


The syntax of a for loop is as follows −

for "initial value" to "last value" by "inremental value" do
   -- Statements to be executed.
end for

Here, you initialize the value of a variable and then body of the loop is executed. After every iteration, variable value is increased by the given incremental value. The last value of the variable is checked and if it is reached, then loop is terminated.

The initial value, last value, and increment must all be atoms. If no increment is specified then +1 is assumed.

The for loop does not support with entry statement.



for a = 1 to 6 do
   printf(1, "value of a %d\n", a)
end for

This produces the following result −

value of a 1
value of a 2
value of a 3
value of a 4
value of a 5
value of a 6

The loop variable is declared automatically. It exists until the end of the loop. The variable has no value outside of the loop and is not even declared. If you need its final value, you need to copy it into another variable before leaving the loop.

Here is one more example with incremental value −


for a = 1.0 to 6.0  by 0.5 do
   printf(1, "value of a %f\n", a)
end for

This produces the following result −

value of a 1.000000
value of a 1.500000
value of a 2.000000
value of a 2.500000
value of a 3.000000
value of a 3.500000
value of a 4.000000
value of a 4.500000
value of a 5.000000
value of a 5.500000
value of a 6.000000