The WHILE statement is used in order to loop over a block of statements.
The syntax is:
WHILE (condition, statements; );
Warning: One cannot make declarations or define equations
inside a WHILE statement.
One can use WHILE statements to control the SOLVE statement. For instance, consider the following bit of GAMS code that randomly searches for a global optimum of a nonconvex model:
scalar count ; count = 0 ; while((count le 1000), x.l(j) = uniform(0,1) ; option bratio = 1 ; solve ml using lp minimizing obj ; if (obj.l le globmin, globmin = obj.l ; globinit(j) = x.l(j) ; ) ; count = count + 1 ; ) ;
In this example, a non-convex model is solved from 1000 random starting points, and the global solution is tracked.
The model PRIME in the model library illustrates the use of the WHILE statement through an example where the set of prime numbers less than 200 are generated
The following GAMS code is illegal since one cannot define equations inside a WHILE statement.
While (s GT 0, eq.. sum(i,x(i)) =g= 2 ; );
The following GAMS code is illegal since one cannot make declarations inside a WHILE statement.
WHILE (s gt 0, scalar y ; y = 5 ; );