File: octave_11.html

package info (click to toggle)
octave 2.0.16-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 26,276 kB
  • ctags: 16,450
  • sloc: cpp: 67,548; fortran: 41,514; ansic: 26,682; sh: 7,361; makefile: 4,077; lex: 2,008; yacc: 1,849; lisp: 1,702; perl: 1,676; exp: 123
file content (950 lines) | stat: -rw-r--r-- 24,641 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from ./octave.texi on 18 June 1999 -->

<TITLE>GNU Octave - Statements</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_10.html">previous</A>, <A HREF="octave_12.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC78" HREF="octave_toc.html#TOC78">Statements</A></H1>
<P>
<A NAME="IDX392"></A>

</P>
<P>
Statements may be a simple constant expression or a complicated list of
nested loops and conditional statements.

</P>
<P>
<STRONG>Control statements</STRONG> such as <CODE>if</CODE>, <CODE>while</CODE>, and so on
control the flow of execution in Octave programs.  All the control
statements start with special keywords such as <CODE>if</CODE> and
<CODE>while</CODE>, to distinguish them from simple expressions.
Many control statements contain other statements; for example, the
<CODE>if</CODE> statement contains another statement which may or may not be
executed.

</P>
<P>
<A NAME="IDX393"></A>
Each control statement has a corresponding <STRONG>end</STRONG> statement that
marks the end of the end of the control statement.  For example, the
keyword <CODE>endif</CODE> marks the end of an <CODE>if</CODE> statement, and
<CODE>endwhile</CODE> marks the end of a <CODE>while</CODE> statement.  You can use
the keyword <CODE>end</CODE> anywhere a more specific end keyword is expected,
but using the more specific keywords is preferred because if you use
them, Octave is able to provide better diagnostics for mismatched or
missing end tokens.

</P>
<P>
The list of statements contained between keywords like <CODE>if</CODE> or
<CODE>while</CODE> and the corresponding end statement is called the
<STRONG>body</STRONG> of a control statement.

</P>



<H2><A NAME="SEC79" HREF="octave_toc.html#TOC79">The <CODE>if</CODE> Statement</A></H2>
<P>
<A NAME="IDX394"></A>
<A NAME="IDX395"></A>
<A NAME="IDX396"></A>
<A NAME="IDX397"></A>

</P>
<P>
The <CODE>if</CODE> statement is Octave's decision-making statement.  There
are three basic forms of an <CODE>if</CODE> statement.  In its simplest form,
it looks like this:

</P>

<PRE>
if (<VAR>condition</VAR>)
  <VAR>then-body</VAR>
endif
</PRE>

<P>
<VAR>condition</VAR> is an expression that controls what the rest of the
statement will do.  The <VAR>then-body</VAR> is executed only if
<VAR>condition</VAR> is true.

</P>
<P>
The condition in an <CODE>if</CODE> statement is considered true if its value
is non-zero, and false if its value is zero.  If the value of the
conditional expression in an <CODE>if</CODE> statement is a vector or a
matrix, it is considered true only if <EM>all</EM> of the elements are
non-zero.

</P>
<P>
The second form of an if statement looks like this:

</P>

<PRE>
if (<VAR>condition</VAR>)
  <VAR>then-body</VAR>
else
  <VAR>else-body</VAR>
endif
</PRE>

<P>
If <VAR>condition</VAR> is true, <VAR>then-body</VAR> is executed; otherwise,
<VAR>else-body</VAR> is executed.

</P>
<P>
Here is an example:

</P>

<PRE>
if (rem (x, 2) == 0)
  printf ("x is even\n");
else
  printf ("x is odd\n");
endif
</PRE>

<P>
In this example, if the expression <CODE>rem (x, 2) == 0</CODE> is true (that
is, the value of <CODE>x</CODE> is divisible by 2), then the first
<CODE>printf</CODE> statement is evaluated, otherwise the second <CODE>printf</CODE>
statement is evaluated.

</P>
<P>
The third and most general form of the <CODE>if</CODE> statement allows
multiple decisions to be combined in a single statement.  It looks like
this:

</P>

<PRE>
if (<VAR>condition</VAR>)
  <VAR>then-body</VAR>
elseif (<VAR>condition</VAR>)
  <VAR>elseif-body</VAR>
else
  <VAR>else-body</VAR>
endif
</PRE>

