AI, machine learning and deep learning · Introductory

Control Statements

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Control statements enable flexible program execution in MATLAB by directing flow based on logical conditions. The core structure relies on an if condition evaluating to a single logical value (0 or 1), potentially extended with elseif and else branches. Execution stops at the first true condition, making the order of checks critical.

Intuition

Think of control statements as a series of checkpoints in a decision-making process. MATLAB reads your code from top to bottom, like a person reading a list of instructions. It checks the first condition; if it is true, it performs the associated action and stops looking further. If that condition is false, it moves to the next elseif check. This sequential evaluation means that the order in which you write your conditions matters significantly, as only the first true condition triggers its block. This prevents multiple blocks from running simultaneously for the same variable state, ensuring predictable and controlled program flow.

Derivation

  1. Control statements are essential constructs for flexible code execution in MATLAB.
  2. The fundamental structure is the if statement, which requires a condition evaluating to a single logical value (0 or 1).
  3. The if block is closed by an end statement, with optional elseif and else branches for multiple scenarios.
  4. MATLAB evaluates conditions sequentially; once a condition is true, its code runs and subsequent conditions are ignored.
  5. Complex conditions use logical operators && (AND) and || (OR) to combine multiple criteria.
  6. Indentation is recommended for visual clarity but is not required for execution.

Worked example

Problem

Given a variable r = 5, determine which block executes in the following code:
if r > 0
disp('Positive')
elseif r > 2
disp('Greater than 2')
else
disp('Other')
end

  1. MATLAB evaluates the first condition: r > 0. Since 5 > 0 is true, this condition is met.
  2. The code inside the first block (disp('Positive')) is executed.
  3. Because a true condition was found, MATLAB ignores the subsequent elseif r > 2 check, even though 5 > 2 is also true.
  4. The else block is also skipped.

Answer The output is 'Positive'. The order of conditions matters; the first true condition triggers its block and stops further evaluation.

Common mistakes

  • Thinking that multiple conditions in an if-elseif block are all evaluated; only the first true condition triggers its block.
  • Confusing the logical OR operator || with the letter 'l' or number '1'.
  • Using double quotation marks for apostrophes inside strings instead of two single quotation marks ('').
  • Assuming the order of elseif conditions does not matter; swapping conditions can change which block executes.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.