The while-statement is one of Magma's iterative statements. It has the syntax:
while CONDITION do STATEMENT; ... STATEMENT; end while;
To execute this statement, Magma initially evaluates the condition, which must be a Boolean expression. If it is true, then Magma executes the statements enclosed between `do' and `end while'. After that, Magma evaluates the condition again, and if it is true, the statements are executed once more. This process continues until the condition is false.
If you are entering a while-statement into Magma interactively, you will normally be given a special prompt
while>
until you type end while; .
The statement break; within a while-loop causes the loop to stop immediately. Execution proceeds from the statement immediately after the loop.
The statement continue; within a while-loop causes Magma to omit the rest of the block of statements and to go to the top of the loop to test the condition again. If the condition is true, execution proceeds from the beginning of the loop statements.
// a run of the (3n+1) problem > n := Random(1, 100); > while n gt 1 do while> print n; while> if IsEven(n) then while|if> n div:= 2; while|if> else while|if> n := 3*n+1; while|if> end if; while> end while; 13 40 20 10 5 16 8 4 2
[Next] [Prev] [_____] [Left] [Up] [Index] [Root]