| MATLAB Function Reference | Search  Help Desk |
| if | Examples See Also |
Conditionally execute statements
Syntax
ifexpressionstatementsend ifexpression1statementselseifexpression2statementselsestatementsend
Description
if
conditionally executes statements.
The simple form is:
ifMore complicated forms useexpressionstatementsend
else or elseif. Each if must be paired with a matching end.
Arguments
Examples
Here is an example showingif, else, and elseif:
for i = 1:n
for j = 1:n
if i == j
a(i,j) = 2;
elseif abs([i j]) == 1
a(i,j) = 1;
else
a(i,j) = 0;
end
end
end
Such expressions are evaluated as false unless every element-wise comparison evaluates as true. Thus, given matrices A and B:
A = B =
1 0 1 1
2 3 3 4
The expression:See Also
break, else, end, for, return, switch, while