<P>
Any number of <CODE>elseif</CODE> clauses may appear.  Each condition is
tested in turn, and if one is found to be true, its corresponding
<VAR>body</VAR> is executed.  If none of the conditions are true and the
<CODE>else</CODE> clause is present, its body is executed.  Only one
<CODE>else</CODE> clause may appear, and it must be the last part of the
statement.

</P>
<P>
In the following example, if the first condition is true (that is, the
value of <CODE>x</CODE> is divisible by 2), then the first <CODE>printf</CODE>
statement is executed.  If it is false, then the second condition is
tested, and if it is true (that is, the value of <CODE>x</CODE> is divisible
by 3), then the second <CODE>printf</CODE> statement is executed.  Otherwise,
the third <CODE>printf</CODE> statement is performed.

</P>

<PRE>
if (rem (x, 2) == 0)
  printf ("x is even\n");
elseif (rem (x, 3) == 0)
  printf ("x is odd and divisible by 3\n");
else
  printf ("x is odd\n");
endif
</PRE>

<P>
Note that the <CODE>elseif</CODE> keyword must not be spelled <CODE>else if</CODE>,
as is allowed in Fortran.  If it is, the space between the <CODE>else</CODE>
and <CODE>if</CODE> will tell Octave to treat this as a new <CODE>if</CODE>
statement within another <CODE>if</CODE> statement's <CODE>else</CODE> clause.  For
example, if you write

</P>

<PRE>
if (<VAR>c1</VAR>)
  <VAR>body-1</VAR>
else if (<VAR>c2</VAR>)
  <VAR>body-2</VAR>
endif
</PRE>

<P>
Octave will expect additional input to complete the first <CODE>if</CODE>
statement.  If you are using Octave interactively, it will continue to
prompt you for additional input.  If Octave is reading this input from a
file, it may complain about missing or mismatched <CODE>end</CODE> statements,
or, if you have not used the more specific <CODE>end</CODE> statements
(<CODE>endif</CODE>, <CODE>endfor</CODE>, etc.), it may simply produce incorrect
results, without producing any warning messages.

</P>
<P>
It is much easier to see the error if we rewrite the statements above
like this,

</P>

<PRE>
if (<VAR>c1</VAR>)
  <VAR>body-1</VAR>
else
  if (<VAR>c2</VAR>)
    <VAR>body-2</VAR>
  endif
</PRE>

<P>
using the indentation to show how Octave groups the statements.
See section <A HREF="octave_12.html#SEC89">Functions and Script Files</A>.

</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>warn_assign_as_truth_value</B>
<DD><A NAME="IDX398"></A>
If the value of <CODE>warn_assign_as_truth_value</CODE> is nonzero, a
warning is issued for statements like

</P>

<PRE>
if (s = t)
  ...
</PRE>

<P>
since such statements are not common, and it is likely that the intent
was to write

</P>

<PRE>
if (s == t)
  ...
</PRE>

<P>
instead.

</P>
<P>
There are times when it is useful to write code that contains
assignments within the condition of a <CODE>while</CODE> or <CODE>if</CODE>
statement.  For example, statements like

</P>

<PRE>
while (c = getc())
  ...
</PRE>

<P>
are common in C programming.

</P>
<P>
It is possible to avoid all warnings about such statements by setting
<CODE>warn_assign_as_truth_value</CODE> to 0, but that may also
let real errors like

</P>

<PRE>
if (x = 1)  # intended to test (x == 1)!
  ...
</PRE>

<P>
slip by.

</P>
<P>
In such cases, it is possible suppress errors for specific statements by
writing them with an extra set of parentheses.  For example, writing the
previous example as

</P>

<PRE>
while ((c = getc()))
  ...
</PRE>

<P>
will prevent the warning from being printed for this statement, while
allowing Octave to warn about other assignments used in conditional
contexts.

</P>
<P>
The default value of <CODE>warn_assign_as_truth_value</CODE> is 1.
</DL>

</P>


<H2><A NAME="SEC80" HREF="octave_toc.html#TOC80">The <CODE>switch</CODE> Statement</A></H2>
<P>
<A NAME="IDX399"></A>
<A NAME="IDX400"></A>
<A NAME="IDX401"></A>
<A NAME="IDX402"></A>

