You are on page 1of 18

Flow Control

Flow control is the execution of commands, functions and statements in


a certain
order that results in a sequence of choices.
American National Standards Institute (ANSI) Flow
Chart

A flowchart is a graphical representation of the processes and data flow


of an operation. Several symbols are used to describe an operation.
Order of operation
The order of processes is represented by an arrow line
Terminal
The termination or beginning of a process is represented by an
edgeless rectangle; usually has ‘start’ or ‘end’ inside.
Process
A process is represented by a rectangle
Decision
A decision is a conditional operation where a choice has to be made
with a binary outcome. A decision is represented by a rhombus.
Data
• Data is represented as a parallelogram regardless if it is input or
output.
Flow Control
• If statements

• for loops
Relational Operators
Display
When writing programmed and scripts, it is important to see a visual
confirmation of input or output data. In order to see just that we use
the disp() function.
The disp() function can only read one type of data at a time (e.g
numbers, strings,etc.) so it is best to convert the data type to a single
type when using the function.
To facilitate, the num2str() function converts number data to string
data.
Example
• T = [ 15 50];
• disp([‘The time is: ’ num2str(T)])
if

The if statement evaluates a logical expression and executes a group of


statements when the expression is true. The optional elseif and else
keywords provide for the execution of alternate groups of statements.
An end keyword, which matches the if, terminates the last group of
statements. The groups of statements are delineated by the four
keywords – no braces or brackets are involved.
size_comparison
A = ones(2,3);
B = rand(3,4);
if eq(size(A),size(B))
C = [A; B];
disp(C)
else
disp('A and B are not the same size.')
C = [];
end
For

The for loop repeats a group of statements a fixed, predetermined


number of times. A matching end delineates the statements.
Example
for v = [1 5 8 17]
end
disp(v)
Example
for v = [1 5 8 17]’
end
disp(v)
Example
for i= 1:4
Q = i.^2
end

You might also like