Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 55

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Assignment Ops - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Expressions.html#Expressions" title="Expressions">
<link rel="prev" href="Boolean-Expressions.html#Boolean-Expressions" title="Boolean Expressions">
<link rel="next" href="Increment-Ops.html#Increment-Ops" title="Increment Ops">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Assignment-Ops"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Increment-Ops.html#Increment-Ops">Increment Ops</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Boolean-Expressions.html#Boolean-Expressions">Boolean Expressions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a>
<hr>
</div>

<h3 class="section">8.6 Assignment Expressions</h3>

<p><a name="index-assignment-expressions-526"></a><a name="index-assignment-operators-527"></a><a name="index-operators_002c-assignment-528"></a><a name="index-expressions_002c-assignment-529"></a>
<a name="index-g_t_003d-530"></a>
An <dfn>assignment</dfn> is an expression that stores a new value into a
variable.  For example, the following expression assigns the value 1 to
the variable <code>z</code>:

<pre class="example">     z = 1
</pre>
   <p class="noindent">After this expression is executed, the variable <code>z</code> has the value 1. 
Whatever old value <code>z</code> had before the assignment is forgotten. 
The &lsquo;<samp><span class="samp">=</span></samp>&rsquo; sign is called an <dfn>assignment operator</dfn>.

   <p>Assignments can store string values also.  For example, the following
expression would store the value <code>"this food is good"</code> in the
variable <code>message</code>:

<pre class="example">     thing = "food"
     predicate = "good"
     message = [ "this " , thing , " is " , predicate ]
</pre>
   <p class="noindent">(This also illustrates concatenation of strings.)

   <p><a name="index-side-effect-531"></a>Most operators (addition, concatenation, and so on) have no effect
except to compute a value.  If you ignore the value, you might as well
not use the operator.  An assignment operator is different.  It does
produce a value, but even if you ignore the value, the assignment still
makes itself felt through the alteration of the variable.  We call this
a <dfn>side effect</dfn>.

   <p><a name="index-lvalue-532"></a>The left-hand operand of an assignment need not be a variable
(see <a href="Variables.html#Variables">Variables</a>).  It can also be an element of a matrix
(see <a href="Index-Expressions.html#Index-Expressions">Index Expressions</a>) or a list of return values
(see <a href="Calling-Functions.html#Calling-Functions">Calling Functions</a>).  These are all called <dfn>lvalues</dfn>, which
means they can appear on the left-hand side of an assignment operator. 
The right-hand operand may be any expression.  It produces the new value
which the assignment stores in the specified variable, matrix element,
or list of return values.

   <p>It is important to note that variables do <em>not</em> have permanent types. 
The type of a variable is simply the type of whatever value it happens
to hold at the moment.  In the following program fragment, the variable
<code>foo</code> has a numeric value at first, and a string value later on:

<pre class="example">     octave:13&gt; foo = 1
     foo = 1
     octave:13&gt; foo = "bar"
     foo = bar
</pre>
   <p class="noindent">When the second assignment gives <code>foo</code> a string value, the fact that
it previously had a numeric value is forgotten.

   <p>Assignment of a scalar to an indexed matrix sets all of the elements
that are referenced by the indices to the scalar value.  For example, if
<code>a</code> is a matrix with at least two columns,

<pre class="example">     a(:, 2) = 5
</pre>
   <p class="noindent">sets all the elements in the second column of <code>a</code> to 5.

   <p>Assigning an empty matrix &lsquo;<samp><span class="samp">[]</span></samp>&rsquo; works in most cases to allow you to
delete rows or columns of matrices and vectors.  See <a href="Empty-Matrices.html#Empty-Matrices">Empty Matrices</a>. 
For example, given a 4 by 5 matrix <var>A</var>, the assignment

<pre class="example">     A (3, :) = []
</pre>
   <p class="noindent">deletes the third row of <var>A</var>, and the assignment

<pre class="example">     A (:, 1:2:5) = []
</pre>
   <p class="noindent">deletes the first, third, and fifth columns.

   <p>An assignment is an expression, so it has a value.  Thus, <code>z = 1</code>
as an expression has the value 1.  One consequence of this is that you
can write multiple assignments together:

<pre class="example">     x = y = z = 0
</pre>
   <p class="noindent">stores the value 0 in all three variables.  It does this because the
value of <code>z = 0</code>, which is 0, is stored into <code>y</code>, and then
the value of <code>y = z = 0</code>, which is 0, is stored into <code>x</code>.

   <p>This is also true of assignments to lists of values, so the following is
a valid expression

<pre class="example">     [a, b, c] = [u, s, v] = svd (a)
</pre>
   <p class="noindent">that is exactly equivalent to

<pre class="example">     [u, s, v] = svd (a)
     a = u
     b = s
     c = v
</pre>
   <p>In expressions like this, the number of values in each part of the
expression need not match.  For example, the expression

<pre class="example">     [a, b] = [u, s, v] = svd (a)
</pre>
   <p class="noindent">is equivalent to

<pre class="example">     [u, s, v] = svd (a)
     a = u
     b = s
</pre>
   <p class="noindent">The number of values on the left side of the expression can, however,
not exceed the number of values on the right side.  For example, the
following will produce an error.

<pre class="example">     [a, b, c, d] = [u, s, v] = svd (a);
     -| error: element number 4 undefined in return list
</pre>
   <p><a name="index-g_t_002b_003d-533"></a>A very common programming pattern is to increment an existing variable
with a given value, like this

<pre class="example">     a = a + 2;
</pre>
   <p class="noindent">This can be written in a clearer and more condensed form using the
<code>+=</code> operator

<pre class="example">     a += 2;
</pre>
   <p class="noindent"><a name="index-g_t_002d_003d-534"></a><a name="index-g_t_002a_003d-535"></a><a name="index-g_t_002f_003d-536"></a>Similar operators also exist for subtraction (<code>-=</code>),
multiplication (<code>*=</code>), and division (<code>/=</code>).  An expression
of the form

<pre class="example">     <var>expr1</var> <var>op</var>= <var>expr2</var>
</pre>
   <p class="noindent">is evaluated as

<pre class="example">     <var>expr1</var> = (<var>expr1</var>) <var>op</var> (<var>expr2</var>)
</pre>
   <p class="noindent">where <var>op</var> can be either <code>+</code>, <code>-</code>, <code>*</code>, or <code>/</code>. 
So, the expression

<pre class="example">     a *= b+1
</pre>
   <p class="noindent">is evaluated as

<pre class="example">     a = a * (b+1)
</pre>
   <p class="noindent">and <em>not</em>

<pre class="example">     a = a * b + 1
</pre>
   <p>You can use an assignment anywhere an expression is called for.  For
example, it is valid to write <code>x != (y = 1)</code> to set <code>y</code> to 1
and then test whether <code>x</code> equals 1.  But this style tends to make
programs hard to read.  Except in a one-shot program, you should rewrite
it to get rid of such nesting of assignments.  This is never very hard.

   <p><a name="index-increment-operator-537"></a><a name="index-decrement-operator-538"></a><a name="index-operators_002c-increment-539"></a><a name="index-operators_002c-decrement-540"></a>

   </body></html>