Difference between revisions of "Begin-End for Grouping Expressions"
m (hyperlinking to functions) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
<breadcrumbs> Analytica User Guide > Procedural Programming > {{PAGENAME}}</breadcrumbs><br /> | <breadcrumbs> Analytica User Guide > Procedural Programming > {{PAGENAME}}</breadcrumbs><br /> | ||
− | As illustrated above, you can group several expressions (statements) as the definition of a variable or function simply by separating them by semicolons (<code>;</code>). To group several expressions as a condition or action of | + | As illustrated above, you can group several expressions (statements) as the definition of a variable or function simply by separating them by semicolons (<code>;</code>). To group several expressions as a condition or action of [[IF a THEN b ELSE c|If a Then b Else c]] or [[For_and_While_Loops#While.28Test.29_Do_Body|While a Do b]], or, indeed, anywhere a single expression is valid, you should enclose the expressions between <code>Begin</code> and <code>End</code>, or between parentheses characters <code>(</code> and <code>)</code>. For example: |
Grouping with <code>Begin</code> and <code>End</code>: | Grouping with <code>Begin</code> and <code>End</code>: | ||
− | + | Variable rot := ... { a 2-D rotation matrix indexed by Dim and Dim2 } | |
− | + | ||
− | + | Variable X_rot := [[ComputedBy]](Y_rot) | |
− | + | ||
− | + | Variable Y_rot := | |
− | + | [[Begin]] | |
− | + | [[Local]] v := [[Array]](Dim, [X, Y]); | |
− | + | [[Local]] v_r := [[Sum]](rot*v, Dim); | |
− | + | X_rot := v_r[Dim2 = 'x']; | |
+ | v_r[Dim2 = 'y']; | ||
+ | [[End]] | ||
Grouping with parentheses: | Grouping with parentheses: | ||
− | + | ||
− | Function Table_to_array(A : Array[Rows, Columns] ; Rows, Columns: IndexType) | + | Function Table_to_array(A : Array[Rows, Columns] ; Rows, Columns: IndexType) |
− | Definition: | + | Definition: |
− | + | [[LocalIndex]] Inds := | |
− | + | ( | |
− | + | [[For]] c := 1..[[IndexLength]](Columns)-1 Do ( | |
− | + | [[Local]] v := A[@Columns = c]; | |
− | + | [[LocalIndex]] I / [[Slice]](Columns,c) := v[Rows = [[Unique]](v, Rows)]; | |
− | + | [[Handle]](I) | |
− | + | ) | |
− | + | ); | |
− | + | [[MdTable]](A, Rows, Columns, inds) | |
− | + | ||
The overall value of the group of statements is the value from evaluating the last expression. For example: | The overall value of the group of statements is the value from evaluating the last expression. For example: | ||
− | :<code>( | + | :<code>([[Local]] x := 10; x := x/2; x - 2) → 3</code> |
Analytica also tolerates a semicolon (<code>;</code>) after the last expression in a group. It still returns the value of the last expression. For example: | Analytica also tolerates a semicolon (<code>;</code>) after the last expression in a group. It still returns the value of the last expression. For example: | ||
− | :<code>( | + | :<code>([[Local]] x := 10; x := x/2; x/2;) → 2.5</code> |
The statements can be grouped on one line, or over several lines. In fact, Analytica does not care where new-lines, spaces, or tabs occur within an expression or sequence of expressions — as long as they are not within a number or identifier. | The statements can be grouped on one line, or over several lines. In fact, Analytica does not care where new-lines, spaces, or tabs occur within an expression or sequence of expressions — as long as they are not within a number or identifier. | ||
Line 47: | Line 49: | ||
− | <footer>Summary of Programming Constructs / {{PAGENAME}} / Local | + | <footer>Summary of Programming Constructs / {{PAGENAME}} / Local Values</footer> |
Latest revision as of 23:09, 3 July 2024
As illustrated above, you can group several expressions (statements) as the definition of a variable or function simply by separating them by semicolons (;
). To group several expressions as a condition or action of If a Then b Else c or While a Do b, or, indeed, anywhere a single expression is valid, you should enclose the expressions between Begin
and End
, or between parentheses characters (
and )
. For example:
Grouping with Begin
and End
:
Variable rot := ... { a 2-D rotation matrix indexed by Dim and Dim2 }
Variable X_rot := ComputedBy(Y_rot)
Variable Y_rot := Begin Local v := Array(Dim, [X, Y]); Local v_r := Sum(rot*v, Dim); X_rot := v_r[Dim2 = 'x']; v_r[Dim2 = 'y']; End
Grouping with parentheses:
Function Table_to_array(A : Array[Rows, Columns] ; Rows, Columns: IndexType) Definition: LocalIndex Inds := ( For c := 1..IndexLength(Columns)-1 Do ( Local v := A[@Columns = c]; LocalIndex I / Slice(Columns,c) := v[Rows = Unique(v, Rows)]; Handle(I) ) ); MdTable(A, Rows, Columns, inds)
The overall value of the group of statements is the value from evaluating the last expression. For example:
(Local x := 10; x := x/2; x - 2) → 3
Analytica also tolerates a semicolon (;
) after the last expression in a group. It still returns the value of the last expression. For example:
(Local x := 10; x := x/2; x/2;) → 2.5
The statements can be grouped on one line, or over several lines. In fact, Analytica does not care where new-lines, spaces, or tabs occur within an expression or sequence of expressions — as long as they are not within a number or identifier.
See Also
Enable comment auto-refresher