Topic 5. Operators. Operators condition and cycle. Sequences
2. Operators of the cycle
For organizing repetitive calculations in Maple, two forms of cycle operators are provided: for- from and for- in.
The first operator of the cycle is universal and includes both cycles, repetitions of a given number of times, and cycles that are performed until some Boolean expression is true.
The second form of the loop for implements a loop over a list item or a plurality, and in other programming languages it is known as the for- each loop.
Loop operators for
The most common syntax for the loop operator for - from the following:
[for name] [from expression] [by expression] [to expression] [while boolean expression]
do sequences_operators end do;
or else:
for var from val1 by val2 while bool1 do expr od
The body of the cycle expr is executed until the logical expression bool1 is true, the variable var varies from the value val1 in step val2.
Example:
for x from 1 by 2 while x <6 do print (x) od;
1
In the for box, the variable name of the loop is specified, the blocks from and to define the initial and final values of the change range of the loop, and in the block by the step it changes (it may be negative).
The execution of the cycle begins with the assignment of the variable of the cycle of the initial value, after which it is checked whether it exceeds the final value, and in the case of a positive answer, the operators of the cycle of the cycle are given in the block do ... end do, the cycle variable is incremented by the step value and the verification algorithm starts again.
If the value of the cycle variable exceeds the final value, then the cycle stops its execution. Or, if the initial value of the cycle variable is checked, it exceeds the final value, then the cycle completes its execution, and the operators of the body of the cycle are never executed.
If the given block is while, then at the same time as checking the value of the cycle variable, it is verified by the true Boolean expression of that block, and the loop also terminates if its value is equal to false.
All of these blocks are optional and can be specified in an arbitrary order with one exception: if there is a block for, then it should be given the first one.
If no block is specified then its parameter accepts the default value:
for i from 1 to 2 do evalf (sqrt (i)) end do;
The second form of the operator of the for - in loop arranges a loop over the elements of the object, which can be represented by a sequence, a list, a sum, a product, or a string. Its general syntax has the form:
for the name in the object [while boolean_expression] do the sequence of operators end do;
The variable cycle defined in the for ... in block consistently takes the value of the operands of the object, as they are defined by the command op ().
The cycle is executed as many times as the operands are specified in the object, unless the boolean expression in the optional block while does not become false earlier than all object operands are sequentially selected.
# Summation of plural elements
S: = {x, y, z}: s: = 0:
for I in S do s: = s + I end do: s;
x + y + z
Normal completion of any cycle occurs either when the value of the cycle variable exceeds a given final value (in the case of a positive step), or it has become less than this (in the case of a negative step), or the boolean condition of the condition while the block is false.
If it is necessary to interrupt the further execution of the iterations of a cycle under the fulfillment of some condition.
For such cases, the Maple language provides a break statement whose primary purpose is to complete the cycle. It is used in conjunction with the condition operator if.
# Summing up the first two sets of the set
S: = {x, y, z}: s: = 0: j: = 1:
for I in S do s: = s + I; if j> 2 then break; end if; j: j + 2; end do: s; j;
x + y
3
The cycle can be interrupted with the while block.
For I from 1 to 5 while i <6 do print (i) end do;
Loop according to the given list:
for i in [1,2,3,4,5] do print (i) end do;
Cycle operators while
The operator of a loop type 'while' has the form: while bool do expr od.
The body of the cycle expr is executed until the value of the logical expression bool = true, and the execution is terminated if bool = false.
Example:
j: = 0:
while j <5 do j: = (j + 1) ^ j od;
Font Face
Font Size
Text Colour
Background Colour
Font Kerning
Image Visibility
Letter Spacing
Line Height
Link Highlight