~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to doc/interpreter/HTML/Operator-Precedence.html

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
3
<title>Operator Precedence - Untitled</title>
 
4
<meta http-equiv="Content-Type" content="text/html">
 
5
<meta name="description" content="Untitled">
 
6
<meta name="generator" content="makeinfo 4.11">
 
7
<link title="Top" rel="start" href="index.html#Top">
 
8
<link rel="up" href="Expressions.html#Expressions" title="Expressions">
 
9
<link rel="prev" href="Increment-Ops.html#Increment-Ops" title="Increment Ops">
 
10
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
 
11
<meta http-equiv="Content-Style-Type" content="text/css">
 
12
<style type="text/css"><!--
 
13
  pre.display { font-family:inherit }
 
14
  pre.format  { font-family:inherit }
 
15
  pre.smalldisplay { font-family:inherit; font-size:smaller }
 
16
  pre.smallformat  { font-family:inherit; font-size:smaller }
 
17
  pre.smallexample { font-size:smaller }
 
18
  pre.smalllisp    { font-size:smaller }
 
19
  span.sc    { font-variant:small-caps }
 
20
  span.roman { font-family:serif; font-weight:normal; } 
 
21
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
 
22
--></style>
 
23
</head>
 
24
<body>
 
25
<div class="node">
 
26
<p>
 
27
<a name="Operator-Precedence"></a>
 
28
Previous:&nbsp;<a rel="previous" accesskey="p" href="Increment-Ops.html#Increment-Ops">Increment Ops</a>,
 
29
Up:&nbsp;<a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a>
 
30
<hr>
 
31
</div>
 
32
 
 
33
<h3 class="section">8.8 Operator Precedence</h3>
 
34
 
 
35
<p><a name="index-operator-precedence-521"></a>
 
36
<dfn>Operator precedence</dfn> determines how operators are grouped, when
 
37
different operators appear close by in one expression.  For example,
 
38
&lsquo;<samp><span class="samp">*</span></samp>&rsquo; has higher precedence than &lsquo;<samp><span class="samp">+</span></samp>&rsquo;.  Thus, the expression
 
39
<code>a + b * c</code> means to multiply <code>b</code> and <code>c</code>, and then add
 
40
<code>a</code> to the product (i.e., <code>a + (b * c)</code>).
 
41
 
 
42
   <p>You can overrule the precedence of the operators by using parentheses. 
 
43
You can think of the precedence rules as saying where the parentheses
 
44
are assumed if you do not write parentheses yourself.  In fact, it is
 
45
wise to use parentheses whenever you have an unusual combination of
 
46
operators, because other people who read the program may not remember
 
47
what the precedence is in this case.  You might forget as well, and then
 
48
you too could make a mistake.  Explicit parentheses will help prevent
 
49
any such mistake.
 
50
 
 
51
   <p>When operators of equal precedence are used together, the leftmost
 
52
operator groups first, except for the assignment and exponentiation
 
53
operators, which group in the opposite order.  Thus, the expression
 
54
<code>a - b + c</code> groups as <code>(a - b) + c</code>, but the expression
 
55
<code>a = b = c</code> groups as <code>a = (b = c)</code>.
 
56
 
 
57
   <p>The precedence of prefix unary operators is important when another
 
58
operator follows the operand.  For example, <code>-x^2</code> means
 
59
<code>-(x^2)</code>, because &lsquo;<samp><span class="samp">-</span></samp>&rsquo; has lower precedence than &lsquo;<samp><span class="samp">^</span></samp>&rsquo;.
 
60
 
 
61
   <p>Here is a table of the operators in Octave, in order of increasing
 
62
precedence.
 
63
 
 
64
     <dl>
 
65
<dt><code>statement separators</code><dd>&lsquo;<samp><span class="samp">;</span></samp>&rsquo;, &lsquo;<samp><span class="samp">,</span></samp>&rsquo;.
 
66
 
 
67
     <br><dt><code>assignment</code><dd>&lsquo;<samp><span class="samp">=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">+=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">-=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">*=</span></samp>&rsquo;,&lsquo;<samp><span class="samp">/=</span></samp>&rsquo;.  This operator
 
68
groups right to left.
 
69
 
 
70
     <br><dt><code>logical "or" and "and"</code><dd>&lsquo;<samp><span class="samp">||</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&amp;&amp;</span></samp>&rsquo;.
 
71
 
 
72
     <br><dt><code>element-wise "or" and "and"</code><dd>&lsquo;<samp><span class="samp">|</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&amp;</span></samp>&rsquo;.
 
73
 
 
74
     <br><dt><code>relational</code><dd>&lsquo;<samp><span class="samp">&lt;</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&lt;=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">==</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&gt;=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&gt;</span></samp>&rsquo;, &lsquo;<samp><span class="samp">!=</span></samp>&rsquo;,
 
75
&lsquo;<samp><span class="samp">~=</span></samp>&rsquo;, &lsquo;<samp><span class="samp">&lt;&gt;</span></samp>&rsquo;.
 
76
 
 
77
     <br><dt><code>colon</code><dd>&lsquo;<samp><span class="samp">:</span></samp>&rsquo;.
 
78
 
 
79
     <br><dt><code>add, subtract</code><dd>&lsquo;<samp><span class="samp">+</span></samp>&rsquo;, &lsquo;<samp><span class="samp">-</span></samp>&rsquo;.
 
80
 
 
81
     <br><dt><code>multiply, divide</code><dd>&lsquo;<samp><span class="samp">*</span></samp>&rsquo;, &lsquo;<samp><span class="samp">/</span></samp>&rsquo;, &lsquo;<samp><span class="samp">\</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.\</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.*</span></samp>&rsquo;, &lsquo;<samp><span class="samp">./</span></samp>&rsquo;.
 
82
 
 
83
     <br><dt><code>transpose</code><dd>&lsquo;<samp><span class="samp">'</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.'</span></samp>&rsquo;
 
84
 
 
85
     <br><dt><code>unary plus, minus, increment, decrement, and ``not''</code><dd>&lsquo;<samp><span class="samp">+</span></samp>&rsquo;, &lsquo;<samp><span class="samp">-</span></samp>&rsquo;, &lsquo;<samp><span class="samp">++</span></samp>&rsquo;, &lsquo;<samp><span class="samp">--</span></samp>&rsquo;, &lsquo;<samp><span class="samp">!</span></samp>&rsquo;, &lsquo;<samp><span class="samp">~</span></samp>&rsquo;.
 
86
 
 
87
     <br><dt><code>exponentiation</code><dd>&lsquo;<samp><span class="samp">^</span></samp>&rsquo;, &lsquo;<samp><span class="samp">**</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.^</span></samp>&rsquo;, &lsquo;<samp><span class="samp">.**</span></samp>&rsquo;. 
 
88
</dl>
 
89
 
 
90
<!-- DO NOT EDIT!  Generated automatically by munge-texi. -->
 
91
<!-- Copyright (C) 1996, 1997, 2007 John W. Eaton -->
 
92
<!-- This file is part of Octave. -->
 
93
<!-- Octave is free software; you can redistribute it and/or modify it -->
 
94
<!-- under the terms of the GNU General Public License as published by the -->
 
95
<!-- Free Software Foundation; either version 3 of the License, or (at -->
 
96
<!-- your option) any later version. -->
 
97
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
 
98
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
 
99
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
 
100
<!-- for more details. -->
 
101
<!-- You should have received a copy of the GNU General Public License -->
 
102
<!-- along with Octave; see the file COPYING.  If not, see -->
 
103
<!-- <http://www.gnu.org/licenses/>. -->
 
104
   </body></html>
 
105