Mercurial > octave-nkf
annotate doc/interpreter/stmt.txi @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | ef2dfe33e5b5 |
children | fca0dc2fb042 |
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 | |
50 * The if Statement:: | |
51 * The switch Statement:: | |
52 * The while Statement:: | |
3489 | 53 * The do-until Statement:: |
3294 | 54 * The for Statement:: |
55 * The break Statement:: | |
56 * The continue Statement:: | |
57 * The unwind_protect Statement:: | |
58 * The try Statement:: | |
59 * Continuation Lines:: | |
60 @end menu | |
61 | |
4167 | 62 @node The 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 | |
4167 | 211 @node The 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 |
219 one variable. This is possible using the @code{if} statement in the | |
220 following way | |
221 | |
222 @example | |
223 if (X == 1) | |
224 do_something (); | |
225 elseif (X == 2) | |
226 do_something_else (); | |
227 else | |
228 do_something_completely_different (); | |
229 endif | |
230 @end example | |
231 | |
232 @noindent | |
233 This kind of code can however be very cumbersome to both write and | |
234 maintain. To overcome this problem Octave supports the @code{switch} | |
235 statement. Using this statement, the above example becomes | |
236 | |
237 @example | |
238 switch (X) | |
239 case 1 | |
240 do_something (); | |
241 case 2 | |
242 do_something_else (); | |
243 otherwise | |
244 do_something_completely_different (); | |
245 endswitch | |
246 @end example | |
247 | |
248 @noindent | |
249 This code makes the repetitive structure of the problem more explicit, | |
250 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
|
251 variable @code{X} should change its name, only one line would need |
6530 | 252 changing compared to one line per case when @code{if} statements are |
253 used. | |
3294 | 254 |
255 The general form of the @code{switch} statement is | |
256 | |
257 @example | |
258 @group | |
259 switch @var{expression} | |
260 case @var{label} | |
261 @var{command_list} | |
262 case @var{label} | |
263 @var{command_list} | |
264 @dots{} | |
265 | |
266 otherwise | |
267 @var{command_list} | |
268 endswitch | |
269 @end group | |
270 @end example | |
271 | |
6530 | 272 @noindent |
273 where @var{label} can be any expression. However, duplicate | |
274 @var{label} values are not detected, and only the @var{command_list} | |
275 corresponding to the first match will be executed. For the | |
276 @code{switch} statement to be meaningful at least one | |
277 @code{case @var{label} @var{command_list}} clause must be present, | |
278 while the @code{otherwise @var{command_list}} clause is optional. | |
3294 | 279 |
6637 | 280 If @var{label} is a cell array the corresponding @var{command_list} |
281 is executed if @emph{any} of the elements of the cell array match | |
282 @var{expression}. As an example, the following program will print | |
283 @samp{Variable is either 6 or 7}. | |
284 | |
285 @example | |
286 A = 7; | |
287 switch A | |
288 case @{ 6, 7 @} | |
289 printf ("variable is either 6 or 7\n"); | |
290 otherwise | |
291 printf ("variable is neither 6 nor 7\n"); | |
292 endswitch | |
293 @end example | |
294 | |
3294 | 295 As with all other specific @code{end} keywords, @code{endswitch} may be |
296 replaced by @code{end}, but you can get better diagnostics if you use | |
297 the specific forms. | |
298 | |
6530 | 299 @c Strings can be matched |
300 | |
301 One advantage of using the @code{switch} statement compared to using | |
302 @code{if} statements is that the @var{label}s can be strings. If an | |
303 @code{if} statement is used it is @emph{not} possible to write | |
304 | |
305 @example | |
306 if (X == "a string") # This is NOT valid | |
307 @end example | |
308 | |
309 @noindent | |
310 since a character-to-character comparison between @code{X} and the | |
311 string will be made instead of evaluating if the strings are equal. | |
312 This special-case is handled by the @code{switch} statement, and it | |
313 is possible to write programs that look like this | |
314 | |
315 @example | |
316 switch (X) | |
317 case "a string" | |
318 do_something | |
319 @dots{} | |
320 endswitch | |
321 @end example | |
322 | |
6535 | 323 @menu |
324 * Notes for the C programmer:: | |
325 @end menu | |
326 | |
6530 | 327 @node Notes for the C programmer |
328 @subsection Notes for the C programmer | |
329 | |
6637 | 330 The @code{switch} statement is also available in the widely used C |
6530 | 331 programming language. There are, however, some differences |
332 between the statement in Octave and C | |
333 | |
334 @itemize @bullet | |
3294 | 335 @item |
336 Cases are exclusive, so they don't `fall through' as do the cases | |
6637 | 337 in the @code{switch} statement of the C language. |
3294 | 338 |
339 @item | |
340 The @var{command_list} elements are not optional. Making the list | |
341 optional would have meant requiring a separator between the label and | |
342 the command list. Otherwise, things like | |
343 | |
344 @example | |
345 @group | |
346 switch (foo) | |
347 case (1) -2 | |
348 @dots{} | |
349 @end group | |
350 @end example | |
351 | |
352 @noindent | |
353 would produce surprising results, as would | |
354 | |
355 @example | |
356 @group | |
357 switch (foo) | |
358 case (1) | |
359 case (2) | |
360 doit (); | |
361 @dots{} | |
362 @end group | |
363 @end example | |
364 | |
365 @noindent | |
6637 | 366 particularly for C programmers. If @code{doit()} should be executed if |
367 @var{foo} is either @code{1} or @code{2}, the above code should be | |
368 written with a cell array like this | |
369 | |
370 @example | |
371 @group | |
372 switch (foo) | |
373 case @{ 1, 2 @} | |
374 doit (); | |
375 @dots{} | |
376 @end group | |
377 @end example | |
3294 | 378 @end itemize |
379 | |
4167 | 380 @node The while Statement |
3294 | 381 @section The @code{while} Statement |
382 @cindex @code{while} statement | |
383 @cindex @code{endwhile} statement | |
384 @cindex loop | |
385 @cindex body of a loop | |
386 | |
387 In programming, a @dfn{loop} means a part of a program that is (or at least can | |
388 be) executed two or more times in succession. | |
389 | |
390 The @code{while} statement is the simplest looping statement in Octave. | |
391 It repeatedly executes a statement as long as a condition is true. As | |
392 with the condition in an @code{if} statement, the condition in a | |
393 @code{while} statement is considered true if its value is non-zero, and | |
394 false if its value is zero. If the value of the conditional expression | |
395 in a @code{while} statement is a vector or a matrix, it is considered | |
6637 | 396 true only if it is non-empty and @emph{all} of the elements are non-zero. |
3294 | 397 |
398 Octave's @code{while} statement looks like this: | |
399 | |
400 @example | |
401 @group | |
402 while (@var{condition}) | |
403 @var{body} | |
404 endwhile | |
405 @end group | |
406 @end example | |
407 | |
408 @noindent | |
409 Here @var{body} is a statement or list of statements that we call the | |
410 @dfn{body} of the loop, and @var{condition} is an expression that | |
411 controls how long the loop keeps running. | |
412 | |
413 The first thing the @code{while} statement does is test @var{condition}. | |
414 If @var{condition} is true, it executes the statement @var{body}. After | |
415 @var{body} has been executed, @var{condition} is tested again, and if it | |
416 is still true, @var{body} is executed again. This process repeats until | |
417 @var{condition} is no longer true. If @var{condition} is initially | |
418 false, the body of the loop is never executed. | |
419 | |
420 This example creates a variable @code{fib} that contains the first ten | |
421 elements of the Fibonacci sequence. | |
422 | |
423 @example | |
424 @group | |
425 fib = ones (1, 10); | |
426 i = 3; | |
427 while (i <= 10) | |
428 fib (i) = fib (i-1) + fib (i-2); | |
429 i++; | |
430 endwhile | |
431 @end group | |
432 @end example | |
433 | |
434 @noindent | |
435 Here the body of the loop contains two statements. | |
436 | |
437 The loop works like this: first, the value of @code{i} is set to 3. | |
438 Then, the @code{while} tests whether @code{i} is less than or equal to | |
439 10. This is the case when @code{i} equals 3, so the value of the | |
440 @code{i}-th element of @code{fib} is set to the sum of the previous two | |
441 values in the sequence. Then the @code{i++} increments the value of | |
442 @code{i} and the loop repeats. The loop terminates when @code{i} | |
443 reaches 11. | |
444 | |
445 A newline is not required between the condition and the | |
446 body; but using one makes the program clearer unless the body is very | |
447 simple. | |
448 | |
4167 | 449 @node The do-until Statement |
3489 | 450 @section The @code{do-until} Statement |
451 @cindex @code{do-until} statement | |
452 | |
453 The @code{do-until} statement is similar to the @code{while} statement, | |
454 except that it repeatedly executes a statement until a condition becomes | |
455 true, and the test of the condition is at the end of the loop, so the | |
456 body of the loop is always executed at least once. As with the | |
457 condition in an @code{if} statement, the condition in a @code{do-until} | |
458 statement is considered true if its value is non-zero, and false if its | |
459 value is zero. If the value of the conditional expression in a | |
460 @code{do-until} statement is a vector or a matrix, it is considered | |
6637 | 461 true only if it is non-empty and @emph{all} of the elements are non-zero. |
3489 | 462 |
463 Octave's @code{do-until} statement looks like this: | |
464 | |
465 @example | |
466 @group | |
467 do | |
468 @var{body} | |
469 until (@var{condition}) | |
470 @end group | |
471 @end example | |
472 | |
473 @noindent | |
474 Here @var{body} is a statement or list of statements that we call the | |
475 @dfn{body} of the loop, and @var{condition} is an expression that | |
476 controls how long the loop keeps running. | |
477 | |
478 This example creates a variable @code{fib} that contains the first ten | |
479 elements of the Fibonacci sequence. | |
480 | |
481 @example | |
482 @group | |
483 fib = ones (1, 10); | |
484 i = 2; | |
485 do | |
486 i++; | |
487 fib (i) = fib (i-1) + fib (i-2); | |
488 until (i == 10) | |
489 @end group | |
490 @end example | |
491 | |
492 A newline is not required between the @code{do} keyword and the | |
493 body; but using one makes the program clearer unless the body is very | |
494 simple. | |
495 | |
4167 | 496 @node The for Statement |
3294 | 497 @section The @code{for} Statement |
498 @cindex @code{for} statement | |
499 @cindex @code{endfor} statement | |
500 | |
501 The @code{for} statement makes it more convenient to count iterations of a | |
502 loop. The general form of the @code{for} statement looks like this: | |
503 | |
504 @example | |
505 @group | |
506 for @var{var} = @var{expression} | |
507 @var{body} | |
508 endfor | |
509 @end group | |
510 @end example | |
511 | |
512 @noindent | |
513 where @var{body} stands for any statement or list of statements, | |
514 @var{expression} is any valid expression, and @var{var} may take several | |
515 forms. Usually it is a simple variable name or an indexed variable. If | |
516 the value of @var{expression} is a structure, @var{var} may also be a | |
6637 | 517 vector with two elements. @xref{Looping Over Structure Elements}, below. |
3294 | 518 |
519 The assignment expression in the @code{for} statement works a bit | |
520 differently than Octave's normal assignment statement. Instead of | |
521 assigning the complete result of the expression, it assigns each column | |
522 of the expression to @var{var} in turn. If @var{expression} is a range, | |
523 a row vector, or a scalar, the value of @var{var} will be a scalar each | |
524 time the loop body is executed. If @var{var} is a column vector or a | |
525 matrix, @var{var} will be a column vector each time the loop body is | |
526 executed. | |
527 | |
528 The following example shows another way to create a vector containing | |
529 the first ten elements of the Fibonacci sequence, this time using the | |
530 @code{for} statement: | |
531 | |
532 @example | |
533 @group | |
534 fib = ones (1, 10); | |
535 for i = 3:10 | |
536 fib (i) = fib (i-1) + fib (i-2); | |
537 endfor | |
538 @end group | |
539 @end example | |
540 | |
541 @noindent | |
542 This code works by first evaluating the expression @code{3:10}, to | |
543 produce a range of values from 3 to 10 inclusive. Then the variable | |
544 @code{i} is assigned the first element of the range and the body of the | |
545 loop is executed once. When the end of the loop body is reached, the | |
546 next value in the range is assigned to the variable @code{i}, and the | |
547 loop body is executed again. This process continues until there are no | |
548 more elements to assign. | |
549 | |
6587 | 550 Within Octave is it also possible to iterate over matrices or cell arrays |
551 using the @code{for} statement. For example consider | |
552 | |
553 @example | |
554 @group | |
555 disp("Loop over a matrix") | |
556 for i = [1,3;2,4] | |
557 i | |
558 endfor | |
559 disp("Loop over a cell array") | |
560 for i = @{1,"two";"three",4@} | |
561 i | |
562 endfor | |
563 @end group | |
564 @end example | |
565 | |
566 @noindent | |
567 In this case the variable @code{i} takes on the value of the columns of | |
568 the matrix or cell matrix. So the first loop iterates twice, producing | |
7001 | 569 two column vectors @code{[1;2]}, followed by @code{[3;4]}, and likewise |
6587 | 570 for the loop over the cell array. This can be extended to loops over |
571 multidimensional arrays. For example | |
572 | |
573 @example | |
574 @group | |
575 a = [1,3;2,4]; b = cat(3, a, 2*a); | |
576 for i = c | |
577 i | |
578 endfor | |
579 @end group | |
580 @end example | |
581 | |
582 @noindent | |
6939 | 583 In the above case, the multidimensional matrix @var{c} is reshaped to a |
6587 | 584 two dimensional matrix as @code{reshape (c, rows(c), |
585 prod(size(c)(2:end)))} and then the same behavior as a loop over a two | |
586 dimensional matrix is produced. | |
587 | |
3294 | 588 Although it is possible to rewrite all @code{for} loops as @code{while} |
589 loops, the Octave language has both statements because often a | |
590 @code{for} loop is both less work to type and more natural to think of. | |
591 Counting the number of iterations is very common in loops and it can be | |
592 easier to think of this counting as part of looping rather than as | |
593 something to do inside the loop. | |
594 | |
595 @menu | |
596 * Looping Over Structure Elements:: | |
597 @end menu | |
598 | |
4167 | 599 @node Looping Over Structure Elements |
3294 | 600 @subsection Looping Over Structure Elements |
601 @cindex structure elements, looping over | |
602 @cindex looping over structure elements | |
603 | |
604 A special form of the @code{for} statement allows you to loop over all | |
605 the elements of a structure: | |
606 | |
607 @example | |
608 @group | |
609 for [ @var{val}, @var{key} ] = @var{expression} | |
610 @var{body} | |
611 endfor | |
612 @end group | |
613 @end example | |
614 | |
615 @noindent | |
616 In this form of the @code{for} statement, the value of @var{expression} | |
617 must be a structure. If it is, @var{key} and @var{val} are set to the | |
618 name of the element and the corresponding value in turn, until there are | |
619 no more elements. For example, | |
620 | |
621 @example | |
622 @group | |
623 x.a = 1 | |
624 x.b = [1, 2; 3, 4] | |
625 x.c = "string" | |
626 for [val, key] = x | |
627 key | |
628 val | |
629 endfor | |
630 | |
631 @print{} key = a | |
632 @print{} val = 1 | |
633 @print{} key = b | |
634 @print{} val = | |
635 @print{} | |
636 @print{} 1 2 | |
637 @print{} 3 4 | |
638 @print{} | |
639 @print{} key = c | |
640 @print{} val = string | |
641 @end group | |
642 @end example | |
643 | |
644 The elements are not accessed in any particular order. If you need to | |
645 cycle through the list in a particular way, you will have to use the | |
6637 | 646 function @code{fieldnames} and sort the list yourself. |
3294 | 647 |
648 The @var{key} variable may also be omitted. If it is, the brackets are | |
649 also optional. This is useful for cycling through the values of all the | |
650 structure elements when the names of the elements do not need to be | |
651 known. | |
652 | |
4167 | 653 @node The break Statement |
3294 | 654 @section The @code{break} Statement |
655 @cindex @code{break} statement | |
656 | |
657 The @code{break} statement jumps out of the innermost @code{for} or | |
658 @code{while} loop that encloses it. The @code{break} statement may only | |
659 be used within the body of a loop. The following example finds the | |
660 smallest divisor of a given integer, and also identifies prime numbers: | |
661 | |
662 @example | |
663 @group | |
664 num = 103; | |
665 div = 2; | |
666 while (div*div <= num) | |
667 if (rem (num, div) == 0) | |
668 break; | |
669 endif | |
670 div++; | |
671 endwhile | |
672 if (rem (num, div) == 0) | |
673 printf ("Smallest divisor of %d is %d\n", num, div) | |
674 else | |
675 printf ("%d is prime\n", num); | |
676 endif | |
677 @end group | |
678 @end example | |
679 | |
680 When the remainder is zero in the first @code{while} statement, Octave | |
681 immediately @dfn{breaks out} of the loop. This means that Octave | |
682 proceeds immediately to the statement following the loop and continues | |
683 processing. (This is very different from the @code{exit} statement | |
684 which stops the entire Octave program.) | |
685 | |
686 Here is another program equivalent to the previous one. It illustrates | |
687 how the @var{condition} of a @code{while} statement could just as well | |
688 be replaced with a @code{break} inside an @code{if}: | |
689 | |
690 @example | |
691 @group | |
692 num = 103; | |
693 div = 2; | |
694 while (1) | |
695 if (rem (num, div) == 0) | |
696 printf ("Smallest divisor of %d is %d\n", num, div); | |
697 break; | |
698 endif | |
699 div++; | |
700 if (div*div > num) | |
701 printf ("%d is prime\n", num); | |
702 break; | |
703 endif | |
704 endwhile | |
705 @end group | |
706 @end example | |
707 | |
4167 | 708 @node The continue Statement |
3294 | 709 @section The @code{continue} Statement |
710 @cindex @code{continue} statement | |
711 | |
712 The @code{continue} statement, like @code{break}, is used only inside | |
713 @code{for} or @code{while} loops. It skips over the rest of the loop | |
714 body, causing the next cycle around the loop to begin immediately. | |
715 Contrast this with @code{break}, which jumps out of the loop altogether. | |
716 Here is an example: | |
717 | |
718 @example | |
719 @group | |
720 # print elements of a vector of random | |
721 # integers that are even. | |
722 | |
723 # first, create a row vector of 10 random | |
724 # integers with values between 0 and 100: | |
725 | |
726 vec = round (rand (1, 10) * 100); | |
727 | |
728 # print what we're interested in: | |
729 | |
730 for x = vec | |
731 if (rem (x, 2) != 0) | |
732 continue; | |
733 endif | |
734 printf ("%d\n", x); | |
735 endfor | |
736 @end group | |
737 @end example | |
738 | |
739 If one of the elements of @var{vec} is an odd number, this example skips | |
740 the print statement for that element, and continues back to the first | |
741 statement in the loop. | |
742 | |
743 This is not a practical example of the @code{continue} statement, but it | |
744 should give you a clear understanding of how it works. Normally, one | |
745 would probably write the loop like this: | |
746 | |
747 @example | |
748 @group | |
749 for x = vec | |
750 if (rem (x, 2) == 0) | |
751 printf ("%d\n", x); | |
752 endif | |
753 endfor | |
754 @end group | |
755 @end example | |
756 | |
4167 | 757 @node The unwind_protect Statement |
3294 | 758 @section The @code{unwind_protect} Statement |
759 @cindex @code{unwind_protect} statement | |
760 @cindex @code{unwind_protect_cleanup} | |
761 @cindex @code{end_unwind_protect} | |
762 | |
763 Octave supports a limited form of exception handling modelled after the | |
764 unwind-protect form of Lisp. | |
765 | |
766 The general form of an @code{unwind_protect} block looks like this: | |
767 | |
768 @example | |
769 @group | |
770 unwind_protect | |
771 @var{body} | |
772 unwind_protect_cleanup | |
773 @var{cleanup} | |
774 end_unwind_protect | |
775 @end group | |
776 @end example | |
777 | |
778 @noindent | |
6637 | 779 where @var{body} and @var{cleanup} are both optional and may contain any |
3294 | 780 Octave expressions or commands. The statements in @var{cleanup} are |
781 guaranteed to be executed regardless of how control exits @var{body}. | |
782 | |
783 This is useful to protect temporary changes to global variables from | |
784 possible errors. For example, the following code will always restore | |
6501 | 785 the original value of the global variable @code{frobnositcate} |
8542
ef2dfe33e5b5
stmt.txi: unwind_protect doc fix
John W. Eaton <jwe@octave.org>
parents:
8347
diff
changeset
|
786 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
|
787 block. |
3294 | 788 |
789 @example | |
790 @group | |
6501 | 791 save_frobnosticate = frobnosticate; |
3294 | 792 unwind_protect |
6501 | 793 frobnosticate = true; |
794 @dots{} | |
3294 | 795 unwind_protect_cleanup |
6501 | 796 frobnosticate = save_frobnosticate; |
3294 | 797 end_unwind_protect |
798 @end group | |
799 @end example | |
800 | |
6637 | 801 @noindent |
6501 | 802 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
|
803 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
|
804 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
|
805 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
|
806 executed. |
3294 | 807 |
4167 | 808 @node The try Statement |
3294 | 809 @section The @code{try} Statement |
810 @cindex @code{try} statement | |
811 @cindex @code{catch} | |
812 @cindex @code{end_try_catch} | |
813 | |
814 In addition to unwind_protect, Octave supports another limited form of | |
815 exception handling. | |
816 | |
817 The general form of a @code{try} block looks like this: | |
818 | |
819 @example | |
820 @group | |
821 try | |
822 @var{body} | |
823 catch | |
824 @var{cleanup} | |
825 end_try_catch | |
826 @end group | |
827 @end example | |
828 | |
6637 | 829 @noindent |
830 where @var{body} and @var{cleanup} are both optional and may contain any | |
3294 | 831 Octave expressions or commands. The statements in @var{cleanup} are |
832 only executed if an error occurs in @var{body}. | |
833 | |
834 No warnings or error messages are printed while @var{body} is | |
835 executing. If an error does occur during the execution of @var{body}, | |
4699 | 836 @var{cleanup} can use the function @code{lasterr} to access the text |
837 of the message that would have been printed. This is the same | |
838 as @code{eval (@var{try}, @var{catch})} but it is more efficient since | |
839 the commands do not need to be parsed each time the @var{try} and | |
6667 | 840 @var{catch} statements are evaluated. @xref{Errors and Warnings}, for more |
4699 | 841 information about the @code{lasterr} function. |
3294 | 842 |
843 @cindex continuation lines | |
844 @cindex @code{...} continuation marker | |
845 @cindex @code{\} continuation marker | |
846 | |
4167 | 847 @node Continuation Lines |
3294 | 848 @section Continuation Lines |
849 | |
850 In the Octave language, most statements end with a newline character and | |
851 you must tell Octave to ignore the newline character in order to | |
852 continue a statement from one line to the next. Lines that end with the | |
853 characters @code{...} or @code{\} are joined with the following line | |
854 before they are divided into tokens by Octave's parser. For example, | |
855 the lines | |
856 | |
857 @example | |
858 @group | |
859 x = long_variable_name ... | |
860 + longer_variable_name \ | |
861 - 42 | |
862 @end group | |
863 @end example | |
864 | |
865 @noindent | |
866 form a single statement. The backslash character on the second line | |
6637 | 867 above is interpreted as a continuation character, @emph{not} as a division |
3294 | 868 operator. |
869 | |
870 For continuation lines that do not occur inside string constants, | |
871 whitespace and comments may appear between the continuation marker and | |
872 the newline character. For example, the statement | |
873 | |
874 @example | |
875 @group | |
876 x = long_variable_name ... # comment one | |
877 + longer_variable_name \ # comment two | |
878 - 42 # last comment | |
879 @end group | |
880 @end example | |
881 | |
882 @noindent | |
883 is equivalent to the one shown above. Inside string constants, the | |
884 continuation marker must appear at the end of the line just before the | |
885 newline character. | |
886 | |
887 Input that occurs inside parentheses can be continued to the next line | |
888 without having to use a continuation marker. For example, it is | |
889 possible to write statements like | |
890 | |
891 @example | |
892 @group | |
893 if (fine_dining_destination == on_a_boat | |
894 || fine_dining_destination == on_a_train) | |
4541 | 895 seuss (i, will, not, eat, them, sam, i, am, i, |
3294 | 896 will, not, eat, green, eggs, and, ham); |
897 endif | |
898 @end group | |
899 @end example | |
900 | |
901 @noindent | |
902 without having to add to the clutter with continuation markers. |