HyperMath

while

while

Previous topic Next topic No expanding text in this topic  

while

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

The conditional looping statement while has the form:

while exp do

  block

end

For example, a simple loop:

> i = 3

> while i>0 do

   print(i,"\n")

    i = i-1

 end

 

3

2

1

You can exit the control of a while statement using the break keyword.  The break keyword must be the last statement in a block.

 

> a = 0

The following has to be on a single line if typed in the command window; here it’s separated for clarity:

> while true do /* infinite loop */

   print(a, ",");

   a = a+100;

   if a>500 then

      break;      //break is the last keyword inside the if statement

   end // exit the loop if the condition is true

 end            

0,100,200,300,400,500

HyperMath supports another syntax for the while statement that is akin to C-programming style. The two formats cannot be mixed.

 

while (exp) {

  block

}

So, the first example above can be reproduced as follows with this syntax:

> i = 3

> while (i>0) {

   print(i,"\n")

   i = i-1

 }

 

3

2

1