F.1 Keywords

The function iskeyword can be used to check whether an identifier is reserved by Octave.

 
tf = iskeyword (name)
keyword_list = iskeyword ()

Return true if name is an Octave keyword.

If name is omitted, return a list of keywords.

See also: isvarname, exist.

Keyword Categories

Variable Declaration | Function Definition | Control Statements | Iterating Structures | Classdef Structures | Execution Environment

Alphabetical Keyword Listing

The identifiers below are keywords, and may not be used as variable or function names.

__FILE__ | __LINE__ | arguments | break | case | catch | classdef | continue | do | else | elseif | end | end_try_catch | end_unwind_protect | endarguments | endclassdef | endenumeration | endevents | endfor | endfunction | endif | endmethods | endparfor | endproperties | endswitch | endwhile | enumeration | events | for | function | global | if | methods | otherwise | parfor | persistent | properties | return | switch | try | until | unwind_protect | unwind_protect_cleanup | while

Variable Declaration

 
global var

Declare variables to have global scope.

global x;
if (isempty (x))
  x = 1;
endif

See also: persistent.

 
persistent var

Declare variables as persistent.

A variable that has been declared persistent within a function will retain its contents in memory between subsequent calls to the same function. The difference between persistent variables and global variables is that persistent variables are local in scope to a particular function and are not visible elsewhere.

See also: global.

Function Definition

 
function [output1, …] = function_name (input1, …)
function function_name (input1, …)
function output = function_name

Begin a function body with name function_name, with outputs as results, and with inputs as parameters.

The function can later be invoked in Octave using the syntax

[output1, output2, ...] = function_name (input1, input2, ...)

See also: return.

 
endfunction

Terminate a function definition.

See also: function.

 
end

The termination of a code block or the last element of an array.

The keyword end is used to terminate blocks of code begun with for, parfor, if, do, while, function, switch, try, or unwind_protect.

When using classdef (object-oriented) programming, the keyword terminates blocks of code begun with classdef, properties, methods, events, or enumeration.

As an index of an array, the magic index "end" refers to the last valid entry in an indexing operation.

Examples:

x = [ 1 2 3; 4 5 6 ];
x(1,end)
 ⇒  3
x(end,1)
 ⇒  4
x(end,end)
 ⇒  6

Programming Notes:

  1. The end keyword cannot be used within subsref, subsasgn, or substruct for manual indexing operations.
  2. For custom classes, enabling use of end in indexing expressions requires writing an overloaded function such as:
    function last_index = end (obj, end_dim, ndim_obj)
      if (end_dim == ndim_obj)
        last_index = prod (size (obj)(end_dim:ndim_obj));
      else
        last_index = size (obj, end_dim);
      endif
    endfunction
    

    For more information, see Object Oriented Programming.

See also: for, parfor, if, do, while, function, switch, try, unwind_protect.

 
return

Return execution control immediately from a function or script to the calling code.

return is used to stop executing code and exit an m-file immediately rather than continuing until the end of the function or script is reached.

If the function or script was invoked directly, rather than from calling code in an m-file, then Octave returns to the command line.

See also: function.

 
arguments

Begin a function arguments block.

Warning: Function arguments validation blocks are not yet implemented in Octave. Use of the keyword arguments will cause the parser to emit a warning, and may throw an error depending on the contents of the code in the block.

 
endarguments

Terminate a function arguments block.

Warning: Function arguments validation blocks are not yet implemented in Octave. Use of the keyword arguments will cause the parser to emit a warning, and may throw an error depending on the contents of the code in the block.

See also: arguments.

Control Statements

 
if (cond) … endif
if (cond) … else … endif
if (cond) … elseif (cond) … endif
if (cond) … elseif (cond) … else … endif

Begin an if block.

The conditional cond is true if it is not empty and if all values are nonzero.

x = 1;
if (x == 1)
  disp ("one");
elseif (x == 2)
  disp ("two");
else
  disp ("not one or two");
endif

See also: switch.

 
else

Alternate action for an if block.

See if for an example.

See also: if.

 
elseif (cond)

Alternate conditional test for an if block.

The conditional cond is true if it is not empty and if all values are nonzero.

See if for an example.

See also: if.

 
endif

Terminate an if block.

See if for an example.

See also: if.

 
switch statement

Begin a switch block.

yesno = "yes";

switch (yesno)
  case {"Yes" "yes" "YES" "y" "Y"}
    value = 1;
  case {"No" "no" "NO" "n" "N"}
    value = 0;
  otherwise
    error ("invalid value");
endswitch

See also: if, case, otherwise.

 
case value
case {value1, …}

A case statement in a switch block.

The code associated with a case statement is executed if one of the tests below succeeds given a switch argument statement and a value of type:

  • number : statement == value
  • character array or string : strcmp (statement, value)
  • object : eq (statement, value)
    (programmer must write overloaded eq function which performs ‘==’)
  • cell array : One of the values in the cell array must match statement using the criteria above.

