FOR

From ASCEND
Jump to: navigation, search

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. In the example below, equations are added via refinement of a sub-model using IS_REFINED_TO.

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.