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-else Statement
When there are two mutually exclusive options, add an
else branch. MATLAB checks the
if condition once; if it is true, it executes the
if branch, and if it is false, it runs the
else branch. Exactly one branch runs each time the code executes.
The if-else Statement Structure.
if
conditionA is true, run
blockA. Otherwise, run
blockB.
if conditionA % true? ➜ run blockA -- false? ➜ run blockB
blockA
else
blockB
end
Notes & Rules:
Every if-statement must end with end.
conditionA must be a logical statement that returns true or false.
Exactly one of blockA OR blockB must run when the code executes.
Building on the previous example, here is a code block that uses an
if-else statement:
Since
age is
23, the logical statement
age >= 21 is true, so MATLAB prints the first message. Now try changing
age to
16 and running it again. This time, the logical statement is false, so MATLAB tells you to move along.
Checkpoint 28 .
(a) If-else executes exactly one branch.
In an
if-else statement, MATLAB executes exactly one of the two branches.
True.
Because the condition is either true or false, MATLAB runs the matching branch and skips the other.
False.
Because the condition is either true or false, MATLAB runs the matching branch and skips the other.
(b) When does the else branch run?
When does the
else branch in an
if-else statement execute?
The else branch runs before the if branch.
MATLAB checks the if condition first, then decides which branch to run.
The else branch runs when the condition is false.
The else branch is the alternative path that executes when the condition evaluates to false.
The else branch runs after the if branch completes.
Only one branch runs; they never both execute in the same pass through the code.
The else branch never runs.
The else branch runs whenever the condition is false.
(c) If-else provides two mutually exclusive paths.
An
if-else statement is useful when you need to choose between two mutually exclusive actions.
True.
The if-else structure guarantees that exactly one of two code paths executes based on whether the condition is true or false.
False.
The if-else structure guarantees that exactly one of two code paths executes based on whether the condition is true or false.