Home, Up: Loop and Script Examples
In this example:
PROMPT, ISLOOP, Loop End through User Innput, ABORT.
This example shows how a script can be used both directly and in a loop, and it shows how user input can be used to exit a loop.
Let's look at our body mass index script bmi from the page Script Example: Body Mass Index -- let's first work with the simple form, in which the input has to be in cm and kg:
PROMPT $cm enter height in cm:
PROMPT $kg enter weight in kg:
$kg $cm 100 / SQ / 1 ROUND
If we want to calculate the body mass indices of several people, we could call this script in a loop -- if we don't know in advance how many calculations we want to perform, we'll use an infinite loop, but then, how can we exit it? By telling the user to enter a height of 0, and adding one line that aborts the loop when this happens.
PROMPT $cm enter height in cm, 0 to exit:
IF $cm IS0 THEN ABORT
PROMPT $kg enter weight in kg:
$kg $cm 100 / SQ / 1 ROUND
But when we call this script in a loop:
? _*bmi
it will calculate body mass indices, but it will not show us the results -- in a loop, only the last result is shown after the loop has ended!
So how do we see the results? One possibility would be to add the line SHOW $, but it's easier to use the command = which displays the value of $ in the currently chosen format.
The last two lines of the script would then be:
$kg $cm 100 / SQ / 1 ROUND
=
This would work, but it would have a minor flaw: if this script were called directly instead of in a loop, then the result would be displayed twice (first by the command = and then because the result always gets displayed at the end of a script).
Even if this would only be a minor flaw, we can avoid it with the help of the ISLOOP pseudo constant -- it is 0 outside of a loop, and 1 inside of a loop:
IF ISLOOP THEN =
So now we have a script that works both in a loop and when called directly:
PROMPT $cm enter height in cm, 0 to exit:
IF $cm IS0 THEN ABORT
PROMPT $kg enter weight in kg:
$kg $cm 100 / SQ / 1 ROUND
IF ISLOOP THEN =
And of course we can also do this with the version of the script that supports input both in cm and kg, and in feet, inches and pounds.
(Note that the variable that contains the height is called $height in this version, not $cm. You can, of course, give it any name you want.)
PROMPT $height enter height in cm or in ft (without inches), 0 to exit:
IF $height IS0 THEN ABORT
IF $height 10 >> THEN PROMPT $kg enter weight in kg:
ALSO: $kg $height 100 / SQ / 1 ROUND
ELSE: PROMPT $in enter inches:
ALSO: PROMPT $lb enter weight in pounds:
ALSO: $lb :kg $height $in 12 / + :m SQ / 1 ROUND
IF ISLOOP THEN =
On the page Script Example: Body Mass Index we also had a version of this script that used SKIP instead of ELSE: -- in that version, you'd need two separate IF ISLOOP THEN = lines to show the results for cm and kg, and feet, inches and pounds.
Anyway, now we can call our script for a single calculation,
? _bmi
or in an infinite loop to calculate as many body mass indices as we want, in both measurement systems, until we enter a height of zero!
? _*bmi
Home, Up: Loop and Script Examples, Next: Loop Example: Rectangles