RATS 11
RATS 11

WHILE  condition

  statement or block of statements executed as long as condition is “true”

END WHILE (omit if WHILE block is nested inside another compiled section)

 

WHILE sets up a conditional loop. RATS will execute the instructions in the statement block as long as the condition is true. WHILE tests the condition at the top of the loop, so it might never execute the statement block. The similar instruction UNTIL loops until a condition is false.

Parameters

condition

This is the expression that RATS tests. It can either be an integer or a real-valued expression. The condition is false if it has a value zero and true if it has any non-zero value. Usually, you construct it using logical and relational operators.

Description

When RATS encounters a WHILE instruction, it tests the condition. If it is true, RATS executes the statement or block of statements following the WHILE instruction, and then tests the condition again. As long as the condition is true, RATS will execute the statement(s). If the condition tests false, RATS immediately skips to the first instruction following the WHILE block. You can terminate the loop at any point with a BREAK instruction, or skip directly to the next condition check with NEXT.

The Statement Block

The statement(s) following the WHILE can be a single instruction, or a group of instructions enclosed in braces ({ }). You can also use a DO, DOFOR, WHILE, UNTIL or LOOP loop, or an IF-ELSE block. If you have any doubt about whether you have set up a block properly, just enclose the statements that you want executed in { and }.

 

If the WHILE instruction is the first instruction in a compiled section, you must enclose the loop in an extra set of { and } to signal the limits of the compiled section, or add an END WHILE to show the end of the compiled section.

Examples

This sums the first 20 positive values of X.

 

compute count=0, i=0, sum=0.0

while count<20 {

 compute i=i+1

 if x(i)>0.0

    compute sum=sum+x(i), count=count+1

 }

end while


 

This is part of a larger procedure (which is why it's indented). It makes repeated adjustments to the coefficients of a model until this model no longer has an explosive root.

 

      comp adjfactor=1., betatilde = betaols-adjfactor*bias

      comp %modelsetcoeffs(model,betatilde)

      eigen(cvalues=cv) %modelcompanion(model)

      while %cabs(cv(1))>=1. {

         comp betatilde = betaols-adjfactor*bias

         comp %modelsetcoeffs(model,betatilde)

         eigen(cvalues=cv) %modelcompanion(model)

         comp adjfactor=adjfactor-0.01

      }


 


Copyright © 2025 Thomas A. Doan