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