if - Execute statements if condition is true

Syntax

if expression, statements, end

Description

if expression, statements, end evaluates expression and, if the evaluation yields logical 1 (true) or a nonzero result, executes one or more MATLAB commands denoted here as statements.

expression is a MATLAB expression, usually consisting of variables or smaller expressions joined by relational operators (e.g., count < limit), or logical functions (e.g., isreal(A)). Simple expressions can be combined by logical operators (&&, ||, ~) into compound expressions such as the following. MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules.

(count < limit) && ((height - offset) >= 0)

Nested if statements must each be paired with a matching end.

The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if statement, the general form of the statement is

if expression1
    statements1
elseif expression2
    statements2
else
    statements3
end

See Program Control Statements in the MATLAB Programming Fundamentals documentation for more information on controlling the flow of your program code.

Remarks

Nonscalar Expressions

If the evaluated expression yields a nonscalar value, then every element of this value must be true or nonzero for the entire expression to be considered true. For example, the statement if (A < B) is true only if each element of matrix A is less than its corresponding element in matrix B. See Example 2, below.

Partial Evaluation of the expression Argument

Within the context of an if or while expression, MATLAB does not necessarily evaluate all parts of a logical expression. In some cases it is possible, and often advantageous, to determine whether an expression is true or false through only partial evaluation.

For example, if A equals zero in statement 1 below, then the expression evaluates to false, regardless of the value of B. In this case, there is no need to evaluate B and MATLAB does not do so. In statement 2, if A is nonzero, then the expression is true, regardless of B. Again, MATLAB does not evaluate the latter part of the expression.

1)   if (A && B)               2)   if (A || B)

You can use this property to your advantage to cause MATLAB to evaluate a part of an expression only if a preceding part evaluates to the desired state. Here are some examples.

while (b ~= 0) && (a/b > 18.5)
if exist('myfun.m') && (myfun(x) >= y)
if iscell(A) && all(cellfun('isreal', A))

Empty Arrays

In most cases, using if on an empty array treats the array as false. There are some conditions however under which if evaluates as true on an empty array. Two examples of this, where A is equal to [], are

if all(A), do_something, end
if 1|A, do_something, end

The latter expression is true because of short-circuiting, which causes MATLAB to ignore the right side operand of an OR statement whenever the left side evaluates to true.

Short-Circuiting Behavior

When used in the context of an if or while expression, and only in this context, the element-wise | and & operators use short-circuiting in evaluating their expressions. That is, A|B and A&B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.

See Short-Circuiting in Elementwise Operators for more information on this.

Examples

Example 1 - Simple if Statement

In this example, if both of the conditions are satisfied, then the student passes the course.

if ((attendance >= 0.90) && (grade_average >= 60))
   pass = 1;
end;

Example 2 - Nonscalar Expression

Given matrices A and B,

A =                 B =
     1     0            1     1
     2     3            3     4

Expression

Evaluates As

Because

A < B

false

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

A < (B + 1)

true

Every element of A is less than that same element of B with 1 added.

A & B

false

A(1,2) is false, and B is ignored due to short-circuiting.

B < 5

true

Every element of B is less than 5.

See Also

else, elseif, end, for, while, switch, break, return, relational operators, logical operators (elementwise and short-circuit),

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS