if, elseif, else Statements
MATLAB's basic conditional structure is the if/elseif/else block, closed with the keyword end (not curly braces). MATLAB evaluates the if condition first; if it is true (nonzero), that block runs and the rest is skipped. Otherwise MATLAB checks each elseif in order, and if none match, the else block runs. A condition is considered true if it evaluates to a nonzero scalar, or if it is a non-empty array, MATLAB requires all elements to be nonzero.
Cricket analogy: an umpire's DRS review decision tree — first check if ball hit bat (if), else check pad-height (elseif), else not out (else), and the review closes at end
Comparison and Logical Operators
MATLAB uses == for equality, ~= for inequality, and <, >, <=, >= for ordering. For combining conditions, use the short-circuit operators && and || inside if statements, which stop evaluating as soon as the result is determined — the second operand is skipped if it's unnecessary. Element-wise & and | always evaluate both sides and are used for array operations, not for scalar control flow inside if.
Cricket analogy: a DLS par-score check using short-circuit logic — if overs_left>0 && required_rate<=12 the match stays alive, and MATLAB skips the second check entirely once overs_left is already zero
switch-case Statements
switch-case is cleaner than a long elseif chain when you're comparing one variable against several discrete values. Each case can match a single value or a cell array of values (so multiple values route to the same block), and an otherwise clause catches anything unmatched. Unlike C, MATLAB's switch does not fall through between cases — only the matching case executes.
Cricket analogy: a switch on match_result routing to case 'won', case 'lost', case 'tied', otherwise 'no result', just like a scorer classifying every possible outcome
function classifyMatch(oversLeft, requiredRate, wicketsLost)
if oversLeft > 0 && requiredRate <= 12
status = 'Match alive';
elseif wicketsLost >= 10
status = 'All out';
else
status = 'Chase unlikely';
end
switch status
case 'Match alive'
disp('Game on!');
case {'All out','Chase unlikely'}
disp('Innings effectively over');
otherwise
disp('Unknown state');
end
endIn an if condition, a non-scalar array is true only if ALL of its elements are nonzero. This is why if someVector > 0 silently behaves like all(someVector > 0) — a common source of confusion for beginners expecting element-wise branching.
Vectorized Conditionals: any() and all()
For array data, MATLAB idiom favors replacing an if inside a for loop with vectorized functions like any() and all(), or with logical indexing. any(v) returns true if at least one element of v is nonzero/true; all(v) returns true only if every element is. Logical indexing, such as x(x>0), extracts elements satisfying a condition in one expression, avoiding an explicit loop with an if check per element entirely.
Cricket analogy: instead of looping over every ball with an if to check for a wicket, all(deliveries==0) instantly confirms a maiden over across the whole vector
Using == to compare floating-point results (e.g. if x == 0.3) is unreliable due to floating-point rounding. Prefer a tolerance check like if abs(x - 0.3) < 1e-9, especially when x comes from arithmetic rather than a literal.
- if/elseif/else blocks are closed with end, and conditions are true when nonzero (or all-nonzero for arrays).
- && and || are short-circuit operators for scalar control flow; & and | are element-wise for array operations.
- switch-case compares one variable against discrete values and supports cell arrays for matching multiple values to one case.
- MATLAB's switch does not fall through between cases the way C's does.
- any() and all() let you replace loop-plus-if patterns with fast vectorized checks.
- Logical indexing (x(x>0)) extracts matching elements without an explicit loop.
- Avoid == for floating-point comparisons; use a tolerance-based check instead.
Practice what you learned
1. What keyword closes an if/elseif/else block in MATLAB?
2. Which operator should be used for scalar condition combination inside an if statement to get short-circuit behavior?
3. In a switch statement, how do you match multiple values to the same case block?
4. What does all([1 1 0 1]) return?
5. Why is if x == 0.3 risky when x results from arithmetic?
Was this page helpful?
You May Also Like
Loops in MATLAB
Master for and while loops in MATLAB, including break/continue, nested loops, preallocation, and iterating cell arrays and structs.
Functions in MATLAB
Learn how to define MATLAB functions with proper file naming, multiple return values, variable input arguments, local function scope, and recursion.
Scripts vs Functions
Understand the key differences between MATLAB scripts and functions — workspace behavior, when to use each, and how to convert a script into a reusable function.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics