gams:the_while_statement

The WHILE statement is used in order to loop over a block of statements.

Syntax

The syntax is:

WHILE (condition, statements; );

Warning: One cannot make declarations or define equations

inside a WHILE statement.

Example 1

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.

Example 2

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

Example 3

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 ; );

Example 4

The following GAMS code is illegal since one cannot make declarations inside a WHILE statement.

WHILE (s gt 0, scalar y ; y = 5 ; );

IMPRESSUM / LEGAL NOTICE
PRIVACY POLICY
gams/the_while_statement.txt · Last modified: 2007/02/02 16:48 (external edit)