FOR: Difference between revisions

From ASCEND
Jump to navigation Jump to search
Restored page from Google Cache, uploaded by John Pye
 
No edit summary
Line 4: Line 4:
FOR..CREATE statements are used with the ''declarative'' part of a model to declare many similar equations or variables. The statement is 'FOR..CREATE' to reflect the fact that for each iteration of the loop, a new equation or variable is added (permanently) to the model.
FOR..CREATE statements are used with the ''declarative'' part of a model to declare many similar equations or variables. The statement is 'FOR..CREATE' to reflect the fact that for each iteration of the loop, a new equation or variable is added (permanently) to the model.


<source lang="a4c">A[1..4] IS_A finite_difference;
<source lang="a4c">
 
A[1..4] IS_A finite_difference;
FOR i IN [1,2,3,4] CREATE
FOR i IN [1,2,3,4] CREATE
     A[i] IS_REFINED_TO central_difference(x[i-1],x[i+1]);
     A[i] IS_REFINED_TO central_difference(x[i-1],x[i+1]);
 
END FOR;
END FOR;</source>
</source>


== Procedural FOR statement (FOR..DO) ==
== Procedural FOR statement (FOR..DO) ==
Line 17: Line 16:
This is used to perform actions on your model, such as [[FIX|FIXing]] variables, setting values, etc:
This is used to perform actions on your model, such as [[FIX|FIXing]] variables, setting values, etc:


<source lang="a4c">FOR i IN [1,2,3,4] DO
<source lang="a4c">
 
FOR i IN [1,2,3,4] DO
     x[i]&nbsp;:= 7 + 0.1 * i;
     x[i] := 7 + 0.1 * i;
 
END FOR;
END FOR;</source>
</source>


Note that reverse-order loops are possible, using
Note that reverse-order loops are possible, using


<source lang="a4c">FOR i IN [n_stages..1] DECREASING DO
<source lang="a4c">
 
FOR i IN [n_stages..1] DECREASING DO
     (* statements here *)
     (* statements here *)
END FOR;</source>
END FOR;
</source>


== See also ==
== See also ==
Line 36: Line 36:
* Further syntax information and examples in the PDF file linked to from [[Category:Documentation]].
* Further syntax information and examples in the PDF file linked to from [[Category:Documentation]].


 
[[Category:Documentation]]
 
[[Category:Documentation]]
[[Category:Syntax]]
[[Category:Syntax]]

Revision as of 02:33, 17 November 2013

The FOR statement exists in two forms, declarative and procedural.

Declarative FOR statement (FOR..CREATE)

FOR..CREATE statements are used with the declarative part of a model to declare many similar equations or variables. The statement is 'FOR..CREATE' to reflect the fact that for each iteration of the loop, a new equation or variable is added (permanently) to the model.

A[1..4] IS_A finite_difference;
FOR i IN [1,2,3,4] CREATE
    A[i] IS_REFINED_TO central_difference(x[i-1],x[i+1]);
END FOR;

Procedural FOR statement (FOR..DO)

When FOR statements are used with METHODs, they can contain procedural code that is run whenever the METHOD is run. This is used to perform actions on your model, such as FIXing variables, setting values, etc:

FOR i IN [1,2,3,4] DO
    x[i] := 7 + 0.1 * i;
END FOR;

Note that reverse-order loops are possible, using

FOR i IN [n_stages..1] DECREASING DO
    (* statements here *)
END FOR;

See also

  • Arrays and sets (usually you will be using arrays within your FOR loops)
  • WHILE
  • Further syntax information and examples in the PDF file linked to from .