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