annotate doc/interpreter/stmt.txi @ 20606:1b62fc4e1b2f stable

doc: Cuddle parentheses in example code of for loop. * stmt.txi: Cuddle parentheses in example code of for loop.
author Rik <rik@octave.org>
date Wed, 07 Oct 2015 14:41:39 -0700
parents 4197fc428c7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19631
diff changeset
1 @c Copyright (C) 1996-2015 John W. Eaton
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
4 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
5 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
6 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
7 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
8 @c your option) any later version.
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
9 @c
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
13 @c for more details.
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
14 @c
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
16 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
17 @c <http://www.gnu.org/licenses/>.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3489
diff changeset
19 @node Statements
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
20 @chapter Statements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
21 @cindex statements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
22
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
23 Statements may be a simple constant expression or a complicated list of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
24 nested loops and conditional statements.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
25
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
26 @dfn{Control statements} such as @code{if}, @code{while}, and so on
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
27 control the flow of execution in Octave programs. All the control
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
28 statements start with special keywords such as @code{if} and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
29 @code{while}, to distinguish them from simple expressions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
30 Many control statements contain other statements; for example, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
31 @code{if} statement contains another statement which may or may not be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
32 executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
33
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
34 @cindex @code{end} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
35 Each control statement has a corresponding @dfn{end} statement that
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 7018
diff changeset
36 marks the end of the control statement. For example, the
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
37 keyword @code{endif} marks the end of an @code{if} statement, and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
38 @code{endwhile} marks the end of a @code{while} statement. You can use
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
39 the keyword @code{end} anywhere a more specific end keyword is expected,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
40 but using the more specific keywords is preferred because if you use
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
41 them, Octave is able to provide better diagnostics for mismatched or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
42 missing end tokens.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
43
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
44 The list of statements contained between keywords like @code{if} or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
45 @code{while} and the corresponding end statement is called the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
46 @dfn{body} of a control statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
47
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
48 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
49 * The if Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
50 * The switch Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
51 * The while Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
52 * The do-until Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
53 * The for Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
54 * The break Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
55 * The continue Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
56 * The unwind_protect Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
57 * The try Statement::
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
58 * Continuation Lines::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
59 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
60
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
61 @node The if Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
62 @section The if Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
63 @cindex @code{if} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
64 @cindex @code{else} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
65 @cindex @code{elseif} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
66 @cindex @code{endif} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
67
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
68 The @code{if} statement is Octave's decision-making statement. There
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
69 are three basic forms of an @code{if} statement. In its simplest form,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
70 it looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
71
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
72 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
73 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
74 if (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
75 @var{then-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
76 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
77 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
78 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
79
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
80 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
81 @var{condition} is an expression that controls what the rest of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 statement will do. The @var{then-body} is executed only if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
83 @var{condition} is true.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
84
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85 The condition in an @code{if} statement is considered true if its value
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
86 is nonzero, and false if its value is zero. If the value of the
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
87 conditional expression in an @code{if} statement is a vector or a
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
88 matrix, it is considered true only if it is non-empty and @emph{all}
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
89 of the elements are nonzero.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
90
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
91 The second form of an if statement looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
92
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
93 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 if (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96 @var{then-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98 @var{else-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
103 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
104 If @var{condition} is true, @var{then-body} is executed; otherwise,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
105 @var{else-body} is executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
106
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
107 Here is an example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
108
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112 printf ("x is even\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114 printf ("x is odd\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 In this example, if the expression @code{rem (x, 2) == 0} is true (that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120 is, the value of @code{x} is divisible by 2), then the first
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121 @code{printf} statement is evaluated, otherwise the second @code{printf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 statement is evaluated.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 The third and most general form of the @code{if} statement allows
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 multiple decisions to be combined in a single statement. It looks like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126 this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
129 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
130 if (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
131 @var{then-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132 elseif (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133 @var{elseif-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 @var{else-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141 Any number of @code{elseif} clauses may appear. Each condition is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
142 tested in turn, and if one is found to be true, its corresponding
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143 @var{body} is executed. If none of the conditions are true and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
144 @code{else} clause is present, its body is executed. Only one
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145 @code{else} clause may appear, and it must be the last part of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146 statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
148 In the following example, if the first condition is true (that is, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149 value of @code{x} is divisible by 2), then the first @code{printf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
150 statement is executed. If it is false, then the second condition is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
151 tested, and if it is true (that is, the value of @code{x} is divisible
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
152 by 3), then the second @code{printf} statement is executed. Otherwise,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
153 the third @code{printf} statement is performed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
154
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
155 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
156 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
157 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
158 printf ("x is even\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
159 elseif (rem (x, 3) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
160 printf ("x is odd and divisible by 3\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
162 printf ("x is odd\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
164 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
165 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
166
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
167 Note that the @code{elseif} keyword must not be spelled @code{else if},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
168 as is allowed in Fortran. If it is, the space between the @code{else}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
169 and @code{if} will tell Octave to treat this as a new @code{if}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
170 statement within another @code{if} statement's @code{else} clause. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
171 example, if you write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
172
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
173 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
174 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
175 if (@var{c1})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
176 @var{body-1}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
177 else if (@var{c2})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178 @var{body-2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
179 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
180 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
181 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
182
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
183 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
184 Octave will expect additional input to complete the first @code{if}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
185 statement. If you are using Octave interactively, it will continue to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
186 prompt you for additional input. If Octave is reading this input from a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
187 file, it may complain about missing or mismatched @code{end} statements,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
188 or, if you have not used the more specific @code{end} statements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
189 (@code{endif}, @code{endfor}, etc.), it may simply produce incorrect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
190 results, without producing any warning messages.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
191
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
192 It is much easier to see the error if we rewrite the statements above
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
193 like this,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
194
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
195 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
196 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
197 if (@var{c1})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
198 @var{body-1}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
199 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
200 if (@var{c2})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
201 @var{body-2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
202 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
203 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
204 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
205
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
206 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
207 using the indentation to show how Octave groups the statements.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
208 @xref{Functions and Scripts}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
209
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
210 @node The switch Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
211 @section The switch Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
212 @cindex @code{switch} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
213 @cindex @code{case} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
214 @cindex @code{otherwise} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
215 @cindex @code{endswitch} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
216
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
217 It is very common to take different actions depending on the value of
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
218 one variable. This is possible using the @code{if} statement in the
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
219 following way
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
220
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
221 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
222 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
223 if (X == 1)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
224 do_something ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
225 elseif (X == 2)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
226 do_something_else ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
227 else
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
228 do_something_completely_different ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
229 endif
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
230 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
231 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
232
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
233 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
234 This kind of code can however be very cumbersome to both write and
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
235 maintain. To overcome this problem Octave supports the @code{switch}
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
236 statement. Using this statement, the above example becomes
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
237
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
238 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
239 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
240 switch (X)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
241 case 1
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
242 do_something ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
243 case 2
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
244 do_something_else ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
245 otherwise
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
246 do_something_completely_different ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
247 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
248 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
249 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
250
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
251 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
252 This code makes the repetitive structure of the problem more explicit,
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
253 making the code easier to read, and hence maintain. Also, if the
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 7018
diff changeset
254 variable @code{X} should change its name, only one line would need
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
255 changing compared to one line per case when @code{if} statements are
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
256 used.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
257
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
258 The general form of the @code{switch} statement is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
259
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
260 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
261 @group
16934
48f5b993b819 doc: Use parentheses around if, switch conditions in documentation.
Rik <rik@octave.org>
parents: 16826
diff changeset
262 switch (@var{expression})
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
263 case @var{label}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
264 @var{command_list}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265 case @var{label}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
266 @var{command_list}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
267 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
268
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
269 otherwise
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
270 @var{command_list}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271 endswitch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
275 @noindent
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
276 where @var{label} can be any expression. However, duplicate
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
277 @var{label} values are not detected, and only the @var{command_list}
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
278 corresponding to the first match will be executed. For the
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
279 @code{switch} statement to be meaningful at least one
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
280 @code{case @var{label} @var{command_list}} clause must be present,
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
281 while the @code{otherwise @var{command_list}} clause is optional.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
282
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
283 If @var{label} is a cell array the corresponding @var{command_list}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
284 is executed if @emph{any} of the elements of the cell array match
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
285 @var{expression}. As an example, the following program will print
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
286 @samp{Variable is either 6 or 7}.
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
287
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
288 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
289 @group
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
290 A = 7;
16934
48f5b993b819 doc: Use parentheses around if, switch conditions in documentation.
Rik <rik@octave.org>
parents: 16826
diff changeset
291 switch (A)
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
292 case @{ 6, 7 @}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
293 printf ("variable is either 6 or 7\n");
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
294 otherwise
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
295 printf ("variable is neither 6 nor 7\n");
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
296 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
297 @end group
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
298 @end example
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
299
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
300 As with all other specific @code{end} keywords, @code{endswitch} may be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
301 replaced by @code{end}, but you can get better diagnostics if you use
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
302 the specific forms.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
303
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
304 @c Strings can be matched
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
305
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
306 One advantage of using the @code{switch} statement compared to using
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
307 @code{if} statements is that the @var{label}s can be strings. If an
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
308 @code{if} statement is used it is @emph{not} possible to write
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
309
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
310 @example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
311 if (X == "a string") # This is NOT valid
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
312 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
313
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
314 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
315 since a character-to-character comparison between @code{X} and the
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
316 string will be made instead of evaluating if the strings are equal.
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
317 This special-case is handled by the @code{switch} statement, and it
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
318 is possible to write programs that look like this
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
319
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
320 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
321 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
322 switch (X)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
323 case "a string"
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
324 do_something
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
325 @dots{}
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
326 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
327 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
328 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
329
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
330 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
331 * Notes for the C Programmer::
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
332 @end menu
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
333
11255
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
334 @node Notes for the C Programmer
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
335 @subsection Notes for the C Programmer
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
336
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
337 The @code{switch} statement is also available in the widely used C
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
338 programming language. There are, however, some differences
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
339 between the statement in Octave and C
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
340
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
341 @itemize @bullet
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
342 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
343 Cases are exclusive, so they don't `fall through' as do the cases
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
344 in the @code{switch} statement of the C language.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
345
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
346 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
347 The @var{command_list} elements are not optional. Making the list
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
348 optional would have meant requiring a separator between the label and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
349 the command list. Otherwise, things like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
350
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
351 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
352 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
353 switch (foo)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
354 case (1) -2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
355 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
356 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
357 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
358
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
359 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
360 would produce surprising results, as would
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
361
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
362 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
363 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
364 switch (foo)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
365 case (1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
366 case (2)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
367 doit ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
368 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
369 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
370 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
371
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
372 @noindent
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
373 particularly for C programmers. If @code{doit()} should be executed if
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
374 @var{foo} is either @code{1} or @code{2}, the above code should be
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
375 written with a cell array like this
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
376
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
377 @example
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
378 @group
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
379 switch (foo)
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
380 case @{ 1, 2 @}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
381 doit ();
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
382 @dots{}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
383 @end group
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
384 @end example
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
385
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
386 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
387
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
388 @node The while Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
389 @section The while Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
390 @cindex @code{while} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
391 @cindex @code{endwhile} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
392 @cindex loop
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
393 @cindex body of a loop
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
394
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
395 In programming, a @dfn{loop} means a part of a program that is (or at least can
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
396 be) executed two or more times in succession.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
397
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
398 The @code{while} statement is the simplest looping statement in Octave.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
399 It repeatedly executes a statement as long as a condition is true. As
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
400 with the condition in an @code{if} statement, the condition in a
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
401 @code{while} statement is considered true if its value is nonzero, and
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
402 false if its value is zero. If the value of the conditional expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
403 in a @code{while} statement is a vector or a matrix, it is considered
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
404 true only if it is non-empty and @emph{all} of the elements are nonzero.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
405
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
406 Octave's @code{while} statement looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
407
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
408 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
409 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
410 while (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
411 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
412 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
413 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
414 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
415
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
416 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
417 Here @var{body} is a statement or list of statements that we call the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
418 @dfn{body} of the loop, and @var{condition} is an expression that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
419 controls how long the loop keeps running.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
420
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
421 The first thing the @code{while} statement does is test @var{condition}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
422 If @var{condition} is true, it executes the statement @var{body}. After
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
423 @var{body} has been executed, @var{condition} is tested again, and if it
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
424 is still true, @var{body} is executed again. This process repeats until
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
425 @var{condition} is no longer true. If @var{condition} is initially
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
426 false, the body of the loop is never executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
427
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
428 This example creates a variable @code{fib} that contains the first ten
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
429 elements of the Fibonacci sequence.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
430
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
431 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
432 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
433 fib = ones (1, 10);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
434 i = 3;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
435 while (i <= 10)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
436 fib (i) = fib (i-1) + fib (i-2);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
437 i++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
438 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
439 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
440 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
441
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
442 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
443 Here the body of the loop contains two statements.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
444
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
445 The loop works like this: first, the value of @code{i} is set to 3.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
446 Then, the @code{while} tests whether @code{i} is less than or equal to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
447 10. This is the case when @code{i} equals 3, so the value of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
448 @code{i}-th element of @code{fib} is set to the sum of the previous two
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
449 values in the sequence. Then the @code{i++} increments the value of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
450 @code{i} and the loop repeats. The loop terminates when @code{i}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
451 reaches 11.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
452
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
453 A newline is not required between the condition and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
454 body; but using one makes the program clearer unless the body is very
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
455 simple.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
456
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
457 @node The do-until Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
458 @section The do-until Statement
3489
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
459 @cindex @code{do-until} statement
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
460
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
461 The @code{do-until} statement is similar to the @code{while} statement,
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
462 except that it repeatedly executes a statement until a condition becomes
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
463 true, and the test of the condition is at the end of the loop, so the
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
464 body of the loop is always executed at least once. As with the
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
465 condition in an @code{if} statement, the condition in a @code{do-until}
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
466 statement is considered true if its value is nonzero, and false if its
3489
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
467 value is zero. If the value of the conditional expression in a
19631
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19630
diff changeset
468 @code{do-until} statement is a vector or a matrix, it is considered
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18229
diff changeset
469 true only if it is non-empty and @emph{all} of the elements are nonzero.
3489
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
470
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
471 Octave's @code{do-until} statement looks like this:
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
472
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
473 @example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
474 @group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
475 do
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
476 @var{body}
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
477 until (@var{condition})
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
478 @end group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
479 @end example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
480
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
481 @noindent
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
482 Here @var{body} is a statement or list of statements that we call the
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
483 @dfn{body} of the loop, and @var{condition} is an expression that
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
484 controls how long the loop keeps running.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
485
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
486 This example creates a variable @code{fib} that contains the first ten
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
487 elements of the Fibonacci sequence.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
488
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
489 @example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
490 @group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
491 fib = ones (1, 10);
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
492 i = 2;
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
493 do
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
494 i++;
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
495 fib (i) = fib (i-1) + fib (i-2);
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
496 until (i == 10)
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
497 @end group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
498 @end example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
499
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
500 A newline is not required between the @code{do} keyword and the
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
501 body; but using one makes the program clearer unless the body is very
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
502 simple.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
503
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
504 @node The for Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
505 @section The for Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
506 @cindex @code{for} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
507 @cindex @code{endfor} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
508
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
509 The @code{for} statement makes it more convenient to count iterations of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
510 loop. The general form of the @code{for} statement looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
511
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
512 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
513 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
514 for @var{var} = @var{expression}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
515 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
516 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
517 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
518 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
519
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
520 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
521 where @var{body} stands for any statement or list of statements,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
522 @var{expression} is any valid expression, and @var{var} may take several
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
523 forms. Usually it is a simple variable name or an indexed variable. If
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
524 the value of @var{expression} is a structure, @var{var} may also be a
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
525 vector with two elements. @xref{Looping Over Structure Elements}, below.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
526
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
527 The assignment expression in the @code{for} statement works a bit
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
528 differently than Octave's normal assignment statement. Instead of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
529 assigning the complete result of the expression, it assigns each column
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
530 of the expression to @var{var} in turn. If @var{expression} is a range,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
531 a row vector, or a scalar, the value of @var{var} will be a scalar each
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
532 time the loop body is executed. If @var{var} is a column vector or a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
533 matrix, @var{var} will be a column vector each time the loop body is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
534 executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
535
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
536 The following example shows another way to create a vector containing
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
537 the first ten elements of the Fibonacci sequence, this time using the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
538 @code{for} statement:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
539
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
540 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
541 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
542 fib = ones (1, 10);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
543 for i = 3:10
20606
1b62fc4e1b2f doc: Cuddle parentheses in example code of for loop.
Rik <rik@octave.org>
parents: 19731
diff changeset
544 fib(i) = fib(i-1) + fib(i-2);
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
545 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
546 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
547 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
550 This code works by first evaluating the expression @code{3:10}, to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
551 produce a range of values from 3 to 10 inclusive. Then the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552 @code{i} is assigned the first element of the range and the body of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553 loop is executed once. When the end of the loop body is reached, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554 next value in the range is assigned to the variable @code{i}, and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
555 loop body is executed again. This process continues until there are no
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
556 more elements to assign.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
557
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
558 Within Octave is it also possible to iterate over matrices or cell arrays
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
559 using the @code{for} statement. For example consider
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
560
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
561 @example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
562 @group
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
563 disp ("Loop over a matrix")
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
564 for i = [1,3;2,4]
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
565 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
566 endfor
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
567 disp ("Loop over a cell array")
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
568 for i = @{1,"two";"three",4@}
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
569 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
570 endfor
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
571 @end group
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
572 @end example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
573
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
574 @noindent
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
575 In this case the variable @code{i} takes on the value of the columns of
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
576 the matrix or cell matrix. So the first loop iterates twice, producing
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
577 two column vectors @code{[1;2]}, followed by @code{[3;4]}, and likewise
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
578 for the loop over the cell array. This can be extended to loops over
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
579 multi-dimensional arrays. For example:
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
580
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
581 @example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
582 @group
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
583 a = [1,3;2,4]; c = cat (3, a, 2*a);
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
584 for i = c
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
585 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
586 endfor
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
587 @end group
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
588 @end example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
589
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
590 @noindent
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
591 In the above case, the multi-dimensional matrix @var{c} is reshaped to a
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
592 two-dimensional matrix as @code{reshape (c, rows (c),
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
593 prod (size (c)(2:end)))} and then the same behavior as a loop over a two
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
594 dimensional matrix is produced.
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
595
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
596 Although it is possible to rewrite all @code{for} loops as @code{while}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
597 loops, the Octave language has both statements because often a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
598 @code{for} loop is both less work to type and more natural to think of.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
599 Counting the number of iterations is very common in loops and it can be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
600 easier to think of this counting as part of looping rather than as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
601 something to do inside the loop.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
602
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
603 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
604 * Looping Over Structure Elements::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
605 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
606
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3489
diff changeset
607 @node Looping Over Structure Elements
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
608 @subsection Looping Over Structure Elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
609 @cindex structure elements, looping over
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
610 @cindex looping over structure elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
611
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
612 A special form of the @code{for} statement allows you to loop over all
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 the elements of a structure:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
614
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
615 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
616 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
617 for [ @var{val}, @var{key} ] = @var{expression}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
618 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
619 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
621 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
622
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
623 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
624 In this form of the @code{for} statement, the value of @var{expression}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
625 must be a structure. If it is, @var{key} and @var{val} are set to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
626 name of the element and the corresponding value in turn, until there are
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
627 no more elements. For example:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
628
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
629 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
630 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
631 x.a = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
632 x.b = [1, 2; 3, 4]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
633 x.c = "string"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
634 for [val, key] = x
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
635 key
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
636 val
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
637 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
638
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
639 @print{} key = a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
640 @print{} val = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
641 @print{} key = b
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
642 @print{} val =
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
643 @print{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
644 @print{} 1 2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
645 @print{} 3 4
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
646 @print{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
647 @print{} key = c
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
648 @print{} val = string
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
649 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
650 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
651
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
652 The elements are not accessed in any particular order. If you need to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
653 cycle through the list in a particular way, you will have to use the
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
654 function @code{fieldnames} and sort the list yourself.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
655
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
656 The @var{key} variable may also be omitted. If it is, the brackets are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
657 also optional. This is useful for cycling through the values of all the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
658 structure elements when the names of the elements do not need to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
659 known.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
661 @node The break Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
662 @section The break Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
663 @cindex @code{break} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
664
15898
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
665 The @code{break} statement jumps out of the innermost @code{while},
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
666 @code{do-until}, or @code{for} loop that encloses it. The @code{break}
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
667 statement may only be used within the body of a loop. The following
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
668 example finds the smallest divisor of a given integer, and also
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
669 identifies prime numbers:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
670
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
671 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
672 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
673 num = 103;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
674 div = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
675 while (div*div <= num)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
676 if (rem (num, div) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
677 break;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
678 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
679 div++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
680 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
681 if (rem (num, div) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
682 printf ("Smallest divisor of %d is %d\n", num, div)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
683 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
684 printf ("%d is prime\n", num);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
685 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
686 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
687 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689 When the remainder is zero in the first @code{while} statement, Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
690 immediately @dfn{breaks out} of the loop. This means that Octave
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
691 proceeds immediately to the statement following the loop and continues
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
692 processing. (This is very different from the @code{exit} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
693 which stops the entire Octave program.)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
694
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
695 Here is another program equivalent to the previous one. It illustrates
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
696 how the @var{condition} of a @code{while} statement could just as well
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
697 be replaced with a @code{break} inside an @code{if}:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
698
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
699 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
700 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
701 num = 103;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
702 div = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
703 while (1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
704 if (rem (num, div) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
705 printf ("Smallest divisor of %d is %d\n", num, div);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
706 break;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
707 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
708 div++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
709 if (div*div > num)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
710 printf ("%d is prime\n", num);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
711 break;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
712 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
713 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
714 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
715 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
716
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
717 @node The continue Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
718 @section The continue Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
719 @cindex @code{continue} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
720
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
721 The @code{continue} statement, like @code{break}, is used only inside
15898
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
722 @code{while}, @code{do-until}, or @code{for} loops. It skips over the
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
723 rest of the loop body, causing the next cycle around the loop to begin
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
724 immediately. Contrast this with @code{break}, which jumps out of the
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
725 loop altogether.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
726 Here is an example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
727
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
728 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
729 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
730 # print elements of a vector of random
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
731 # integers that are even.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
732
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
733 # first, create a row vector of 10 random
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
734 # integers with values between 0 and 100:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
735
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
736 vec = round (rand (1, 10) * 100);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
737
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
738 # print what we're interested in:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
739
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
740 for x = vec
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
741 if (rem (x, 2) != 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
742 continue;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
743 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
744 printf ("%d\n", x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
745 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
746 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
747 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
748
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
749 If one of the elements of @var{vec} is an odd number, this example skips
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
750 the print statement for that element, and continues back to the first
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
751 statement in the loop.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
752
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
753 This is not a practical example of the @code{continue} statement, but it
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
754 should give you a clear understanding of how it works. Normally, one
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
755 would probably write the loop like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
756
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
757 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
758 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
759 for x = vec
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
760 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
761 printf ("%d\n", x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
762 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
763 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
764 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
765 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
766
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
767 @node The unwind_protect Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
768 @section The unwind_protect Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
769 @cindex @code{unwind_protect} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
770 @cindex @code{unwind_protect_cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
771 @cindex @code{end_unwind_protect}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
772
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 15898
diff changeset
773 Octave supports a limited form of exception handling modeled after the
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
774 unwind-protect form of Lisp.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
775
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
776 The general form of an @code{unwind_protect} block looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
777
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
778 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
779 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
780 unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
781 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
782 unwind_protect_cleanup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
783 @var{cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
784 end_unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
785 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
786 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
787
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
788 @noindent
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
789 where @var{body} and @var{cleanup} are both optional and may contain any
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18229
diff changeset
790 Octave expressions or commands. The statements in @var{cleanup} are
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
791 guaranteed to be executed regardless of how control exits @var{body}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
792
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
793 This is useful to protect temporary changes to global variables from
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
794 possible errors. For example, the following code will always restore
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
795 the original value of the global variable @code{frobnosticate}
8542
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
796 even if an error occurs in the first part of the @code{unwind_protect}
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
797 block.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
798
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
799 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
800 @group
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
801 save_frobnosticate = frobnosticate;
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
802 unwind_protect
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
803 frobnosticate = true;
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
804 @dots{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
805 unwind_protect_cleanup
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
806 frobnosticate = save_frobnosticate;
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
807 end_unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
808 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
809 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
810
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
811 @noindent
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
812 Without @code{unwind_protect}, the value of @var{frobnosticate}
8542
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
813 would not be restored if an error occurs while evaluating the first part
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
814 of the @code{unwind_protect} block because evaluation would stop at the
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
815 point of the error and the statement to restore the value would not be
ef2dfe33e5b5 stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents: 8347
diff changeset
816 executed.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
817
18220
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
818 In addition to unwind_protect, Octave supports another form of
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
819 exception handling, the @code{try} block.
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
820
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
821
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
822 @node The try Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
823 @section The try Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
824 @cindex @code{try} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
825 @cindex @code{catch}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
826 @cindex @code{end_try_catch}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
827
18220
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
828 The original form of a @code{try} block looks like this:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
829
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
830 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
831 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
832 try
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
833 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
834 catch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
835 @var{cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
836 end_try_catch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
837 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
838 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
839
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
840 @noindent
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
841 where @var{body} and @var{cleanup} are both optional and may contain any
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
842 Octave expressions or commands. The statements in @var{cleanup} are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
843 only executed if an error occurs in @var{body}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
844
18220
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
845 No warnings or error messages are printed while @var{body} is executing.
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
846 If an error does occur during the execution of @var{body}, @var{cleanup}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
847 can use the functions @code{lasterr} or @code{lasterror} to access the
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
848 text of the message that would have been printed, as well as its
18229
faf32a725693 * stmt.txi: Fix missing comma after @xref.
John W. Eaton <jwe@octave.org>
parents: 18220
diff changeset
849 identifier. The alternative form,
18220
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
850
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
851 @example
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
852 @group
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
853 try
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
854 @var{body}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
855 catch @var{err}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
856 @var{cleanup}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
857 end_try_catch
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
858 @end group
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
859 @end example
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
860
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
861 @noindent
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
862 will automatically store the output of @code{lasterror} in the structure
18229
faf32a725693 * stmt.txi: Fix missing comma after @xref.
John W. Eaton <jwe@octave.org>
parents: 18220
diff changeset
863 @var{err}. @xref{Errors and Warnings}, for more information about the
18220
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
864 @code{lasterr} and @code{lasterror} functions.
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
865
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
866
17418
0bcbbd82c9b5 doc: Fix indexing of continuation lines
Stefan Mahr <dac922@gmx.de>
parents: 17259
diff changeset
867 @node Continuation Lines
0bcbbd82c9b5 doc: Fix indexing of continuation lines
Stefan Mahr <dac922@gmx.de>
parents: 17259
diff changeset
868 @section Continuation Lines
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
869 @cindex continuation lines
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
870 @cindex @code{...} continuation marker
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
871 @cindex @code{\} continuation marker
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
872
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
873 In the Octave language, most statements end with a newline character and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874 you must tell Octave to ignore the newline character in order to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 continue a statement from one line to the next. Lines that end with the
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
876 characters @code{...} are joined with the following line before they are
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
877 divided into tokens by Octave's parser. For example, the lines
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
879 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
880 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
881 x = long_variable_name ...
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
882 + longer_variable_name ...
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
883 - 42
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
887 @noindent
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
888 form a single statement.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
890 Any text between the continuation marker and the newline character is
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
891 ignored. For example, the statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
892
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
893 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894 @group
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
895 x = long_variable_name ... # comment one
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
896 + longer_variable_name ...comment two
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
897 - 42 # last comment
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
900
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
901 @noindent
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
902 is equivalent to the one shown above.
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
903
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
904 Inside double-quoted string constants, the character @code{\} has to be
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
905 used as continuation marker. The @code{\} must appear at the end of the
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
906 line just before the newline character:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
907
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
908 @example
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
909 @group
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
910 s = "This text starts in the first line \
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
911 and is continued in the second line."
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
912 @end group
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
913 @end example
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
914
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
915 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
916 Input that occurs inside parentheses can be continued to the next line
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
917 without having to use a continuation marker. For example, it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
918 possible to write statements like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
919
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
920 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
921 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
922 if (fine_dining_destination == on_a_boat
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
923 || fine_dining_destination == on_a_train)
4541
3774dc061cdc [project @ 2003-10-17 01:10:10 by jwe]
jwe
parents: 4455
diff changeset
924 seuss (i, will, not, eat, them, sam, i, am, i,
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
925 will, not, eat, green, eggs, and, ham);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
926 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
927 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
928 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
929
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
930 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
931 without having to add to the clutter with continuation markers.