Skip to content

MATLAB - Nest If Statememt

In MATLAB, you can nest conditional statements, which means placing one if or if-elseif-else structure inside another. This allows you to check for a secondary condition after a primary condition has already been met, enabling more complex decision-making logic.

The basic syntax for a nested if statement is as follows. The inner if statement is only evaluated if the outer if condition is true.

if primary_condition
% This block executes if primary_condition is true
if secondary_condition
% This block executes only if BOTH
% primary_condition AND secondary_condition are true
end
% Other statements for the primary condition can go here
end

You can nest any combination of if, elseif, and else to create detailed logic flows.

Let’s consider a practical example where we want to determine if a student is eligible for an honors distinction. They must have a score above 90, and they must also be in ‘Good Standing’.

% Define student variables
score = 95;
status = 'Good Standing';
% --- Main logic ---
% Check the primary condition: the score
if score > 90
fprintf('Score of %d is in the honors range.\n', score);
% Now, check the secondary condition: the status
% This is the nested 'if' statement
if strcmp(status, 'Good Standing')
fprintf('Status is ''Good Standing''.\n');
disp('Result: Student is eligible for honors distinction.');
else
fprintf('Status is ''%s''.\n', status);
disp('Result: Student is NOT eligible due to status.');
end
else
fprintf('Score of %d is not high enough for honors consideration.\n', score);
end

When you run this script, it first checks the score. Since 95 > 90, it enters the first block and then proceeds to check the status. The output is:

Score of 95 is in the honors range.
Status is 'Good Standing'.
Result: Student is eligible for honors distinction.

If you change status = 'On Probation';, the inner else would trigger, leading to a different outcome.

While nesting is powerful, excessive nesting can make code difficult to read and debug. This is often called the ‘arrowhead anti-pattern’ because of the shape of the indented code.

For simple cases like our example, you can often avoid nesting by combining conditions with logical operators. The short-circuiting AND && is perfect for this.

% Alternative using logical AND (&&)
score = 95;
status = 'Good Standing';
if score > 90 && strcmp(status, 'Good Standing')
disp('Result: Student is eligible for honors distinction.');
else
disp('Result: Student is NOT eligible.');
end

This version is more concise and often easier to understand.

If you have a chain of mutually exclusive conditions, an if-elseif-else chain is much cleaner than deep nesting.

If the logic inside a nested block is complex, consider moving it into its own function. This improves readability and makes the code more modular and testable.

% Example of refactoring
if score > 90
checkStudentStatus(status); % Call a separate function
else
% ...
end
function checkStudentStatus(studentStatus)
% Logic for checking status goes here...
end

Key Takeaway: Use nested if statements when you need to perform a sequence of checks where each step depends on the last. For simpler combined conditions, prefer logical operators. For complex or deeply nested logic, refactor your code to keep it flat and readable.

matlab_decisions.htm