Tutorials Point


  Euphoria Basics
  Euphoria Useful References
  Euphoria Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Euphoria - The for statement


previous next AddThis Social Bookmark Button


Advertisements

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 how many times a task is to be repeated.

Syntax:

The syntax of a for loop is:

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

Here you would initialise initial value of a variable and then body of the loop is executed. After every iteration, variable value is increased by the given incremental value and then last value of the variable is checked and it it is reached then loop would be 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.

Example:

#!/home/euphoria-4.0b2/bin/eui

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

This would produce 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 and exists until the end of the loop. Outside of the loop the variable has no value and is not even declared. If you need its final value, copy it into another variable before leaving the loop.

Check one more example with incremental value:

#!/home/euphoria-4.0b2/bin/eui

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

This would produce 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


previous next Printer Friendly

Advertisements


  

Advertisements