Structured Programming
The simplest of all M-files perform instructions
sequentially.
All computer languages include statements allowing
programs to take non sequential paths.
These can be classified as
Decisions(or Selection). The branching of flow based on a
decision.
Loops(or Repetition). The looping of flow to allow
statements to be repeated.
2/22/2014
Decisions: if structure
This structure allows you to execute a set of statements if a logical
2/22/2014
Logical Conditions
Operator
Relationship
x == 0
==
Equal
unit ~=m
~=
Not Equal
a<0
<
Less than
s>t
>
Greater than
<=
r >= 0
>=
2/22/2014
Example,
if grade>=60
disp (passing grade)
else
disp (fail)
end
2/22/2014
rather than testing individual conditions, the branching is based on single test
expression.
Depending on its value, different blocks of code are implemented.
An optional block is implemented if expression takes none of prescribed
values.
It has the general syntax
switch testexpression
case value1
statements1
case value2
statements2
.
.
otherwise
statementsotherwise
end
2/22/2014
Loops
As the name implies, loops perform operations repetitively.
There are two types of loops, depending on how the
2/22/2014
The index is a variable that is set at an initial value, start. The program
then compares the index with a desired final value, finish.
If the index is less than or equal to the finish, the statements are
executed.
When it reaches the end line that marks the end of the loop, the index
variable is increased by the step and the program loops back up to the
for statement.
The process continues until the index becomes greater than the finish
value.
A while Structure
A while loop repeats as long as a logical condition is true. Its general
syntax is
while condition
statements
end
The statements between the while and the end are repeated as long
2/22/2014
Thanks