Written and maintained by CASRAI Editorial Board
Last updated
Quick answer: in MATLAB, if runs a block of statements only when its condition is true, with optional elseif and else branches and a closing end. A condition counts as true when its result is nonempty and contains only nonzero elements; an empty result is false. This page is for researchers and analysts who need to write correct conditions, including ones that combine tests (and, or, not), compare values, or receive arrays. If you are specifically looking for the elseif and else branches, the else if versus elseif distinction and multi-branch examples, see If-Else in MATLAB: if, elseif, else syntax with examples; this page focuses on how MATLAB evaluates the condition itself.
What the if statement is used for in research code
Analysis scripts branch constantly: apply a correction only when a sample size passes a threshold, skip a file when a field is missing, choose a processing path by data type. The if statement is MATLAB’s conditional for those decisions. To repeat work across many items, pair it with the loops covered in Loops in MATLAB: for, while and how to stop them; to select elements of an array without writing a loop at all, the MathWorks documentation notes that logical indexing often outperforms if and loops.
Access: where the official syntax lives
The authoritative reference is the MathWorks documentation page for if (mathworks.com/help/matlab/ref/if.html, read 2026-11-07). The same keyword exists in the free GNU Octave language, documented at docs.octave.org, The if Statement (v9.2.0, read 2026-11-07); the differences are tabulated below.
Step-by-step: writing an if statement
Syntax
The documented form is:
if expression
statements
elseif expression
statements
else
statements
end
elseif and else are optional, and you may use several elseif branches.
- Write the condition. Start with
iffollowed by an expression that evaluates to a logical or numeric result. - Add the statements to run when the condition is true.
- Add alternatives if needed with
elseif expressionfor further tests and a finalelsefor everything else. - Close the block with
end. Each nestedifneeds its ownend. - Write
elseifas one word. Typingelse ifwith a space creates a nestedifinsideelse, which then needs an extraend.
MATLAB if not
Use ~ for logical not, and ~= for “not equal”, for example if ~isempty(x) or if a ~= b.
MATLAB if and / MATLAB and if (multiple conditions)
Combine tests with && (and) and || (or). A range check from the MathWorks examples has the form if (x >= minVal) && (x <= maxVal), optionally followed by an elseif for the remaining cases. Inside an if condition, the single operators & and | short-circuit the same way as && and ||, but the documentation recommends && and || for consistency.
MATLAB if a or b
Write if a || b. Short-circuiting matters when the second operand would produce an error: the documentation’s example tests exist('myfunction.m','file') first so that the function is only called when the file exists.
MATLAB if equal
For scalars, == compares values. For arrays, the documentation prefers isequal, because == errors when the two arrays differ in size. For character vectors, use strcmp.
MATLAB for if
Placing an if inside a for loop is ordinary nesting: the loop supplies each value and the if tests it, with each construct closed by its own end.
Output: how MATLAB reads the condition
An if produces no value of its own; what you read is which branch ran. The branch depends on how the condition is judged true or false:
| Condition result | Treated as | What to use instead if you meant something else |
|---|---|---|
| Nonempty, all elements nonzero (logical or real numeric) | True | Not applicable |
| Empty | False | Test isempty explicitly if emptiness matters |
| Nonscalar array | True only if every element is nonzero | any(...) to test whether at least one element is true, for example if any(A > limit) |
Comparing two arrays with == |
Errors if sizes differ | isequal(A,B) |
| Comparing character vectors | Documentation example uses strcmp |
strcmp |
This is why a “matrix if” often surprises people: if A > 0 on a vector runs only when all elements pass.
Troubleshooting
- Missing
enderror or code running in the wrong place: count theendkeywords against everyif, and check you did not typeelse ifwith a space. - Branch not running for an array: every element must be nonzero; use
anyoralldeliberately. - Error when comparing arrays: switch from
==toisequal. - Error from the second half of an or/and test: use short-circuit
&&or||and put the guarding test first. - Generating C/C++/GPU code: the documentation says not to use
&or|inifconditions for code generation, and that HDL Coder does not support nonscalar conditions, so reduce withallorany. - Selecting elements: if the goal is to filter an array, logical indexing often outperforms
ifwith loops, and the same condition ideas carry over to anonymous functions and cell array handling.
Free alternatives: GNU Octave
GNU Octave documentation (v9.2.0, read 2026-11-07) describes the same construct with these differences from the MATLAB documentation:
| Point | MATLAB | GNU Octave |
|---|---|---|
| Block terminator | end |
endif (or end) |
| Array condition | True only if nonempty and all elements nonzero | True only if nonempty and all elements nonzero (equivalent to all(matrix(:))) |
| Else-if spelling | Use elseif; else if creates a nested if |
elseif must not be spelled else if |
For wider comparisons of the two environments for research, see MATLAB vs R.
FAQ
How do I write “if not” in MATLAB?
Use the ~ operator, as in if ~condition, or ~= for inequality between two values.
How do I test multiple conditions in one if?
Join them with && or ||, or chain branches with elseif.
How do I check equality in an if?
Use == for scalars, isequal for arrays and strcmp for character vectors.
What does if A do when A is a matrix?
It is true only when A is nonempty and every element is nonzero. Use any to test for at least one nonzero element.
Can I write else if with a space?
You can, but it creates a nested if that needs its own end. Write elseif to continue the same block.
Sources (official documentation)
- MathWorks, if: Execute statements if condition is true, read 2026-11-07.
- GNU Octave 9.2.0 manual, The if Statement, read 2026-11-07.
Related CASRAI guides: Fit in MATLAB, MATLAB LineStyle.







