Loop - EndLoop
The four valid loop constructions are:
Indexed Loop
While Loop
For Loop
ForEach Loop
The lines of FISH between loop and endloop are executed repeatedly until certain conditions are met. Specific details of each loop option are given below:
Loops may be nested to any depth.
Indexed Loop
In the first form, which uses a numeric counter, var is given the value of expr1 initially, and is incremented by expr3 at the end of each loop execution until it reaches a value greater than expr2. If expr3 is not given, a value of 1 is used by default.
The local statement may optionally be used before var to create var as a variable local to the function.
Note that expr1, expr2, and expr3 (which may be arbitrary expressions) are evaluated at the start of the loop; redefinition of their component variables within the loop has no effect on the number of loop executions.
Also note that the comparison of var with the result of expr2 is done at the end of the loop, so the interior of the loop will always be executed at least once regardless of the values.
var is a genaral FISH variable; it may be used in expressions within the loop (even in functions called from within the loop, if it is a global variable), and may even be redefined. The increment of the value designated by expr3 is done in terms of the general FISH + operator, and the comparison with expr2 is done with the < operator. Any type(s) compatibile with those operators may be used,although most commonly the type used is an integer or a real.
The code snippet below demonstrates basic usage of this form to sum all integers below a specified value.
With n`=:lint:`10, the end result of the functions is s = 55 and s2 = 30. Note that the loop value may be an integer or a real, and by using a negative value for expr3 it may go from a larger to a smaller value.
While Loop
In the second form of the loop structure, the loop body is executed while the test expression evaluates to true; otherwise control passes to the next line after the endloop statement.
The expressions may involve floating-point variables as well as integers; the use of Boolean, strings and pointers is also permitted under the same conditions that apply to the if statement.
The code snippet below demonstrate basic usage of this form to sum all even integers below a specified value.
For Loop
The main function of the third form of the loop structure is to repeat execution of the loop body while the test expression is true, similar to the loop while form. But, in addition, the loop for construct provides specific locations to contain an initialize field and a modify field. So this loop is specially designed to perform a repetitive action with a counter which is initialized and modified on each iteration. It works in the following way.
Initialization is executed. Generally it is an initial value setting for a counter variable (which can be local or global). This is executed only once.
Condition is checked. If it is true, the loop continues; otherwise, the loop ends and the loop body is not executed.
Loop body is executed.
Finally, whatever is specified in the modify field is executed, and the loop goes back to step 2.
The code snippet below demonstrate basic usage of this form to sum all odd integers below a specified value.
With n = 10, the end result of the sum is 25.
The first three forms of the loop statement may be contrasted. In the first, the test is done at the end of the loop (so there will be at least one pass through the loop); in the second and third, the test is done at the start of the loop (so the loop will be bypassed if the test is false initially).
ForEach Loop
The fourth form of the loop statement is a specialized syntax to allow iterating through all objects inside a given iterable object.
In this case, expr1 must evaluate to an iterable object, most commonly a pointer to a container or a list.
For example, all scalars in the list of user-defined scalars are returned by the FISH intrinsic data.scalar.list
.
As with the first loop form, var may be preceded by the local statement to indicate that a local variable is to be created instead of a global one. var will be assigned to each value in the iterable object consecutively.
If the object being looped over is a pointer to a container, and var is deleted during processing of the loop, the loop will continue normally as long as var is not referenced again within the same loop after deletion. If other items in the container are deleted (for example, if a function that deletes the contents of the container is called), then the loop might exit prematurely.
Loop Control Statements
Within the loop, the exit loop
statement may be used to break out of the loop and continue processing after the endloop
statement.
In addition, the continue
statement may be used to stop processing a current loop iteration and continue to the next iteration, without executing code that may lie between it and the endloop
statement.
Was this helpful? ... | Itasca Software © 2024, Itasca | Updated: Dec 03, 2024 |