MATLAB Function Reference    
if

Conditionally execute statements

Syntax

Description

if conditionally executes statements.

The simple form is:

More complicated forms use else or elseif. Each if must be paired with a matching end.

Arguments
expression
A MATLAB expression, usually consisting of smaller expressions or variables joined by relational operators (==, <, >, <=, >=, or ~=). Two examples are: count < limit and (height - offset) >= 0.
Expressions may also include logical functions, as in: isreal(A).
Simple expressions can be combined by logical operators (&,|,~) into compound expressions such as: (count < limit) & ((height - offset) >= 0).
statements
One or more MATLAB statements to be executed only if the expression is true (or nonzero). See Examples for information about how nonscalar variables are evaluated.

Examples

Here is an example showing if, else, and elseif:

Such expressions are evaluated as false unless every element-wise comparison evaluates as true. Thus, given matrices A and B:

The expression:

A < B

Evaluates as false

Since A(1,1) is not less than B(1,1).

A < (B+1)

Evaluates as true

Since no element of A is greater than the corresponding element of B.

A & B

Evaluates as false

Since A(1,2) | B(1,2) is false.

5 > B

Evaluates as true

Since every element of B is less than 5.

See Also

break, else, end, for, return, switch, while


 i ifft