![]() | Programming Guide | ![]() |
Repeatedly executes a block while a condition is true
whilelogicalExprstatementBlock
logicalExpr- Specifies the condition that JPL uses to determine whether to reiterate execution of the while block.
statementBlock- One or more statements that JPL executes if
logicalExprevaluates to true. IfstatementBlockhas more than one statement, enclose the block with open and close blocking characters {0} on the lines before and after.
The
whilestatement repeatedly executes a block of one or more statements as long as the value oflogicalExpris true. JPL evaluateslogicalExprbefore each iteration of the loop.
// do do_proc as often as user wants
vars ans
ans = sm_message_box \
("Start processing?","",SM_MB_YESNO, "")
while ans
{
call do_proc
ans = sm_message_box \
("Repeat processing?","",SM_MB_YESNO, "")
}When you construct a logical expression, take into account that JPL, unlike C, always fully evaluates a boolean expression. For example, the following while loop traverses a screen's fields by field number (ct) until the last field or the first modified field is reached:
vars ct
vars n_flds = @screen("@current")->numflds
while ct <= n_flds && @field_num(ct)->mdt == PV_NO
{
...
ct = ct + 1
}If all fields are unmodified,
ctincrements to one greater thann_fldson the last pass through the while loop, so the first condition evaluates to false; however, JPL also evaluates the second condition@field_num(ct), which is invalid. Consequently, JPL issues an error message and stops execution of the remaining code.