|
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; );
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:
option forlim=10; |