Skip to main content
Contents
Calc
Dark Mode Prev Next Profile
\(\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Subsection If-elseif-else Statement
Use
elseif when you need to test several conditions in order. MATLAB checks each condition from top to bottom and executes the first block whose condition is true. An optional
else branch acts as a fallback so you can guarantee that a variable gets assigned a value.
The if-elseif-else Statement Structure.
if conditionA is true, run blockA,
if conditionB is true, run blockB,
if conditionC is true, run blockC,
if all of the above are false, run blockD (default).
if conditionA % true? β run blockA -- false? β check conditionB
blockA
elseif conditionB % true? β run blockB -- false? β check conditionC
blockB
elseif conditionC % true? β run blockC -- false? β run blockD
blockC
else
blockD
end
Notes & Rules:
You can add as many elseif branches as needed.
The else (default) branch is optional.
The conditions are checked in order from top to bottom.
Exactly one of the blocks will run when the code executes.
Every if-statement must end with end.
Each logicalStatement must evaluate to true or false.
The else block runs only when all preceding conditions are false.
You can nest if-statements within other if-statements, but this should be avoided whenever possible.
For practice, test this block with different initial values such as
x = 0; y = 4;,
x = 0; y = 3;, and
x = 3; y = 3;. Watch how only one branch executes each time.
Checkpoint 29 .
(a) How elseif chains work.
Which statements about an
if-elseif-else chain are true? Select all that apply.
Conditions are checked from top to bottom.
MATLAB evaluates conditions in order and stops at the first true one.
The else block runs even if a previous condition is true.
The else block is a fallback and only runs when all conditions are false.
Only the first true conditionβs block executes.
Once MATLAB finds a true condition, it skips the remaining branches.
You must include an else branch in every chain.
The else branch is optional, though it can be helpful as a default.
You can include multiple elseif branches.
Add as many elseif tests as you need to cover different cases.
(b) Order matters in elseif chains.
In an
if-elseif-else chain, changing the order of conditions can change which block executes.
True.
Because MATLAB checks conditions from top to bottom and stops at the first true one, the order of conditions matters. If multiple conditions could be true, only the first true conditionβs block will execute.
False.
Because MATLAB checks conditions from top to bottom and stops at the first true one, the order of conditions matters. If multiple conditions could be true, only the first true conditionβs block will execute.
(c) Purpose of the else branch.
What is the purpose of the
else branch in an
if-elseif-else chain?
To check one more condition before ending.
The else branch does not check a condition; use elseif to check additional conditions.
To provide a default action when no conditions are true.
The else branch serves as a fallback that runs when all previous conditions evaluate to false.
To close the if statement.
The end keyword closes the if statement, not else.
To run code after all other branches have executed.
Only one branch executes; the else runs instead of the others, not after them.