@c Copyright (C) 1992 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node Statements, Built-in Variables, Expressions, Top
@chapter Statements
@cindex statements

@menu
* Statement Lists::             
* Declaration Statements::      
* Selection Statements::        
* Iteration Statements::        
* Defining Functions::          
* Jump Statements::             
@end menu

@node Statement Lists, Declaration Statements,  , Statements
@section Statement Lists

A statement list is formed from a set of statements, separated by
commas, semicolons, or, in some contexts, newline characters.

@node Declaration Statements, Selection Statements, Statement Lists, Statements
@section Declaration Statements

@noindent
@var{declaration-stmt}:
@example
global @var{global-decl-list}
@end example

@noindent
@var{global-decl-list}:
@example
@var{identifier}
@var{identifier} = @var{expr}
@var{global-decl-list} @var{identifier}
@var{global-decl-list} @var{identifier} = @var{expr}
@var{global-decl-list} , @var{identifier}
@var{global-decl-list} , @var{identifier} = @var{expr}
@end example

@node Selection Statements, Iteration Statements, Declaration Statements, Statements
@section Selection Statements
@cindex if command

A selection statement executes one of several possible lists of
statements.

@noindent
@var{selection-statement}:
@example
if @var{expr} @var{stmt-list} @var{elseif-clause} @var{else-clause} endif
@end example

@noindent
where the @var{elseif-clause} and the @var{else-clause} may be omitted.

@noindent
@var{else-clause}:
@example
else @var{stmt-list}
@end example

@noindent
@var{elseif-clause}:
@example
elseif @var{expr} @var{stmt-list}
@var{elseif-clause} elseif @var{expr} @var{stmt-list}
@end example

Although it is not required, it is a good idea to surround the test
expression in parentheses to make the meaning clear.

@node Iteration Statements, Defining Functions, Selection Statements, Statements
@section Iteration Statements
@cindex for command
@cindex while command

An iteration statements specifies looping.

@noindent
@var{iteration-stmt}:
@example
while @var{expr} @var{stmt-list} endwhile
for @var{identifier} = @var{expr} @var{stmt-list} endfor
@end example

@node Defining Functions, Jump Statements, Iteration Statements, Statements
@section Defining Functions
@cindex function command

The @code{function} statement allows you to define your own functions.
The syntax of the @code{function} statement is:

@noindent
@var{function}:
@example
function @var{ret-list} @var{identifier} @var{arg-list} @var{stmt-list} endfunction
@end example

@noindent
where the @var{ret-list} and @var{arg-list} may be omitted.

@noindent
@var{ret-list}:
@example
@var{identifier} =
[ @var{identifier-list} ] =
@end example

@noindent
@var{arg-list}:
@example
( )
( @var{identifier-list} )
@end example

@noindent
@var{identifier-list}:
@example
@var{identifier}
@var{identifier-list} @var{identifier}
@end example

@node Jump Statements,  , Defining Functions, Statements
@section Jump Statements
@cindex break command
@cindex continue command

A jump statement transfers control unconditionally.

The @code{break} statement terminates execution of the innermost
enclosing loop statement.

The @code{continue} statement transfers control to the end of the
innermost enclosing loop statement.  The condition controlling the
execution of loop is then tested again, and if it is not equal to zero,
the loop body is executed again.

@noindent
@var{jump-stmt}:
@example
break
continue
return
@end example

The @code{break} and @code{continue} statements may only be used within
the body of a loop, and the @code{return} statement may only be used
within a function body.
