For, To, Downto, and By

Top  Previous  Next

The For statement allows one to repeatedly execute a block of statements over a successively varied values of a scalar.

for (scalarq = startval to(downto) endval by increment,

 statements;

 );

 

wherescalarq is a scalar

startval is the constant or scalar giving the value where scalarq will begin

endval is a constant or scalar giving the value that will result in statement termination when scalarq equals or passes it

increment is a positive constant or scalar which is optional and defaults to one

To indicates that GAMS will add increment until scalarq gets equal to or larger than end

Downto indicates that GAMS will subtract increment until scalarq gets equal to or smaller than end.

Example:

(For.gms)

for (iter = 1 to iterlim,

  root=(maxroot+minroot)/2;

  function_value=a-b*root+c*sqr(root);

  If(abs(function_value) lt tolerance,

          iter=iterlim;

  else

            If(sign(function_value1)=

         sign(function_value),

         minroot=root;

         function_value1=function_value;

            else

         maxroot=root;

         function_value2=function_value;);

             );

  );

Notes:

One cannot incorporate parameter, set, file, table, model, equation, variable or scalar statements or .. equation declarations inside a for statement.
The values of start, end and increment need not be integer.  The start and end values can be positive or negative real numbers.
The value of increment has to be a positive real number.
The total number of passes through the For statements can be limited using the option Forlim.

 option forlim=10;