7.2: Flowcharts
- Page ID
- 84477
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
By Carey A. Smith
A flowchart is a graphical representation of code logic. It is especially helpful to understand the logic of code with for loops and if-else logic.
An example is given here. More information can be found at:
https://en.wikipedia.org/wiki/Flowchart [en.Wikipedia.org]

Try to code this up
Solution
The corresponding code is:
for n = 1:12
if(mod(n,2) == 0)
disp([num2str(n),' is even'])
else
disp([num2str(n),' is odd'])
end
end
.
flowchart for primes
This flowchart and code show an algorithm to determine which numbers between 1 and 48 are prime numbers.
We already know that 2, 3, and 5 are prime, so the for loop starts at k = 6.
For each value it tests, it displays a message stating whether or not it is a prime number. It stores the prime numbers it finds in a vector called prime_list.

Try coding this logic.
Note that the for-loop automatically does the "k = k+1" shown at the bottom of the flowchart; do not code that line.
(There are more sophisticated and efficient prime-number algorithms; this one is an illustration of moderately complex logic.)
Solution
%% "for loop" example with “if-then-elseif” logic:
% Find the prime numbers between 1 to 48:
prime_list = [2,3,5] % Initial set of primes
for k = 6:48
if( mod(k,2) == 0) % Tests if k is divisible by 2
disp([num2str(k),' is not prime. It is divisible by 2'])
elseif (mod(k,3) == 0) % Tests if k is divisible by 3
disp([num2str(k),' is not prime. It is divisible by 3'])
elseif( mod(k,5) == 0) % Tests if k is divisible by 5
disp([num2str(k),' is not prime. It is divisible by 5'])
else
disp([num2str(k),' is prime.'])
% Add k to the list of primes:
prime_list = [prime_list, k];
end % end if
end % for k = 6:48
% Display the primes found
prime_list
This code is the attached file for_loop_primes.m
.
Homework:
1. Watch the following video from Robert Talbert
Then code the second example in the video about the temperature and rain. The corresponding flowchart is this:

Figure \(\PageIndex{i}\): Flowchart for Temperature, Rain, Wearing a Coat.
Put parentheses around the conditional expressions, even though the video did not do this. Examples:
if (temp < 50)
elseif (raining == 1)
.


