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!
=== Structured programming === REDUCE supports [[Conditional (computer programming)|conditional]] and repetition statements, some of which are controlled by a ''boolean expression'', which is any expression whose value can be either ''true'' or ''false'', such as <math>x>0</math>. (The REDUCE user-level language does not explicitly support constants representing ''true'' or ''false'' although, as in [[C (programming language)|C]] and related languages, 0 has the boolean value ''false'', whereas 1 and many other non-zero values have the boolean value ''true''.) ==== Conditional statements: if ... then ... else ==== The conditional statement has the form <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>if</code> ''boolean expression'' <code>then</code> ''statement'' </div> which can optionally be followed by <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>else</code> ''statement'' </div> For example, the following conditional statement ensures that the value of <math>n</math>, assumed to be numerical, is positive. (It effectively implements the [[Absolute value (algebra)|absolute value]] function.) <syntaxhighlight lang="octave"> if n < 0 then n := -n </syntaxhighlight> The following conditional statement, used as an expression, avoids an error that would be caused by dividing by 0. <syntaxhighlight lang="octave"> recip_x := if x = 0 then infinity else 1/x </syntaxhighlight> ==== 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> ==== Repetition statements: while ... do; repeat ... until ==== The two loop statements <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>while</code> ''boolean expression'' <code>do</code> ''statement''<br /><code>repeat</code> ''statement'' <code>until</code> ''boolean expression'' </div> are closely related to the conditional statement and execute ''statement'' repeatedly a number of times that need not be known in advance. Their difference is that <code>while</code> repetition stops when ''boolean expression'' becomes false whereas <code>repeat</code> repetition stops when ''boolean expression'' becomes true. Also, <code>repeat</code> always executes ''statement'' at least once and it can be used to initialize ''boolean expression'', whereas when using <code>while</code> ''boolean expression'' must be initialized before entering the loop. The following <code>while</code> statement computes the value of <math>n!</math> as the value of the variable <code>fac</code>. Note that this code treats the assignment <code>n := n - 1</code> as an expression and uses its value. <syntaxhighlight lang="octave" copy> n := 5; fac := n$ while n > 1 do fac := fac*(n := n - 1); fac; </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)