</P>
<P>
The <CODE>switch</CODE> statement was introduced in Octave 2.0.5.  It should
be considered experimental, and details of the implementation may change
slightly in future versions of Octave.  If you have comments or would
like to share your experiences in trying to use this new command in real
programs, please send them to
@email{octave-maintainers@bevo.che.wisc.edu}.  (But if you think you've
found a bug, please report it to @email{bug-octave@bevo.che.wisc.edu}.

</P>
<P>
The general form of the <CODE>switch</CODE> statement is

</P>

<PRE>
switch <VAR>expression</VAR>
  case <VAR>label</VAR>
    <VAR>command_list</VAR>
  case <VAR>label</VAR>
    <VAR>command_list</VAR>
  ...

  otherwise
    <VAR>command_list</VAR>
endswitch
</PRE>


<UL>
<LI>

The identifiers <CODE>switch</CODE>, <CODE>case</CODE>, <CODE>otherwise</CODE>, and
<CODE>endswitch</CODE> are now keywords. 

<LI>

The <VAR>label</VAR> may be any expression.

<LI>

Duplicate <VAR>label</VAR> values are not detected.  The <VAR>command_list</VAR>
corresponding to the first match will be executed.

<LI>

You must have at least one <CODE>case <VAR>label</VAR> <VAR>command_list</VAR></CODE>
clause.

<LI>

The <CODE>otherwise <VAR>command_list</VAR></CODE> clause is optional.

<LI>

As with all other specific <CODE>end</CODE> keywords, <CODE>endswitch</CODE> may be
replaced by <CODE>end</CODE>, but you can get better diagnostics if you use
the specific forms.

<LI>

Cases are exclusive, so they don't `fall through' as do the cases
in the switch statement of the C language.

<LI>

The <VAR>command_list</VAR> elements are not optional.  Making the list
optional would have meant requiring a separator between the label and
the command list.  Otherwise, things like


<PRE>
switch (foo)
  case (1) -2
  ...
</PRE>

would produce surprising results, as would


<PRE>
switch (foo)
  case (1)
  case (2)
    doit ();
  ...
</PRE>

particularly for C programmers.

<LI>

The implementation is simple-minded and currently offers no real
performance improvement over an equivalent <CODE>if</CODE> block, even if all
the labels are integer constants.  Perhaps a future variation on this
could detect all constant integer labels and improve performance by
using a jump table.
</UL>

<P>
<DL>
<DT><U>Built-in Variable:</U> <B>warn_variable_switch_label</B>
<DD><A NAME="IDX403"></A>
If the value of this variable is nonzero, Octave will print a warning if
a switch label is not a constant or constant expression
</DL>

</P>


<H2><A NAME="SEC81" HREF="octave_toc.html#TOC81">The <CODE>while</CODE> Statement</A></H2>
<P>
<A NAME="IDX404"></A>
<A NAME="IDX405"></A>
<A NAME="IDX406"></A>
<A NAME="IDX407"></A>

</P>
<P>
In programming, a <STRONG>loop</STRONG> means a part of a program that is (or at least can
be) executed two or more times in succession.

</P>
<P>
The <CODE>while</CODE> statement is the simplest looping statement in Octave.
It repeatedly executes a statement as long as a condition is true.  As
with the condition in an <CODE>if</CODE> statement, the condition in a
<CODE>while</CODE> statement is considered true if its value is non-zero, and
false if its value is zero.  If the value of the conditional expression
in a <CODE>while</CODE> statement is a vector or a matrix, it is considered
true only if <EM>all</EM> of the elements are non-zero.

</P>
<P>
Octave's <CODE>while</CODE> statement looks like this:

</P>

<PRE>
while (<VAR>condition</VAR>)
  <VAR>body</VAR>
endwhile
</PRE>

<P>
Here <VAR>body</VAR> is a statement or list of statements that we call the
<STRONG>body</STRONG> of the loop, and <VAR>condition</VAR> is an expression that
controls how long the loop keeps running.

</P>
<P>
The first thing the <CODE>while</CODE> statement does is test <VAR>condition</VAR>.
If <VAR>condition</VAR> is true, it executes the statement <VAR>body</VAR>.  After
<VAR>body</VAR> has been executed, <VAR>condition</VAR> is tested again, and if it
is still true, <VAR>body</VAR> is executed again.  This process repeats until
<VAR>condition</VAR> is no longer true.  If <VAR>condition</VAR> is initially
false, the body of the loop is never executed.

</P>
<P>
This example creates a variable <CODE>fib</CODE> that contains the first ten
elements of the Fibonacci sequence.

</P>

<PRE>
fib = ones (1, 10);
i = 3;
while (i &#60;= 10)
  fib (i) = fib (i-1) + fib (i-2);
  i++;
endwhile
</PRE>

<P>
Here the body of the loop contains two statements.

</P>
<P>
The loop works like this: first, the value of <CODE>i</CODE> is set to 3.
Then, the <CODE>while</CODE> tests whether <CODE>i</CODE> is less than or equal to
10.  This is the case when <CODE>i</CODE> equals 3, so the value of the
<CODE>i</CODE>-th element of <CODE>fib</CODE> is set to the sum of the previous two
values in the sequence.  Then the <CODE>i++</CODE> increments the value of
<CODE>i</CODE> and the loop repeats.  The loop terminates when <CODE>i</CODE>
reaches 11.

</P>
<P>
A newline is not required between the condition and the
body; but using one makes the program clearer unless the body is very
simple.

</P>
<P>
See section <A HREF="octave_11.html#SEC79">The <CODE>if</CODE> Statement</A> for a description of the variable
<CODE>warn_assign_as_truth_value</CODE>.

</P>


<H2><A NAME="SEC82" HREF="octave_toc.html#TOC82">The <CODE>for</CODE> Statement</A></H2>
<P>
<A NAME="IDX408"></A>
<A NAME="IDX409"></A>

</P>
<P>
The <CODE>for</CODE> statement makes it more convenient to count iterations of a
loop.  The general form of the <CODE>for</CODE> statement looks like this:

</P>

<PRE>
for <VAR>var</VAR> = <VAR>expression</VAR>
  <VAR>body</VAR>
endfor
</PRE>

<P>
where <VAR>body</VAR> stands for any statement or list of statements,
<VAR>expression</VAR> is any valid expression, and <VAR>var</VAR> may take several
forms.  Usually it is a simple variable name or an indexed variable.  If
the value of <VAR>expression</VAR> is a structure, <VAR>var</VAR> may also be a
list.  See section <A HREF="octave_11.html#SEC83">Looping Over Structure Elements</A>, below.

</P>
<P>
The assignment expression in the <CODE>for</CODE> statement works a bit
differently than Octave's normal assignment statement.  Instead of
assigning the complete result of the expression, it assigns each column
of the expression to <VAR>var</VAR> in turn.  If <VAR>expression</VAR> is a range,
a row vector, or a scalar, the value of <VAR>var</VAR> will be a scalar each
time the loop body is executed.  If <VAR>var</VAR> is a column vector or a
matrix, <VAR>var</VAR> will be a column vector each time the loop body is
executed.

</P>
<P>
The following example shows another way to create a vector containing
the first ten elements of the Fibonacci sequence, this time using the
<CODE>for</CODE> statement:

</P>

<PRE>
fib = ones (1, 10);
for i = 3:10
  fib (i) = fib (i-1) + fib (i-2);
endfor
</PRE>

<P>
This code works by first evaluating the expression <CODE>3:10</CODE>, to
produce a range of values from 3 to 10 inclusive.  Then the variable
<CODE>i</CODE> is assigned the first element of the range and the body of the
loop is executed once.  When the end of the loop body is reached, the
next value in the range is assigned to the variable <CODE>i</CODE>, and the
loop body is executed again.  This process continues until there are no
more elements to assign.

</P>
<P>
Although it is possible to rewrite all <CODE>for</CODE> loops as <CODE>while</CODE>
loops, the Octave language has both statements because often a
<CODE>for</CODE> loop is both less work to type and more natural to think of.
Counting the number of iterations is very common in loops and it can be
easier to think of this counting as part of looping rather than as
something to do inside the loop.

</P>



<H3><A NAME="SEC83" HREF="octave_toc.html#TOC83">Looping Over Structure Elements</A></H3>
<P>
<A NAME="IDX410"></A>
<A NAME="IDX411"></A>

</P>
<P>
A special form of the <CODE>for</CODE> statement allows you to loop over all
the elements of a structure:

</P>

<PRE>
for [ <VAR>val</VAR>, <VAR>key</VAR> ] = <VAR>expression</VAR>
  <VAR>body</VAR>
endfor
</PRE>

<P>
In this form of the <CODE>for</CODE> statement, the value of <VAR>expression</VAR>
must be a structure.  If it is, <VAR>key</VAR> and <VAR>val</VAR> are set to the
name of the element and the corresponding value in turn, until there are
no more elements. For example,

</P>

<PRE>
x.a = 1
x.b = [1, 2; 3, 4]
x.c = "string"
for [val, key] = x
  key
  val
endfor

     -| key = a
     -| val = 1
     -| key = b
     -| val =
     -| 
     -|   1  2
     -|   3  4
     -| 
     -| key = c
     -| val = string
</PRE>

<P>
The elements are not accessed in any particular order.  If you need to
cycle through the list in a particular way, you will have to use the
function <CODE>struct_elements</CODE> and sort the list yourself.

</P>
<P>
The <VAR>key</VAR> variable may also be omitted.  If it is, the brackets are
also optional.  This is useful for cycling through the values of all the
structure elements when the names of the elements do not need to be
known.

</P>


<H2><A NAME="SEC84" HREF="octave_toc.html#TOC84">The <CODE>break</CODE> Statement</A></H2>
<P>
<A NAME="IDX412"></A>

</P>
<P>
The <CODE>break</CODE> statement jumps out of the innermost <CODE>for</CODE> or
<CODE>while</CODE> loop that encloses it.  The <CODE>break</CODE> statement may only
be used within the body of a loop.  The following example finds the
smallest divisor of a given integer, and also identifies prime numbers:

</P>

<PRE>
num = 103;
div = 2;
while (div*div &#60;= num)
  if (rem (num, div) == 0)
    break;
  endif
  div++;
endwhile
if (rem (num, div) == 0)
  printf ("Smallest divisor of %d is %d\n", num, div)
else
  printf ("%d is prime\n", num);
endif
</PRE>

<P>
When the remainder is zero in the first <CODE>while</CODE> statement, Octave
immediately <STRONG>breaks out</STRONG> of the loop.  This means that Octave
proceeds immediately to the statement following the loop and continues
processing.  (This is very different from the <CODE>exit</CODE> statement
which stops the entire Octave program.)

</P>
<P>
Here is another program equivalent to the previous one.  It illustrates
how the <VAR>condition</VAR> of a <CODE>while</CODE> statement could just as well
be replaced with a <CODE>break</CODE> inside an <CODE>if</CODE>:

</P>

<PRE>
num = 103;
div = 2;
while (1)
  if (rem (num, div) == 0)
    printf ("Smallest divisor of %d is %d\n", num, div);
    break;
  endif
  div++;
  if (div*div &#62; num)
    printf ("%d is prime\n", num);
    break;
  endif
endwhile
</PRE>



<H2><A NAME="SEC85" HREF="octave_toc.html#TOC85">The <CODE>continue</CODE> Statement</A></H2>
<P>
<A NAME="IDX413"></A>

</P>
<P>
The <CODE>continue</CODE> statement, like <CODE>break</CODE>, is used only inside
<CODE>for</CODE> or <CODE>while</CODE> loops.  It skips over the rest of the loop
body, causing the next cycle around the loop to begin immediately.
Contrast this with <CODE>break</CODE>, which jumps out of the loop altogether.
Here is an example:

</P>

<PRE>
# print elements of a vector of random
# integers that are even.

# first, create a row vector of 10 random
# integers with values between 0 and 100:

vec = round (rand (1, 10) * 100);

# print what we're interested in:

for x = vec
  if (rem (x, 2) != 0)
    continue;
  endif
  printf ("%d\n", x);
endfor
</PRE>

<P>
If one of the elements of <VAR>vec</VAR> is an odd number, this example skips
the print statement for that element, and continues back to the first
statement in the loop.

</P>
<P>
This is not a practical example of the <CODE>continue</CODE> statement, but it
should give you a clear understanding of how it works.  Normally, one
would probably write the loop like this:

</P>

<PRE>
for x = vec
  if (rem (x, 2) == 0)
    printf ("%d\n", x);
  endif
endfor
</PRE>



<H2><A NAME="SEC86" HREF="octave_toc.html#TOC86">The <CODE>unwind_protect</CODE> Statement</A></H2>
<P>
<A NAME="IDX414"></A>
<A NAME="IDX415"></A>
<A NAME="IDX416"></A>

</P>
<P>
Octave supports a limited form of exception handling modelled after the
unwind-protect form of Lisp.  

</P>
<P>
The general form of an <CODE>unwind_protect</CODE> block looks like this:

</P>

<PRE>
unwind_protect
  <VAR>body</VAR>
unwind_protect_cleanup
  <VAR>cleanup</VAR>
end_unwind_protect
</PRE>

<P>
Where <VAR>body</VAR> and <VAR>cleanup</VAR> are both optional and may contain any
Octave expressions or commands.  The statements in <VAR>cleanup</VAR> are 
guaranteed to be executed regardless of how control exits <VAR>body</VAR>.

</P>
<P>
This is useful to protect temporary changes to global variables from
possible errors.  For example, the following code will always restore
the original value of the built-in variable <CODE>do_fortran_indexing</CODE>
even if an error occurs while performing the indexing operation.

</P>

<PRE>
save_do_fortran_indexing = do_fortran_indexing;
unwind_protect
  do_fortran_indexing = 1;
  elt = a (idx)
unwind_protect_cleanup
  do_fortran_indexing = save_do_fortran_indexing;
end_unwind_protect
</PRE>

<P>
Without <CODE>unwind_protect</CODE>, the value of <VAR>do_fortran_indexing</VAR>
would not be restored if an error occurs while performing the indexing
operation because evaluation would stop at the point of the error and
the statement to restore the value would not be executed.

</P>


<H2><A NAME="SEC87" HREF="octave_toc.html#TOC87">The <CODE>try</CODE> Statement</A></H2>
<P>
<A NAME="IDX417"></A>
<A NAME="IDX418"></A>
<A NAME="IDX419"></A>

</P>
<P>
In addition to unwind_protect, Octave supports another limited form of
exception handling.

</P>
<P>
The general form of a <CODE>try</CODE> block looks like this:

</P>

<PRE>
try
  <VAR>body</VAR>
catch
  <VAR>cleanup</VAR>
end_try_catch
</PRE>

<P>
Where <VAR>body</VAR> and <VAR>cleanup</VAR> are both optional and may contain any
Octave expressions or commands.  The statements in <VAR>cleanup</VAR> are
only executed if an error occurs in <VAR>body</VAR>.

</P>
<P>
No warnings or error messages are printed while <VAR>body</VAR> is
executing.  If an error does occur during the execution of <VAR>body</VAR>,
<VAR>cleanup</VAR> can access the text of the message that would have been
printed in the builtin constant <CODE>__error_text__</CODE>.  This is the same
as <CODE>eval (<VAR>try</VAR>, <VAR>catch</VAR>)</CODE> (which may now also use
<CODE>__error_text__</CODE>) but it is more efficient since the commands do
not need to be parsed each time the <VAR>try</VAR> and <VAR>catch</VAR> statements
are evaluated.  See section <A HREF="octave_13.html#SEC99">Error Handling</A>, for more information about the
<CODE>__error_text__</CODE> variable.

</P>
<P>
Octave's <VAR>try</VAR> block is a very limited variation on the Lisp
condition-case form (limited because it cannot handle different classes
of errors separately).  Perhaps at some point Octave can have some sort
of classification of errors and try-catch can be improved to be as
powerful as condition-case in Lisp.

</P>
<P>
<A NAME="IDX420"></A>
<A NAME="IDX421"></A>
<A NAME="IDX422"></A>

</P>


<H2><A NAME="SEC88" HREF="octave_toc.html#TOC88">Continuation Lines</A></H2>

<P>
In the Octave language, most statements end with a newline character and
you must tell Octave to ignore the newline character in order to
continue a statement from one line to the next.  Lines that end with the
characters <CODE>...</CODE> or <CODE>\</CODE> are joined with the following line
before they are divided into tokens by Octave's parser.  For example,
the lines

</P>

<PRE>
x = long_variable_name ...
    + longer_variable_name \
    - 42
</PRE>

<P>
form a single statement.  The backslash character on the second line
above is interpreted a continuation character, <EM>not</EM> as a division
operator.

</P>
<P>
For continuation lines that do not occur inside string constants,
whitespace and comments may appear between the continuation marker and
the newline character.  For example, the statement

</P>

<PRE>
x = long_variable_name ...     # comment one
    + longer_variable_name \   # comment two
    - 42                       # last comment
</PRE>

<P>
is equivalent to the one shown above.  Inside string constants, the
continuation marker must appear at the end of the line just before the
newline character.

</P>
<P>
Input that occurs inside parentheses can be continued to the next line
without having to use a continuation marker.  For example, it is
possible to write statements like

</P>

<PRE>
if (fine_dining_destination == on_a_boat
    || fine_dining_destination == on_a_train)
  suess (i, will, not, eat, them, sam, i, am, i,
         will, not, eat, green, eggs, and, ham);
endif
</PRE>

<P>
without having to add to the clutter with continuation markers.

</P>
<P><HR><P>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_10.html">previous</A>, <A HREF="octave_12.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
</BODY>
</HTML>