|
While |
Top Previous Next |
|
The While statement allows one to repeatedly execute a block of statements until a logical condition is satisfied. Ordinarily, the syntax of the While statement is: While (logical condition, statements to be executed While condition is true; ); But when $Onend is specified the statement becomes Examples: A binary root finder using While (While.gms) is as follows
While (converge = 0 and iter lt lim, root=(maxroot+minroot)/2; iter=iter+1; function_value=a-b*root+c*sqr(root); If(abs(function_value) lt tolerance, converge=1; else If(sign(function_value1)=sign(function_value), minroot=root; function_value1=function_value; else maxroot=root; function_value2=function_value; ); ); );
or
$onend While converge = 0 and iter lt lim do root=(maxroot+minroot)/2; iter=iter+1; function_value=a-b*root+c*sqr(root); function_value=function_value; If(abs(function_value)) lt tolerance then converge=1; else if(sign(function_value1)=sign(function_value)) then minroot=root; function_value1=function_value; else maxroot=root; function_value2=function_value; endif; endif; endwhile;
Notes:
|