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