Mercurial > octave-nkf
comparison doc/interpreter/stmt.txi @ 6637:c18ed0e7ee41
[project @ 2007-05-21 19:12:46 by jwe]
author | jwe |
---|---|
date | Mon, 21 May 2007 19:12:46 +0000 |
parents | 00fad3bad2a5 |
children | 15837c5982cb |
comparison
equal
deleted
inserted
replaced
6636:de8a8ba43848 | 6637:c18ed0e7ee41 |
---|---|
69 @var{condition} is true. | 69 @var{condition} is true. |
70 | 70 |
71 The condition in an @code{if} statement is considered true if its value | 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 | 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 | 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 | 74 matrix, it is considered true only if it is non-empty and @emph{all} |
75 non-zero. | 75 of the elements are non-zero. |
76 | 76 |
77 The second form of an if statement looks like this: | 77 The second form of an if statement looks like this: |
78 | 78 |
79 @example | 79 @example |
80 @group | 80 @group |
260 corresponding to the first match will be executed. For the | 260 corresponding to the first match will be executed. For the |
261 @code{switch} statement to be meaningful at least one | 261 @code{switch} statement to be meaningful at least one |
262 @code{case @var{label} @var{command_list}} clause must be present, | 262 @code{case @var{label} @var{command_list}} clause must be present, |
263 while the @code{otherwise @var{command_list}} clause is optional. | 263 while the @code{otherwise @var{command_list}} clause is optional. |
264 | 264 |
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 | |
265 As with all other specific @code{end} keywords, @code{endswitch} may be | 280 As with all other specific @code{end} keywords, @code{endswitch} may be |
266 replaced by @code{end}, but you can get better diagnostics if you use | 281 replaced by @code{end}, but you can get better diagnostics if you use |
267 the specific forms. | 282 the specific forms. |
268 | 283 |
269 @c Strings can be matched | 284 @c Strings can be matched |
295 @end menu | 310 @end menu |
296 | 311 |
297 @node Notes for the C programmer | 312 @node Notes for the C programmer |
298 @subsection Notes for the C programmer | 313 @subsection Notes for the C programmer |
299 | 314 |
300 The @code{switch} statement is also used in the widely used C | 315 The @code{switch} statement is also available in the widely used C |
301 programming language. There are, however, some differences | 316 programming language. There are, however, some differences |
302 between the statement in Octave and C | 317 between the statement in Octave and C |
303 | 318 |
304 @itemize @bullet | 319 @itemize @bullet |
305 @item | 320 @item |
306 Cases are exclusive, so they don't `fall through' as do the cases | 321 Cases are exclusive, so they don't `fall through' as do the cases |
307 in the switch statement of the C language. | 322 in the @code{switch} statement of the C language. |
308 | 323 |
309 @item | 324 @item |
310 The @var{command_list} elements are not optional. Making the list | 325 The @var{command_list} elements are not optional. Making the list |
311 optional would have meant requiring a separator between the label and | 326 optional would have meant requiring a separator between the label and |
312 the command list. Otherwise, things like | 327 the command list. Otherwise, things like |
331 @dots{} | 346 @dots{} |
332 @end group | 347 @end group |
333 @end example | 348 @end example |
334 | 349 |
335 @noindent | 350 @noindent |
336 particularly for C programmers. | 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 | |
337 @end itemize | 363 @end itemize |
338 | 364 |
339 @node The while Statement | 365 @node The while Statement |
340 @section The @code{while} Statement | 366 @section The @code{while} Statement |
341 @cindex @code{while} statement | 367 @cindex @code{while} statement |
350 It repeatedly executes a statement as long as a condition is true. As | 376 It repeatedly executes a statement as long as a condition is true. As |
351 with the condition in an @code{if} statement, the condition in a | 377 with the condition in an @code{if} statement, the condition in a |
352 @code{while} statement is considered true if its value is non-zero, and | 378 @code{while} statement is considered true if its value is non-zero, and |
353 false if its value is zero. If the value of the conditional expression | 379 false if its value is zero. If the value of the conditional expression |
354 in a @code{while} statement is a vector or a matrix, it is considered | 380 in a @code{while} statement is a vector or a matrix, it is considered |
355 true only if @emph{all} of the elements are non-zero. | 381 true only if it is non-empty and @emph{all} of the elements are non-zero. |
356 | 382 |
357 Octave's @code{while} statement looks like this: | 383 Octave's @code{while} statement looks like this: |
358 | 384 |
359 @example | 385 @example |
360 @group | 386 @group |
415 body of the loop is always executed at least once. As with the | 441 body of the loop is always executed at least once. As with the |
416 condition in an @code{if} statement, the condition in a @code{do-until} | 442 condition in an @code{if} statement, the condition in a @code{do-until} |
417 statement is considered true if its value is non-zero, and false if its | 443 statement is considered true if its value is non-zero, and false if its |
418 value is zero. If the value of the conditional expression in a | 444 value is zero. If the value of the conditional expression in a |
419 @code{do-until} statement is a vector or a matrix, it is considered | 445 @code{do-until} statement is a vector or a matrix, it is considered |
420 true only if @emph{all} of the elements are non-zero. | 446 true only if it is non-empty and @emph{all} of the elements are non-zero. |
421 | 447 |
422 Octave's @code{do-until} statement looks like this: | 448 Octave's @code{do-until} statement looks like this: |
423 | 449 |
424 @example | 450 @example |
425 @group | 451 @group |
471 @noindent | 497 @noindent |
472 where @var{body} stands for any statement or list of statements, | 498 where @var{body} stands for any statement or list of statements, |
473 @var{expression} is any valid expression, and @var{var} may take several | 499 @var{expression} is any valid expression, and @var{var} may take several |
474 forms. Usually it is a simple variable name or an indexed variable. If | 500 forms. Usually it is a simple variable name or an indexed variable. If |
475 the value of @var{expression} is a structure, @var{var} may also be a | 501 the value of @var{expression} is a structure, @var{var} may also be a |
476 list. @xref{Looping Over Structure Elements}, below. | 502 vector with two elements. @xref{Looping Over Structure Elements}, below. |
477 | 503 |
478 The assignment expression in the @code{for} statement works a bit | 504 The assignment expression in the @code{for} statement works a bit |
479 differently than Octave's normal assignment statement. Instead of | 505 differently than Octave's normal assignment statement. Instead of |
480 assigning the complete result of the expression, it assigns each column | 506 assigning the complete result of the expression, it assigns each column |
481 of the expression to @var{var} in turn. If @var{expression} is a range, | 507 of the expression to @var{var} in turn. If @var{expression} is a range, |
600 @end group | 626 @end group |
601 @end example | 627 @end example |
602 | 628 |
603 The elements are not accessed in any particular order. If you need to | 629 The elements are not accessed in any particular order. If you need to |
604 cycle through the list in a particular way, you will have to use the | 630 cycle through the list in a particular way, you will have to use the |
605 function @code{struct_elements} and sort the list yourself. | 631 function @code{fieldnames} and sort the list yourself. |
606 | 632 |
607 The @var{key} variable may also be omitted. If it is, the brackets are | 633 The @var{key} variable may also be omitted. If it is, the brackets are |
608 also optional. This is useful for cycling through the values of all the | 634 also optional. This is useful for cycling through the values of all the |
609 structure elements when the names of the elements do not need to be | 635 structure elements when the names of the elements do not need to be |
610 known. | 636 known. |
733 end_unwind_protect | 759 end_unwind_protect |
734 @end group | 760 @end group |
735 @end example | 761 @end example |
736 | 762 |
737 @noindent | 763 @noindent |
738 Where @var{body} and @var{cleanup} are both optional and may contain any | 764 where @var{body} and @var{cleanup} are both optional and may contain any |
739 Octave expressions or commands. The statements in @var{cleanup} are | 765 Octave expressions or commands. The statements in @var{cleanup} are |
740 guaranteed to be executed regardless of how control exits @var{body}. | 766 guaranteed to be executed regardless of how control exits @var{body}. |
741 | 767 |
742 This is useful to protect temporary changes to global variables from | 768 This is useful to protect temporary changes to global variables from |
743 possible errors. For example, the following code will always restore | 769 possible errors. For example, the following code will always restore |
754 frobnosticate = save_frobnosticate; | 780 frobnosticate = save_frobnosticate; |
755 end_unwind_protect | 781 end_unwind_protect |
756 @end group | 782 @end group |
757 @end example | 783 @end example |
758 | 784 |
785 @noindent | |
759 Without @code{unwind_protect}, the value of @var{frobnosticate} | 786 Without @code{unwind_protect}, the value of @var{frobnosticate} |
760 would not be restored if an error occurs while performing the indexing | 787 would not be restored if an error occurs while performing the indexing |
761 operation because evaluation would stop at the point of the error and | 788 operation because evaluation would stop at the point of the error and |
762 the statement to restore the value would not be executed. | 789 the statement to restore the value would not be executed. |
763 | 790 |
780 @var{cleanup} | 807 @var{cleanup} |
781 end_try_catch | 808 end_try_catch |
782 @end group | 809 @end group |
783 @end example | 810 @end example |
784 | 811 |
785 Where @var{body} and @var{cleanup} are both optional and may contain any | 812 @noindent |
813 where @var{body} and @var{cleanup} are both optional and may contain any | |
786 Octave expressions or commands. The statements in @var{cleanup} are | 814 Octave expressions or commands. The statements in @var{cleanup} are |
787 only executed if an error occurs in @var{body}. | 815 only executed if an error occurs in @var{body}. |
788 | 816 |
789 No warnings or error messages are printed while @var{body} is | 817 No warnings or error messages are printed while @var{body} is |
790 executing. If an error does occur during the execution of @var{body}, | 818 executing. If an error does occur during the execution of @var{body}, |
823 @end group | 851 @end group |
824 @end example | 852 @end example |
825 | 853 |
826 @noindent | 854 @noindent |
827 form a single statement. The backslash character on the second line | 855 form a single statement. The backslash character on the second line |
828 above is interpreted a continuation character, @emph{not} as a division | 856 above is interpreted as a continuation character, @emph{not} as a division |
829 operator. | 857 operator. |
830 | 858 |
831 For continuation lines that do not occur inside string constants, | 859 For continuation lines that do not occur inside string constants, |
832 whitespace and comments may appear between the continuation marker and | 860 whitespace and comments may appear between the continuation marker and |
833 the newline character. For example, the statement | 861 the newline character. For example, the statement |