Programming Notes: Octave cases are exclusive and do not fall-through as do C-language cases. A switch statement must have at least one case.

See switch for an example.

See also: switch.

 
otherwise

The default statement in a switch block which is executed when no other case statements match the input.

See also: switch, case.

 
endswitch

Terminate a switch block.

See switch for an example.

See also: switch.

 
try

Begin a try-catch block.

If an error occurs within a try block, then the code in the catch block will be run. Execution will proceed after the catch block (though it is often recommended to use the lasterr function to re-throw the error after cleanup is completed).

See also: catch, unwind_protect.

 
catch
catch exception_var

Begin the cleanup part of a try-catch block.

If exception_var is specified then a variable of that name will defined in the scope of the catch block. The variable is a scalar struct with fields identifier, message, and stack which describes the error that was caught.

See also: try.

 
end_try_catch

Terminate a try-catch block.

See also: try, catch.

 
unwind_protect

Begin an unwind_protect block.

If an error occurs within the first part of an unwind_protect block the commands within the unwind_protect_cleanup block are executed before the error is thrown. If an error is not thrown, then the unwind_protect_cleanup block is still executed. In other words, the unwind_protect_cleanup code is guaranteed to execute regardless of success or failure in the unwind_protect block.

See also: unwind_protect_cleanup, try.

 
unwind_protect_cleanup

Begin the cleanup section of an unwind_protect block.

See also: unwind_protect.

 
end_unwind_protect

Terminate an unwind_protect block.

See also: unwind_protect.

Iterating Structures

 
for i = range

Begin a for loop.

for i = 1:10
  i
endfor

See also: parfor, do, while.

 
endfor

Terminate a for loop.

See for for an example.

See also: for.

 
while (cond)

Begin a while loop.

The conditional cond is true if it is not empty and if all values are nonzero.

i = 0;
while (i < 10)
  i++
endwhile

See also: do, endwhile, for, until.

 
endwhile

Terminate a while loop.

See while for an example.

See also: do, while.

 
do

Begin a do-until loop.

This differs from a while loop in that the body of the loop is executed at least once.

i = 0;
do
  i++
until (i == 10)

See also: for, until, while.

 
until (cond)

End a do-until loop.

The conditional cond is true if it is not empty and if all values are nonzero.

See do for an example.

See also: do.

 
parfor i = range
parfor (i = range, maxproc)

Begin a for loop that may execute in parallel.

A parfor loop has the same syntax as a for loop. If your Octave session has a parallel processing pool enabled, the iterations of the parfor loop will be executed in parallel across the pool’s workers. Otherwise, parfor will behave exactly as for.

When operating in parallel mode, a parfor loop’s iterations are not guaranteed to occur sequentially, and there are additional restrictions about the data access operations you can do inside the loop body.

Warning: parallel processing pools are currently unimplemented in Octave; parfor currently behaves exactly as a normal for loop.

parfor i = 1:10
  i
endparfor

See also: for, do, while.

 
endparfor

Terminate a parfor loop.

See parfor for an example.

See also: parfor.

 
break

Exit the innermost enclosing do, while, or for loop.

See also: continue, do, while, for, parfor.

 
continue

Jump to the end of the innermost enclosing do, while, or for loop.

See also: break, do, while, for, parfor.

Classdef Structures

 
classdef

Begin a classdef block.

See also: properties, methods, events, enumeration.

 
endclassdef

Terminate a classdef definition.

See also: classdef.

 
properties

Mark the beginning of a block of properties in a classdef definition.

Note that the function "properties" is not a keyword, but rather an ordinary function that lists the properties of a classdef class or object.

See also: endproperties.

 
endproperties

Terminate a properties block in a classdef definition.

See also: properties.

 
methods

Mark the beginning of a block of methods in a classdef definition.

Note that the function "methods" is not a keyword, but rather an ordinary function that lists the methods of a class or object.

See also: endmethods.

 
endmethods

Terminate a methods block in a classdef definition.

See also: methods.

 
events

Begin an events block in a classdef definition.

 
endevents

Terminate an events block in a classdef definition.

See also: events.

 
enumeration

Begin an enumeration block in a classdef definition.

 
endenumeration

Terminate an enumeration block in a classdef definition.

See also: enumeration.

Execution Environment

 
__FILE__

When the lexer recognizes the "__FILE__" keyword it returns a character array containing the full name and path of the file that is being executed. "__FILE__" will return "stdin" if called from the command line.

See also: __LINE__.

 
__LINE__

When the lexer recognizes the "__LINE__" keyword it returns a numeric value containing the current input line number of the function or file being executed. "__LINE__" will return 1 if called from the command line.

See also: __FILE__.