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 |
3366
|
196 @DOCSTRING(warn_assign_as_truth_value) |
3294
|
197 |
4167
|
198 @node The switch Statement |
3294
|
199 @section The @code{switch} Statement |
|
200 @cindex @code{switch} statement |
|
201 @cindex @code{case} statement |
|
202 @cindex @code{otherwise} statement |
|
203 @cindex @code{endswitch} statement |
|
204 |
|
205 The @code{switch} statement was introduced in Octave 2.0.5. It should |
|
206 be considered experimental, and details of the implementation may change |
|
207 slightly in future versions of Octave. If you have comments or would |
|
208 like to share your experiences in trying to use this new command in real |
|
209 programs, please send them to |
|
210 @email{octave-maintainers@@bevo.che.wisc.edu}. (But if you think you've |
|
211 found a bug, please report it to @email{bug-octave@@bevo.che.wisc.edu}. |
|
212 |
|
213 The general form of the @code{switch} statement is |
|
214 |
|
215 @example |
|
216 @group |
|
217 switch @var{expression} |
|
218 case @var{label} |
|
219 @var{command_list} |
|
220 case @var{label} |
|
221 @var{command_list} |
|
222 @dots{} |
|
223 |
|
224 otherwise |
|
225 @var{command_list} |
|
226 endswitch |
|
227 @end group |
|
228 @end example |
|
229 |
|
230 @itemize @bullet |
|
231 @item |
|
232 The identifiers @code{switch}, @code{case}, @code{otherwise}, and |
|
233 @code{endswitch} are now keywords. |
|
234 |
|
235 @item |
|
236 The @var{label} may be any expression. |
|
237 |
|
238 @item |
|
239 Duplicate @var{label} values are not detected. The @var{command_list} |
|
240 corresponding to the first match will be executed. |
|
241 |
|
242 @item |
|
243 You must have at least one @code{case @var{label} @var{command_list}} |
|
244 clause. |
|
245 |
|
246 @item |
|
247 The @code{otherwise @var{command_list}} clause is optional. |
|
248 |
|
249 @item |
|
250 As with all other specific @code{end} keywords, @code{endswitch} may be |
|
251 replaced by @code{end}, but you can get better diagnostics if you use |
|
252 the specific forms. |
|
253 |
|
254 @item |
|
255 Cases are exclusive, so they don't `fall through' as do the cases |
|
256 in the switch statement of the C language. |
|
257 |
|
258 @item |
|
259 The @var{command_list} elements are not optional. Making the list |
|
260 optional would have meant requiring a separator between the label and |
|
261 the command list. Otherwise, things like |
|
262 |
|
263 @example |
|
264 @group |
|
265 switch (foo) |
|
266 case (1) -2 |
|
267 @dots{} |
|
268 @end group |
|
269 @end example |
|
270 |
|
271 @noindent |
|
272 would produce surprising results, as would |
|
273 |
|
274 @example |
|
275 @group |
|
276 switch (foo) |
|
277 case (1) |
|
278 case (2) |
|
279 doit (); |
|
280 @dots{} |
|
281 @end group |
|
282 @end example |
|
283 |
|
284 @noindent |
|
285 particularly for C programmers. |
|
286 |
|
287 @item |
|
288 The implementation is simple-minded and currently offers no real |
|
289 performance improvement over an equivalent @code{if} block, even if all |
|
290 the labels are integer constants. Perhaps a future variation on this |
|
291 could detect all constant integer labels and improve performance by |
|
292 using a jump table. |
|
293 @end itemize |
|
294 |
3366
|
295 @DOCSTRING(warn_variable_switch_label) |
3294
|
296 |
4167
|
297 @node The while Statement |
3294
|
298 @section The @code{while} Statement |
|
299 @cindex @code{while} statement |
|
300 @cindex @code{endwhile} statement |
|
301 @cindex loop |
|
302 @cindex body of a loop |
|
303 |
|
304 In programming, a @dfn{loop} means a part of a program that is (or at least can |
|
305 be) executed two or more times in succession. |
|
306 |
|
307 The @code{while} statement is the simplest looping statement in Octave. |
|
308 It repeatedly executes a statement as long as a condition is true. As |
|
309 with the condition in an @code{if} statement, the condition in a |
|
310 @code{while} statement is considered true if its value is non-zero, and |
|
311 false if its value is zero. If the value of the conditional expression |
|
312 in a @code{while} statement is a vector or a matrix, it is considered |
|
313 true only if @emph{all} of the elements are non-zero. |
|
314 |
|
315 Octave's @code{while} statement looks like this: |
|
316 |
|
317 @example |
|
318 @group |
|
319 while (@var{condition}) |
|
320 @var{body} |
|
321 endwhile |
|
322 @end group |
|
323 @end example |
|
324 |
|
325 @noindent |
|
326 Here @var{body} is a statement or list of statements that we call the |
|
327 @dfn{body} of the loop, and @var{condition} is an expression that |
|
328 controls how long the loop keeps running. |
|
329 |
|
330 The first thing the @code{while} statement does is test @var{condition}. |
|
331 If @var{condition} is true, it executes the statement @var{body}. After |
|
332 @var{body} has been executed, @var{condition} is tested again, and if it |
|
333 is still true, @var{body} is executed again. This process repeats until |
|
334 @var{condition} is no longer true. If @var{condition} is initially |
|
335 false, the body of the loop is never executed. |
|
336 |
|
337 This example creates a variable @code{fib} that contains the first ten |
|
338 elements of the Fibonacci sequence. |
|
339 |
|
340 @example |
|
341 @group |
|
342 fib = ones (1, 10); |
|
343 i = 3; |
|
344 while (i <= 10) |
|
345 fib (i) = fib (i-1) + fib (i-2); |
|
346 i++; |
|
347 endwhile |
|
348 @end group |
|
349 @end example |
|
350 |
|
351 @noindent |
|
352 Here the body of the loop contains two statements. |
|
353 |
|
354 The loop works like this: first, the value of @code{i} is set to 3. |
|
355 Then, the @code{while} tests whether @code{i} is less than or equal to |
|
356 10. This is the case when @code{i} equals 3, so the value of the |
|
357 @code{i}-th element of @code{fib} is set to the sum of the previous two |
|
358 values in the sequence. Then the @code{i++} increments the value of |
|
359 @code{i} and the loop repeats. The loop terminates when @code{i} |
|
360 reaches 11. |
|
361 |
|
362 A newline is not required between the condition and the |
|
363 body; but using one makes the program clearer unless the body is very |
|
364 simple. |
|
365 |
3402
|
366 @xref{The if Statement}, for a description of the variable |
3294
|
367 @code{warn_assign_as_truth_value}. |
|
368 |
4167
|
369 @node The do-until Statement |
3489
|
370 @section The @code{do-until} Statement |
|
371 @cindex @code{do-until} statement |
|
372 |
|
373 The @code{do-until} statement is similar to the @code{while} statement, |
|
374 except that it repeatedly executes a statement until a condition becomes |
|
375 true, and the test of the condition is at the end of the loop, so the |
|
376 body of the loop is always executed at least once. As with the |
|
377 condition in an @code{if} statement, the condition in a @code{do-until} |
|
378 statement is considered true if its value is non-zero, and false if its |
|
379 value is zero. If the value of the conditional expression in a |
|
380 @code{do-until} statement is a vector or a matrix, it is considered |
|
381 true only if @emph{all} of the elements are non-zero. |
|
382 |
|
383 Octave's @code{do-until} statement looks like this: |
|
384 |
|
385 @example |
|
386 @group |
|
387 do |
|
388 @var{body} |
|
389 until (@var{condition}) |
|
390 @end group |
|
391 @end example |
|
392 |
|
393 @noindent |
|
394 Here @var{body} is a statement or list of statements that we call the |
|
395 @dfn{body} of the loop, and @var{condition} is an expression that |
|
396 controls how long the loop keeps running. |
|
397 |
|
398 This example creates a variable @code{fib} that contains the first ten |
|
399 elements of the Fibonacci sequence. |
|
400 |
|
401 @example |
|
402 @group |
|
403 fib = ones (1, 10); |
|
404 i = 2; |
|
405 do |
|
406 i++; |
|
407 fib (i) = fib (i-1) + fib (i-2); |
|
408 until (i == 10) |
|
409 @end group |
|
410 @end example |
|
411 |
|
412 A newline is not required between the @code{do} keyword and the |
|
413 body; but using one makes the program clearer unless the body is very |
|
414 simple. |
|
415 |
|
416 @xref{The if Statement}, for a description of the variable |
|
417 @code{warn_assign_as_truth_value}. |
|
418 |
4167
|
419 @node The for Statement |
3294
|
420 @section The @code{for} Statement |
|
421 @cindex @code{for} statement |
|
422 @cindex @code{endfor} statement |
|
423 |
|
424 The @code{for} statement makes it more convenient to count iterations of a |
|
425 loop. The general form of the @code{for} statement looks like this: |
|
426 |
|
427 @example |
|
428 @group |
|
429 for @var{var} = @var{expression} |
|
430 @var{body} |
|
431 endfor |
|
432 @end group |
|
433 @end example |
|
434 |
|
435 @noindent |
|
436 where @var{body} stands for any statement or list of statements, |
|
437 @var{expression} is any valid expression, and @var{var} may take several |
|
438 forms. Usually it is a simple variable name or an indexed variable. If |
|
439 the value of @var{expression} is a structure, @var{var} may also be a |
|
440 list. @xref{Looping Over Structure Elements}, below. |
|
441 |
|
442 The assignment expression in the @code{for} statement works a bit |
|
443 differently than Octave's normal assignment statement. Instead of |
|
444 assigning the complete result of the expression, it assigns each column |
|
445 of the expression to @var{var} in turn. If @var{expression} is a range, |
|
446 a row vector, or a scalar, the value of @var{var} will be a scalar each |
|
447 time the loop body is executed. If @var{var} is a column vector or a |
|
448 matrix, @var{var} will be a column vector each time the loop body is |
|
449 executed. |
|
450 |
|
451 The following example shows another way to create a vector containing |
|
452 the first ten elements of the Fibonacci sequence, this time using the |
|
453 @code{for} statement: |
|
454 |
|
455 @example |
|
456 @group |
|
457 fib = ones (1, 10); |
|
458 for i = 3:10 |
|
459 fib (i) = fib (i-1) + fib (i-2); |
|
460 endfor |
|
461 @end group |
|
462 @end example |
|
463 |
|
464 @noindent |
|
465 This code works by first evaluating the expression @code{3:10}, to |
|
466 produce a range of values from 3 to 10 inclusive. Then the variable |
|
467 @code{i} is assigned the first element of the range and the body of the |
|
468 loop is executed once. When the end of the loop body is reached, the |
|
469 next value in the range is assigned to the variable @code{i}, and the |
|
470 loop body is executed again. This process continues until there are no |
|
471 more elements to assign. |
|
472 |
|
473 Although it is possible to rewrite all @code{for} loops as @code{while} |
|
474 loops, the Octave language has both statements because often a |
|
475 @code{for} loop is both less work to type and more natural to think of. |
|
476 Counting the number of iterations is very common in loops and it can be |
|
477 easier to think of this counting as part of looping rather than as |
|
478 something to do inside the loop. |
|
479 |
|
480 @menu |
|
481 * Looping Over Structure Elements:: |
|
482 @end menu |
|
483 |
4167
|
484 @node Looping Over Structure Elements |
3294
|
485 @subsection Looping Over Structure Elements |
|
486 @cindex structure elements, looping over |
|
487 @cindex looping over structure elements |
|
488 |
|
489 A special form of the @code{for} statement allows you to loop over all |
|
490 the elements of a structure: |
|
491 |
|
492 @example |
|
493 @group |
|
494 for [ @var{val}, @var{key} ] = @var{expression} |
|
495 @var{body} |
|
496 endfor |
|
497 @end group |
|
498 @end example |
|
499 |
|
500 @noindent |
|
501 In this form of the @code{for} statement, the value of @var{expression} |
|
502 must be a structure. If it is, @var{key} and @var{val} are set to the |
|
503 name of the element and the corresponding value in turn, until there are |
|
504 no more elements. For example, |
|
505 |
|
506 @example |
|
507 @group |
|
508 x.a = 1 |
|
509 x.b = [1, 2; 3, 4] |
|
510 x.c = "string" |
|
511 for [val, key] = x |
|
512 key |
|
513 val |
|
514 endfor |
|
515 |
|
516 @print{} key = a |
|
517 @print{} val = 1 |
|
518 @print{} key = b |
|
519 @print{} val = |
|
520 @print{} |
|
521 @print{} 1 2 |
|
522 @print{} 3 4 |
|
523 @print{} |
|
524 @print{} key = c |
|
525 @print{} val = string |
|
526 @end group |
|
527 @end example |
|
528 |
|
529 The elements are not accessed in any particular order. If you need to |
|
530 cycle through the list in a particular way, you will have to use the |
|
531 function @code{struct_elements} and sort the list yourself. |
|
532 |
|
533 The @var{key} variable may also be omitted. If it is, the brackets are |
|
534 also optional. This is useful for cycling through the values of all the |
|
535 structure elements when the names of the elements do not need to be |
|
536 known. |
|
537 |
4167
|
538 @node The break Statement |
3294
|
539 @section The @code{break} Statement |
|
540 @cindex @code{break} statement |
|
541 |
|
542 The @code{break} statement jumps out of the innermost @code{for} or |
|
543 @code{while} loop that encloses it. The @code{break} statement may only |
|
544 be used within the body of a loop. The following example finds the |
|
545 smallest divisor of a given integer, and also identifies prime numbers: |
|
546 |
|
547 @example |
|
548 @group |
|
549 num = 103; |
|
550 div = 2; |
|
551 while (div*div <= num) |
|
552 if (rem (num, div) == 0) |
|
553 break; |
|
554 endif |
|
555 div++; |
|
556 endwhile |
|
557 if (rem (num, div) == 0) |
|
558 printf ("Smallest divisor of %d is %d\n", num, div) |
|
559 else |
|
560 printf ("%d is prime\n", num); |
|
561 endif |
|
562 @end group |
|
563 @end example |
|
564 |
|
565 When the remainder is zero in the first @code{while} statement, Octave |
|
566 immediately @dfn{breaks out} of the loop. This means that Octave |
|
567 proceeds immediately to the statement following the loop and continues |
|
568 processing. (This is very different from the @code{exit} statement |
|
569 which stops the entire Octave program.) |
|
570 |
|
571 Here is another program equivalent to the previous one. It illustrates |
|
572 how the @var{condition} of a @code{while} statement could just as well |
|
573 be replaced with a @code{break} inside an @code{if}: |
|
574 |
|
575 @example |
|
576 @group |
|
577 num = 103; |
|
578 div = 2; |
|
579 while (1) |
|
580 if (rem (num, div) == 0) |
|
581 printf ("Smallest divisor of %d is %d\n", num, div); |
|
582 break; |
|
583 endif |
|
584 div++; |
|
585 if (div*div > num) |
|
586 printf ("%d is prime\n", num); |
|
587 break; |
|
588 endif |
|
589 endwhile |
|
590 @end group |
|
591 @end example |
|
592 |
4167
|
593 @node The continue Statement |
3294
|
594 @section The @code{continue} Statement |
|
595 @cindex @code{continue} statement |
|
596 |
|
597 The @code{continue} statement, like @code{break}, is used only inside |
|
598 @code{for} or @code{while} loops. It skips over the rest of the loop |
|
599 body, causing the next cycle around the loop to begin immediately. |
|
600 Contrast this with @code{break}, which jumps out of the loop altogether. |
|
601 Here is an example: |
|
602 |
|
603 @example |
|
604 @group |
|
605 # print elements of a vector of random |
|
606 # integers that are even. |
|
607 |
|
608 # first, create a row vector of 10 random |
|
609 # integers with values between 0 and 100: |
|
610 |
|
611 vec = round (rand (1, 10) * 100); |
|
612 |
|
613 # print what we're interested in: |
|
614 |
|
615 for x = vec |
|
616 if (rem (x, 2) != 0) |
|
617 continue; |
|
618 endif |
|
619 printf ("%d\n", x); |
|
620 endfor |
|
621 @end group |
|
622 @end example |
|
623 |
|
624 If one of the elements of @var{vec} is an odd number, this example skips |
|
625 the print statement for that element, and continues back to the first |
|
626 statement in the loop. |
|
627 |
|
628 This is not a practical example of the @code{continue} statement, but it |
|
629 should give you a clear understanding of how it works. Normally, one |
|
630 would probably write the loop like this: |
|
631 |
|
632 @example |
|
633 @group |
|
634 for x = vec |
|
635 if (rem (x, 2) == 0) |
|
636 printf ("%d\n", x); |
|
637 endif |
|
638 endfor |
|
639 @end group |
|
640 @end example |
|
641 |
4167
|
642 @node The unwind_protect Statement |
3294
|
643 @section The @code{unwind_protect} Statement |
|
644 @cindex @code{unwind_protect} statement |
|
645 @cindex @code{unwind_protect_cleanup} |
|
646 @cindex @code{end_unwind_protect} |
|
647 |
|
648 Octave supports a limited form of exception handling modelled after the |
|
649 unwind-protect form of Lisp. |
|
650 |
|
651 The general form of an @code{unwind_protect} block looks like this: |
|
652 |
|
653 @example |
|
654 @group |
|
655 unwind_protect |
|
656 @var{body} |
|
657 unwind_protect_cleanup |
|
658 @var{cleanup} |
|
659 end_unwind_protect |
|
660 @end group |
|
661 @end example |
|
662 |
|
663 @noindent |
|
664 Where @var{body} and @var{cleanup} are both optional and may contain any |
|
665 Octave expressions or commands. The statements in @var{cleanup} are |
|
666 guaranteed to be executed regardless of how control exits @var{body}. |
|
667 |
|
668 This is useful to protect temporary changes to global variables from |
|
669 possible errors. For example, the following code will always restore |
4455
|
670 the original value of the built-in variable @code{warn_fortran_indexing} |
3294
|
671 even if an error occurs while performing the indexing operation. |
|
672 |
|
673 @example |
|
674 @group |
4455
|
675 save_warn_fortran_indexing = warn_fortran_indexing; |
3294
|
676 unwind_protect |
4455
|
677 warn_fortran_indexing = 1; |
3294
|
678 elt = a (idx) |
|
679 unwind_protect_cleanup |
4455
|
680 warn_fortran_indexing = save_warn_fortran_indexing; |
3294
|
681 end_unwind_protect |
|
682 @end group |
|
683 @end example |
|
684 |
4455
|
685 Without @code{unwind_protect}, the value of @var{warn_fortran_indexing} |
3294
|
686 would not be restored if an error occurs while performing the indexing |
|
687 operation because evaluation would stop at the point of the error and |
|
688 the statement to restore the value would not be executed. |
|
689 |
4167
|
690 @node The try Statement |
3294
|
691 @section The @code{try} Statement |
|
692 @cindex @code{try} statement |
|
693 @cindex @code{catch} |
|
694 @cindex @code{end_try_catch} |
|
695 |
|
696 In addition to unwind_protect, Octave supports another limited form of |
|
697 exception handling. |
|
698 |
|
699 The general form of a @code{try} block looks like this: |
|
700 |
|
701 @example |
|
702 @group |
|
703 try |
|
704 @var{body} |
|
705 catch |
|
706 @var{cleanup} |
|
707 end_try_catch |
|
708 @end group |
|
709 @end example |
|
710 |
|
711 Where @var{body} and @var{cleanup} are both optional and may contain any |
|
712 Octave expressions or commands. The statements in @var{cleanup} are |
|
713 only executed if an error occurs in @var{body}. |
|
714 |
|
715 No warnings or error messages are printed while @var{body} is |
|
716 executing. If an error does occur during the execution of @var{body}, |
4699
|
717 @var{cleanup} can use the function @code{lasterr} to access the text |
|
718 of the message that would have been printed. This is the same |
|
719 as @code{eval (@var{try}, @var{catch})} but it is more efficient since |
|
720 the commands do not need to be parsed each time the @var{try} and |
|
721 @var{catch} statements are evaluated. @xref{Error Handling}, for more |
|
722 information about the @code{lasterr} function. |
3294
|
723 |
|
724 Octave's @var{try} block is a very limited variation on the Lisp |
|
725 condition-case form (limited because it cannot handle different classes |
|
726 of errors separately). Perhaps at some point Octave can have some sort |
|
727 of classification of errors and try-catch can be improved to be as |
|
728 powerful as condition-case in Lisp. |
|
729 |
|
730 @cindex continuation lines |
|
731 @cindex @code{...} continuation marker |
|
732 @cindex @code{\} continuation marker |
|
733 |
4167
|
734 @node Continuation Lines |
3294
|
735 @section Continuation Lines |
|
736 |
|
737 In the Octave language, most statements end with a newline character and |
|
738 you must tell Octave to ignore the newline character in order to |
|
739 continue a statement from one line to the next. Lines that end with the |
|
740 characters @code{...} or @code{\} are joined with the following line |
|
741 before they are divided into tokens by Octave's parser. For example, |
|
742 the lines |
|
743 |
|
744 @example |
|
745 @group |
|
746 x = long_variable_name ... |
|
747 + longer_variable_name \ |
|
748 - 42 |
|
749 @end group |
|
750 @end example |
|
751 |
|
752 @noindent |
|
753 form a single statement. The backslash character on the second line |
|
754 above is interpreted a continuation character, @emph{not} as a division |
|
755 operator. |
|
756 |
|
757 For continuation lines that do not occur inside string constants, |
|
758 whitespace and comments may appear between the continuation marker and |
|
759 the newline character. For example, the statement |
|
760 |
|
761 @example |
|
762 @group |
|
763 x = long_variable_name ... # comment one |
|
764 + longer_variable_name \ # comment two |
|
765 - 42 # last comment |
|
766 @end group |
|
767 @end example |
|
768 |
|
769 @noindent |
|
770 is equivalent to the one shown above. Inside string constants, the |
|
771 continuation marker must appear at the end of the line just before the |
|
772 newline character. |
|
773 |
|
774 Input that occurs inside parentheses can be continued to the next line |
|
775 without having to use a continuation marker. For example, it is |
|
776 possible to write statements like |
|
777 |
|
778 @example |
|
779 @group |
|
780 if (fine_dining_destination == on_a_boat |
|
781 || fine_dining_destination == on_a_train) |
4541
|
782 seuss (i, will, not, eat, them, sam, i, am, i, |
3294
|
783 will, not, eat, green, eggs, and, ham); |
|
784 endif |
|
785 @end group |
|
786 @end example |
|
787 |
|
788 @noindent |
|
789 without having to add to the clutter with continuation markers. |