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