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 Additional Flow Control: continue, break, return
MATLAB provides commands that modify normal loop flow. These are most often used inside an
if-statement within a loop.
Table 36. Flow control commands for loops
continue
Skip the rest of the current iteration and move to the next iteration.
break
Exit the loop immediately and continue after end.
return
Exit the current script or function immediately.
Important notes:
These commands only execute if MATLAB reaches the line where they appear.
continue and break may only be used inside a for- or while-loop; using them elsewhere causes an error.
return can be used anywhere inside a script or function to exit early.
π Example 37 . Searching with break.
A loop can search for a target value and stop early when found. The
break command exits the loop immediately.
target = 10;
found = false;
location = -1;
for k = 1:50
value = randi([-20 20]); % generate one random integer
if value == target
found = true;
location = k;
break
end
end
if found
fprintf('The number %i was found on iteration %i\n', target, location)
else
fprintf('The number %i was not found in 50 trials\n', target)
end
This patternβtest a condition, then exit early with
breakβis useful whenever you want to stop searching once you find what you need.
π Example 38 . Example: Testing for Primality.
An integer greater than 1 is prime if it has no divisors other than 1 and itself. This loop tests potential divisors and stops early if it finds one:
number = 75913;
if number < 2
is_prime = false;
else
divider = 2;
is_prime = true;
while divider <= sqrt(number)
if mod(number, divider) == 0
is_prime = false;
break
end
divider = divider + 1;
end
end
if is_prime
fprintf('The number %0.10g is prime!\n', number)
else
fprintf('The number %0.10g is not prime!\n', number)
end
We only need to test divisors up to
\(\sqrt{\text{number}}\text{,}\) which makes the loop much faster for large inputs. The
break command exits as soon as a divisor is found.