Register Login



Loops

Math Processor provides two kinds of loops: (1) repeat loop; and (2) while loop. Details of these are given below.

repeat

The synax of the repeat loop is as under:

repeat (count) { command_1:command_2: ... : command_n:}

Here count is an integral value, which tells how many times the loop should execute. The loop can be exited anytime by using the break statement.

Example

Following is an MP example session showing the use of repeat and break statements:

>> num1 = 1
>> 1.0000
>> repeat (10) { num1 : if ( num1 == 3) { break:} : num1 = num1 + 1;: }
>> 1.0000
>> 2.0000
>> 3.0000

while

The synax of the while loop is as under:

while (boolean_expression) { command_1:command_2: ... : command_n:}

Here boolean_expression is a simple or compound Boolean Expression. The loop can be exited anytime by using the break statement.

The while loop also uses a variable maxtime to control its execution time. The variable maxtime stores the number of milliseconds the loop is allowed to execute. At the end of each iteration, MP chekcs if the time spent in the loop is less than maxtime. If not, the execution is prematurely aborted. You can modify the value of maxtime to increase or decrease the maximum time you want to allocate to while loops. By default this value is 5000 milliseconds (5 seconds).

Example

Following is an MP example session showing the use of while and break statements:

>> num1 = 1
>> 1.0000
>> while (num1 < 10) { num1 : if ( num1 == 3) { break:} : num1 = num1 + 1;: }
>> 1.0000
>> 2.0000
>> 3.0000