annotate doc/interpreter/stmt.txi @ 21122:dba88797f69f

doc: Periodic grammarcheck of documentation.
author Rik <rik@octave.org>
date Wed, 20 Jan 2016 21:14:47 -0800
parents fdffc955b4ed
children dcf8922b724b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19597
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.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
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.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
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
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18230
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}
20750
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
89 of the elements are nonzero. The conceptually equivalent code when
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
90 @var{condition} is a matrix is shown below.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
91
20750
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
92 @example
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
93 if (@var{matrix}) @equiv{} if (all (@var{matrix}(:)))
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
94 @end example
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
95
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
96 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97 The second form of an if statement looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101 if (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102 @var{then-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
103 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
104 @var{else-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
105 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
106 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
107 @end 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 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110 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
111 @var{else-body} is executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113 Here is an example:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118 printf ("x is even\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120 printf ("x is odd\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 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
126 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
127 @code{printf} statement is evaluated, otherwise the second @code{printf}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128 statement is evaluated.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
129
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
130 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
131 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
132 this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 if (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 @var{then-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
138 elseif (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139 @var{elseif-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
141 @var{else-body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
142 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
144 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147 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
148 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
149 @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
150 @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
151 @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
152 statement.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
153
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
154 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
155 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
156 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
157 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
158 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
159 the third @code{printf} statement is performed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
160
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
162 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
164 printf ("x is even\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
165 elseif (rem (x, 3) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
166 printf ("x is odd and divisible by 3\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
167 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
168 printf ("x is odd\n");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
169 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
170 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
171 @end example
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 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
174 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
175 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
176 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
177 example, if you write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
178
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
179 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
180 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
181 if (@var{c1})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
182 @var{body-1}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
183 else if (@var{c2})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
184 @var{body-2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
185 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
186 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
187 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
188
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
189 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
190 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
191 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
192 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
193 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
194 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
195 (@code{endif}, @code{endfor}, etc.), it may simply produce incorrect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
196 results, without producing any warning messages.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
197
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
198 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
199 like this,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
200
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
201 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
202 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
203 if (@var{c1})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
204 @var{body-1}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
205 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
206 if (@var{c2})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
207 @var{body-2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
208 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
209 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
210 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
211
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
212 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
213 using the indentation to show how Octave groups the statements.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
214 @xref{Functions and Scripts}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
215
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
216 @node The switch Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
217 @section The switch Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
218 @cindex @code{switch} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
219 @cindex @code{case} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
220 @cindex @code{otherwise} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
221 @cindex @code{endswitch} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
222
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
223 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
224 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
225 following way
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
226
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
227 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
228 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
229 if (X == 1)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
230 do_something ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
231 elseif (X == 2)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
232 do_something_else ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
233 else
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
234 do_something_completely_different ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
235 endif
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
236 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
237 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
238
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
239 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
240 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
241 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
242 statement. Using this statement, the above example becomes
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
243
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
244 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
245 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
246 switch (X)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
247 case 1
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
248 do_something ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
249 case 2
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
250 do_something_else ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
251 otherwise
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
252 do_something_completely_different ();
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
253 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
254 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
255 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
256
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
257 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
258 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
259 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
260 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
261 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
262 used.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
263
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
264 The general form of the @code{switch} statement is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
266 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
267 @group
16934
48f5b993b819 doc: Use parentheses around if, switch conditions in documentation.
Rik <rik@octave.org>
parents: 16826
diff changeset
268 switch (@var{expression})
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
269 case @var{label}
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 case @var{label}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272 @var{command_list}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 otherwise
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276 @var{command_list}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
277 endswitch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
278 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
280
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
281 @noindent
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
282 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
283 @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
284 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
285 @code{switch} statement to be meaningful at least one
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
286 @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
287 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
288
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
289 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
290 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
291 @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
292 @samp{Variable is either 6 or 7}.
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
293
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
294 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
295 @group
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
296 A = 7;
16934
48f5b993b819 doc: Use parentheses around if, switch conditions in documentation.
Rik <rik@octave.org>
parents: 16826
diff changeset
297 switch (A)
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
298 case @{ 6, 7 @}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
299 printf ("variable is either 6 or 7\n");
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
300 otherwise
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
301 printf ("variable is neither 6 nor 7\n");
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
302 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
303 @end group
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
304 @end example
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
305
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
306 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
307 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
308 the specific forms.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
309
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
310 @c Strings can be matched
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
311
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
312 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
313 @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
314 @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
315
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
316 @example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
317 if (X == "a string") # This is NOT valid
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
318 @end example
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 @noindent
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
321 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
322 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
323 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
324 is possible to write programs that look like this
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
325
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
326 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
327 @group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
328 switch (X)
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
329 case "a string"
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
330 do_something
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
331 @dots{}
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
332 endswitch
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
333 @end group
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
334 @end example
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
335
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
336 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
337 * Notes for the C Programmer::
6535
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
338 @end menu
3ef1aa12f04c [project @ 2007-04-18 16:17:25 by jwe]
jwe
parents: 6530
diff changeset
339
11255
d682cd6669ac Update info-based documentation menus to include new nodes.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
340 @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
341 @subsection Notes for the C Programmer
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
342
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
343 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
344 programming language. There are, however, some differences
6530
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
345 between the statement in Octave and C
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
346
f80cc454860d [project @ 2007-04-16 23:16:58 by jwe]
jwe
parents: 6501
diff changeset
347 @itemize @bullet
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
348 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
349 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
350 in the @code{switch} statement of the C language.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
351
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
352 @item
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
353 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
354 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
355 the command list. Otherwise, things like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
356
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
357 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
358 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
359 switch (foo)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
360 case (1) -2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
361 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
362 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
363 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
364
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
365 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
366 would produce surprising results, as would
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
367
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
368 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
369 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
370 switch (foo)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
371 case (1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
372 case (2)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
373 doit ();
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
374 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
375 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
376 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
377
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
378 @noindent
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
379 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
380 @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
381 written with a cell array like this
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
382
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
383 @example
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
384 @group
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
385 switch (foo)
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
386 case @{ 1, 2 @}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
387 doit ();
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
388 @dots{}
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
389 @end group
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
390 @end example
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
391
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
392 @end itemize
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
393
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
394 @node The while Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
395 @section The while Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
396 @cindex @code{while} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
397 @cindex @code{endwhile} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
398 @cindex loop
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
399 @cindex body of a loop
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
400
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
401 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
402 be) executed two or more times in succession.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
403
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
404 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
405 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
406 with the condition in an @code{if} statement, the condition in a
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18230
diff changeset
407 @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
408 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
409 in a @code{while} statement is a vector or a matrix, it is considered
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18230
diff changeset
410 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
411
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
412 Octave's @code{while} statement looks like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
413
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
414 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
415 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
416 while (@var{condition})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
417 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
418 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
419 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
420 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
421
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
422 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
423 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
424 @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
425 controls how long the loop keeps running.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
426
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
427 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
428 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
429 @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
430 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
431 @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
432 false, the body of the loop is never executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
433
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
434 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
435 elements of the Fibonacci sequence.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
436
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
437 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
438 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
439 fib = ones (1, 10);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
440 i = 3;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
441 while (i <= 10)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
442 fib (i) = fib (i-1) + fib (i-2);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
443 i++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
444 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
445 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
446 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
447
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
448 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
449 Here the body of the loop contains two statements.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
450
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
451 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
452 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
453 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
454 @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
455 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
456 @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
457 reaches 11.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
458
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
459 A newline is not required between the condition and the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
460 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
461 simple.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
462
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
463 @node The do-until Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
464 @section The do-until Statement
3489
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
465 @cindex @code{do-until} statement
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
466
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
467 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
468 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
469 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
470 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
471 condition in an @code{if} statement, the condition in a @code{do-until}
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18230
diff changeset
472 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
473 value is zero. If the value of the conditional expression in a
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19596
diff changeset
474 @code{do-until} statement is a vector or a matrix, it is considered
18812
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18230
diff changeset
475 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
476
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
477 Octave's @code{do-until} statement looks like this:
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
478
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
479 @example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
480 @group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
481 do
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
482 @var{body}
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
483 until (@var{condition})
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
484 @end group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
485 @end example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
486
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
487 @noindent
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
488 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
489 @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
490 controls how long the loop keeps running.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
491
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
492 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
493 elements of the Fibonacci sequence.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
494
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
495 @example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
496 @group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
497 fib = ones (1, 10);
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
498 i = 2;
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
499 do
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
500 i++;
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
501 fib (i) = fib (i-1) + fib (i-2);
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
502 until (i == 10)
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
503 @end group
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
504 @end example
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
505
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
506 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
507 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
508 simple.
cbee5fbb696d [project @ 2000-01-28 09:14:32 by jwe]
jwe
parents: 3402
diff changeset
509
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
510 @node The for Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
511 @section The for Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
512 @cindex @code{for} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
513 @cindex @code{endfor} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
514
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
515 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
516 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
517
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
518 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
519 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
520 for @var{var} = @var{expression}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
521 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
522 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
523 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
524 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
525
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
526 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
527 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
528 @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
529 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
530 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
531 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
532
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
533 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
534 differently than Octave's normal assignment statement. Instead of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
535 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
536 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
537 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
538 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
539 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
540 executed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
541
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
542 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
543 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
544 @code{for} statement:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
545
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
546 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
547 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548 fib = ones (1, 10);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549 for i = 3:10
20571
1b62fc4e1b2f doc: Cuddle parentheses in example code of for loop.
Rik <rik@octave.org>
parents: 19697
diff changeset
550 fib(i) = fib(i-1) + fib(i-2);
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
551 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
555 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
556 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
557 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
558 @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
559 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
560 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
561 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
562 more elements to assign.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
563
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
564 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
565 using the @code{for} statement. For example consider
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
566
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
567 @example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
568 @group
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
569 disp ("Loop over a matrix")
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
570 for i = [1,3;2,4]
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
571 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
572 endfor
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
573 disp ("Loop over a cell array")
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
574 for i = @{1,"two";"three",4@}
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
575 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
576 endfor
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
577 @end group
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
578 @end example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
579
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
580 @noindent
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
581 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
582 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
583 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
584 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
585 multi-dimensional arrays. For example:
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
586
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
587 @example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
588 @group
14856
c3fd61c59e9c maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
589 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
590 for i = c
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
591 i
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
592 endfor
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
593 @end group
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
594 @end example
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
595
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
596 @noindent
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 9209
diff changeset
597 In the above case, the multi-dimensional matrix @var{c} is reshaped to a
20750
fdffc955b4ed doc: Clarify how if statement determines true or false for a matrix input.
Rik <rik@octave.org>
parents: 20571
diff changeset
598 two-dimensional matrix as @code{reshape (c, rows (c), prod (size (c)(2:end)))}
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20750
diff changeset
599 and then the same behavior as a loop over a two-dimensional matrix is produced.
6587
00fad3bad2a5 [project @ 2007-04-26 20:23:31 by dbateman]
dbateman
parents: 6535
diff changeset
600
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
601 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
602 loops, the Octave language has both statements because often a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
603 @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
604 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
605 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
606 something to do inside the loop.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
607
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
608 @menu
17152
f2a8592b8fbd doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents: 16934
diff changeset
609 * Looping Over Structure Elements::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
610 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
611
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3489
diff changeset
612 @node Looping Over Structure Elements
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 @subsection Looping Over Structure Elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
614 @cindex structure elements, looping over
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
615 @cindex looping over structure elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
616
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
617 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
618 the elements of a structure:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
619
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
621 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
622 for [ @var{val}, @var{key} ] = @var{expression}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
623 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
624 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
625 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
626 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
627
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
628 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
629 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
630 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
631 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
632 no more elements. For example:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
633
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
634 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
635 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
636 x.a = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
637 x.b = [1, 2; 3, 4]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
638 x.c = "string"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
639 for [val, key] = x
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
640 key
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
641 val
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
642 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
643
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
644 @print{} key = a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
645 @print{} val = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
646 @print{} key = b
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
647 @print{} val =
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
648 @print{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
649 @print{} 1 2
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
650 @print{} 3 4
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
651 @print{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
652 @print{} key = c
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
653 @print{} val = string
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
654 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
655 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
656
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
657 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
658 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
659 function @code{fieldnames} and sort the list yourself.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
661 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
662 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
663 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
664 known.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
665
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
666 @node The break Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
667 @section The break Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
668 @cindex @code{break} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
669
15898
a7d89366d7ed Document that break and continue statements work for do-until loops.
Rik <rik@octave.org>
parents: 15685
diff changeset
670 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
671 @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
672 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
673 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
674 identifies prime numbers:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
675
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
676 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
677 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
678 num = 103;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
679 div = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
680 while (div*div <= num)
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 break;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
683 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
684 div++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
685 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
686 if (rem (num, div) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
687 printf ("Smallest divisor of %d is %d\n", num, div)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689 printf ("%d is prime\n", num);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
690 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
691 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
692 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
693
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
694 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
695 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
696 proceeds immediately to the statement following the loop and continues
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
697 processing. (This is very different from the @code{exit} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
698 which stops the entire Octave program.)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
699
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
700 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
701 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
702 be replaced with a @code{break} inside an @code{if}:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
703
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
704 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
705 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
706 num = 103;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
707 div = 2;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
708 while (1)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
709 if (rem (num, div) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
710 printf ("Smallest divisor of %d is %d\n", num, div);
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 div++;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
714 if (div*div > num)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
715 printf ("%d is prime\n", num);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
716 break;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
717 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
718 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
719 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
720 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
721
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
722 @node The continue Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
723 @section The continue Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
724 @cindex @code{continue} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
725
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
726 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
727 @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
728 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
729 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
730 loop altogether.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
731 Here is an example:
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 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
734 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
735 # print elements of a vector of random
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
736 # integers that are even.
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 # first, create a row vector of 10 random
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
739 # integers with values between 0 and 100:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
740
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
741 vec = round (rand (1, 10) * 100);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
742
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
743 # print what we're interested in:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
744
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
745 for x = vec
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
746 if (rem (x, 2) != 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
747 continue;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
748 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
749 printf ("%d\n", x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
750 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
751 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
752 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
753
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
754 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
755 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
756 statement in the loop.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
757
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
758 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
759 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
760 would probably write the loop like this:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
761
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
762 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
763 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
764 for x = vec
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
765 if (rem (x, 2) == 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
766 printf ("%d\n", x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
767 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
768 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
769 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
770 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
771
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
772 @node The unwind_protect Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
773 @section The unwind_protect Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
774 @cindex @code{unwind_protect} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
775 @cindex @code{unwind_protect_cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
776 @cindex @code{end_unwind_protect}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
777
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 15898
diff changeset
778 Octave supports a limited form of exception handling modeled after the
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
779 unwind-protect form of Lisp.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
780
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
781 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
782
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
783 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
784 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
785 unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
786 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
787 unwind_protect_cleanup
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
788 @var{cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
789 end_unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
790 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
791 @end example
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 @noindent
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
794 where @var{body} and @var{cleanup} are both optional and may contain any
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18230
diff changeset
795 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
796 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
797
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
798 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
799 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
800 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
801 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
802 block.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
803
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
804 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
805 @group
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
806 save_frobnosticate = frobnosticate;
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
807 unwind_protect
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
808 frobnosticate = true;
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
809 @dots{}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
810 unwind_protect_cleanup
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
811 frobnosticate = save_frobnosticate;
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
812 end_unwind_protect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
813 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
814 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
815
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
816 @noindent
6501
68f3125f6e27 [project @ 2007-04-05 20:53:40 by jwe]
jwe
parents: 5041
diff changeset
817 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
818 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
819 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
820 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
821 executed.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
822
18221
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
823 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
824 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
825
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
826
15684
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
827 @node The try Statement
ddc651eecf7a Fix Info index for language statements (bug #37787)
Rik <rik@octave.org>
parents: 14138
diff changeset
828 @section The try Statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
829 @cindex @code{try} statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
830 @cindex @code{catch}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
831 @cindex @code{end_try_catch}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
832
18221
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
833 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
834
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
835 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
836 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
837 try
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
838 @var{body}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
839 catch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
840 @var{cleanup}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
841 end_try_catch
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
842 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
843 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
844
6637
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
845 @noindent
c18ed0e7ee41 [project @ 2007-05-21 19:12:46 by jwe]
jwe
parents: 6587
diff changeset
846 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
847 Octave expressions or commands. The statements in @var{cleanup} are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
848 only executed if an error occurs in @var{body}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
849
18221
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
850 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
851 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
852 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
853 text of the message that would have been printed, as well as its
18230
faf32a725693 * stmt.txi: Fix missing comma after @xref.
John W. Eaton <jwe@octave.org>
parents: 18221
diff changeset
854 identifier. The alternative form,
18221
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
855
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
856 @example
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
857 @group
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
858 try
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
859 @var{body}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
860 catch @var{err}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
861 @var{cleanup}
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
862 end_try_catch
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
863 @end group
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
864 @end example
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
865
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
866 @noindent
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
867 will automatically store the output of @code{lasterror} in the structure
18230
faf32a725693 * stmt.txi: Fix missing comma after @xref.
John W. Eaton <jwe@octave.org>
parents: 18221
diff changeset
868 @var{err}. @xref{Errors and Warnings}, for more information about the
18221
6fd22474783e doc: Update manual for "catch err" syntax (bug #33217)
Felipe G. Nievinski <fgnievinski@gmail.com>
parents: 17744
diff changeset
869 @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
870
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
871
17418
0bcbbd82c9b5 doc: Fix indexing of continuation lines
Stefan Mahr <dac922@gmx.de>
parents: 17259
diff changeset
872 @node Continuation Lines
0bcbbd82c9b5 doc: Fix indexing of continuation lines
Stefan Mahr <dac922@gmx.de>
parents: 17259
diff changeset
873 @section Continuation Lines
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874 @cindex continuation lines
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 @cindex @code{...} continuation marker
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
876 @cindex @code{\} continuation marker
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
877
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878 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
879 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
880 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
881 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
882 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
883
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886 x = long_variable_name ...
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
887 + longer_variable_name ...
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
888 - 42
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
890 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
891
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
892 @noindent
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
893 form a single statement.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
895 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
896 ignored. For example, the statement
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
897
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 @group
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
900 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
901 + longer_variable_name ...comment two
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
902 - 42 # last comment
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
903 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
904 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
905
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
906 @noindent
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
907 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
908
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
909 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
910 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
911 line just before the newline character:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
912
17259
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
913 @example
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
914 @group
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
915 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
916 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
917 @end group
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
918 @end example
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
919
a654c1eccd29 doc: Modify chapter 'continuation lines' to fit actual behaviour
Stefan Mahr <dac922@gmx.de>
parents: 17152
diff changeset
920 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
921 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
922 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
923 possible to write statements like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
924
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
925 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
926 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
927 if (fine_dining_destination == on_a_boat
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
928 || fine_dining_destination == on_a_train)
4541
3774dc061cdc [project @ 2003-10-17 01:10:10 by jwe]
jwe
parents: 4455
diff changeset
929 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
930 will, not, eat, green, eggs, and, ham);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
931 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
932 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
933 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
934
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
935 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
936 without having to add to the clutter with continuation markers.