| Programming and Data Types | ![]() |
if evaluates a logical expression and executes a group of statements based on the value of the expression. In its simplest form, its syntax is
If the logical expression is true (1), MATLAB executes all the statements between the if and end lines. It resumes execution at the line following the end statement. If the condition is false (0), MATLAB skips all the statements between the if and end lines, and resumes execution at the line following the end statement.
You can nest any number of if statements.
If the logical expression evaluates to a nonscalar value, all the elements of the argument must be nonzero. For example, assume X is a matrix. Then the statement
The else and elseif statements further conditionalize the if statement:
else statement has no logical condition. The statements associated with it execute if the preceding if (and possibly elseif condition) is false (0).
elseif statement has a logical condition that it evaluates if the preceding if (and possibly elseif condition) is false (0). The statements associated with it execute if its logical condition is true (1). You can have multiple elseifs within an if block.
if Statements and Empty Arrays
An if condition that reduces to an empty array represents a false condition. That is,
will execute statement S0 when A is an empty array.
| Flow Control | switch | ![]() |