Repeat, Until

Top  Previous  Next

The Repeat statement causes one to execute a block of statements over and over until a logical condition is satisfied.  The syntax of the Repeat statement is:

repeat ( statements to be executed;

   until logical condition is true );

Examples:

A binary root finder using Repeat may be found in repeat.gms

repeat (

  root=root+inc;

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

  If((sign(function_value1) ne sign(function_value2)

    and abs(function_value1) gt 0

    and abs(function_value2) gt tolerance),

      maxroot=root;

      signswitch=1

  else

    If(abs(function_value2) gt tolerance,

      function_value1=function_value2;

      minroot=root;));

  until (signswitch>0 or root > maxroot) ;);

Notes:

Repeat is followed by an open ( and a close ) which surround subsequent statements to be executed, an until and the logical condition which when true causes statement termination.
The until and logical condition are at the end of the statement.
The until precedes the logical condition and is then followed by a closing parenthesis.
One cannot place GAMS set, acronym, for, scalar, parameter, table, variable, equation or model statements inside the repeat statement.
Many logical condition forms are possible as explained in the Conditionals chapter.
The total number of passes through the Repeat statements can be limited using the option Forlim.