Programming and Data Types |
Flow Control
There are eight flow control statements in MATLAB:
if
, together with else
and elseif
, executes a group of statements based on some logical condition.
switch
, together with case
and otherwise
, executes different groups of statements depending on the value of some logical condition.
while
executes a group of statements an indefinite number of times, based on some logical condition.
for
executes a group of statements a fixed number of times.
continue
passes control to the next iteration of a for
or while
loop, skipping any remaining statements in the body of the loop.
break
terminates execution of a for
or while
loop.
try...
catch
changes flow control if an error is detected during execution.
return
causes execution to return to the invoking function.
All flow constructs use end
to indicate the end of the flow control block.
Note
You can often speed up the execution of MATLAB code by replacing for and while loops with vectorized code. See Techniques for Improving Performance in the MATLAB "Programming and Data Types" documentation.
|
Keywords | if, else, and elseif |