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