Sets variable var to start, and for as long as this variable is more than or equal to nish, executes the
sequence of commands, and then subtracts increment from var.
WHILE
Syntax: WHILE test DO commands END;
Evaluates test. If result is true (not 0), executes the commands, and repeats.
Example: A perfect number is one that is equal to the sum of all its proper divisors. For example, 6 is a perfect
number because 6 = 1+2+3. The example below returns true when its argument is a perfect number.
EXPORT ISPERFECT(n)
BEGIN
LOCAL d, sum;
2 ▶ d;
1 ▶ sum;
WHILE sum <= n AND d < n DO
IF irem(n,d)==0 THEN sum+d ▶ sum;
END;
d+1 ▶ d;
END;
RETURN sum==n;
END;
The following program displays all the perfect numbers up to 1000:
EXPORT PERFECTNUMS()
BEGIN
LOCAL k;
FOR k FROM 2 TO 1000 DO
IF ISPERFECT(k) THEN
MSGBOX(k+" is perfect, press OK");
END;
END;
END;
REPEAT
Syntax: REPEAT commands UNTIL test;
Repeats the sequence of commands until test is true (not 0).
The example below prompts for a positive value for SIDES, modifying an earlier program in this chapter:
EXPORT SIDES;
Program commands 587
Komentarze do niniejszej Instrukcji