12.07.2015 Views

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12: Statements 87# 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 = vecif (rem (x, 2) != 0)continue;endifprintf ("%d\n", x);endforIf one of the elements of vec is an odd number, this example skips the print statementfor that element, and continues back to the first statement in the loop.This is not a practical example of the continue statement, but it should give you a clearunderstanding of how it works. Normally, one would probably write the loop like this:for x = vecif (rem (x, 2) == 0)printf ("%d\n", x);endifendfor12.8 The unwind_protect Statement<strong>Octave</strong> supports a limited form of exception handling modelled after the unwind-protectform of Lisp.The general form of an unwind_protect block looks like this:unwind_protectbodyunwind_protect_cleanupcleanupend_unwind_protectWhere body and cleanup are both optional and may contain any <strong>Octave</strong> expressions orcommands. The statements in cleanup are guaranteed to be executed regardless of howcontrol exits body.This is useful to protect temporary changes to global variables from possible errors. Forexample, the following code will always restore the original value of the built-in variablewarn_fortran_indexing even if an error occurs while performing the indexing operation.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!