Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reduce (computer algebra system)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Repetition statements: for ... ==== The <code>for</code> statement is a flexible loop construct that executes ''statement'' repeatedly a number of times that must be known in advance. One version has the form <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>for</code> ''variable'' := ''initial'' <code>step</code> ''increment'' <code>until</code> ''final'' <code>do</code> ''statement'' </div> where ''variable'' names a variable whose value can be used within ''statement'', and ''initial, increment'' and ''final'' are numbers (preferably integers). The value of ''variable'' is initialized to ''initial'' and ''statement'' is executed, then the value of ''variable'' is repeatedly increased by ''increment'' and the ''statement'' executed again, provided the value of ''variable'' is not greater than ''final''. The common special case "''initial'' <code>step 1 until</code> ''final''" can be abbreviated as "''initial'' : ''final''". The following <code>for</code> statement computes the value of <math>n!</math> as the value of the variable <code>fac</code>. <syntaxhighlight lang="octave" copy=""> n := 5; fac := 1$ for r := 2 : n do fac := fac*r; fac; </syntaxhighlight> Another version of the <code>for</code> statement iterates over a list, and the keyword <code>do</code> can be replaced by <code>product</code>, <code>sum</code>, <code>collect</code> or <code>join</code>, in which case the <code>for</code> statement becomes an expression and the controlled ''statement'' is treated as an expression. With <code>product</code>, the value is the product of the values of the controlled ''statement''; with <code>sum</code>, the value is the sum of the values of the controlled ''statement''; with <code>collect</code>, the value is the values of the controlled ''statement'' collected into a list; with <code>join</code>, the value is the values of the controlled ''statement'', which must be lists, joined into one list. The following <code>for</code> statement computes the value of <math>n!</math> much more succinctly and elegantly than the previous example. <syntaxhighlight lang="octave" copy=""> n := 5; for r := 2 : n product r; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)