Академический Документы
Профессиональный Документы
Культура Документы
Content
Flow diagram
while loop and for loop.
Addition, subtraction and other mathematical
functions.
Break and continue statement.
Branches if else, switch/case.
Debugging tools
Execution Control
Logical functions
A sequence is a list of commands that are
executed one after another.
A selection structure allows the programmer
to execute one command if some criterion is
true and second if the criterion is false.
A repetition structure, or loop, causes a group
of statements to be executed multiple times.
Structures
Sequence
Selection
Repetition
Sequence
Selection
Repetition (Loop)
Block of code to
excute if the
comparison is true
Comparison
False
Block of code to
excute if the
comparison is false
M-file Program
Write
Start
if age<16
True
Sorry
Youll have
to wait
elseif
age<18
True
You may
have a youth
license
elseif
age<70
else
Drivers over 70
require a
special license
End
True
As a general rule
If structures work well for scalars
For vectors or arrays use a find function or..
Combine if structures with a repetition
structure
Repetition structures are introduced in the next
chapter
switch/case
case option_n
code to be executed if variable is exactly
equal to option n
otherwise
code to be executed if variable is not
equal to any of the options
end
Menu
The menu function is often used in
conjunction with a switch/case structure.
This function causes a menu box to appear on
the screen with a series of buttons defined by
the programmer.
input = menu(Message to the user,text for button 1,text for button 2, etc)
If I select Honolulu
Summary
structures
repetition structures
Summary Sequence
and
||or
~
xor
not
exclusive or
Summary if family
Summary switch/case
Similar to the if/elseif/else structure
Commonly used with menu
X=5
Y=1
X<Y another example
X=[1, 2, 3, 4,5]
Y=[-2,0,2,4,6]
Use same sign
Selection structures
If comparison
statements
End
if G<50
disp(G is a small value equal to :)
disp(G);
end
if /elseif/ else
The elseif function allows you to check
multiple criteria while keeping the code easy
to read.
eg
For loop
% To understand For Loop
% For loop will do iterations only the fixed number of times
clc
clear
SumNumbers =0 ;
for I = 1:10
SumNumbers = SumNumbers+ I
end
Exercise
Convert the values from degree to radian from
10 degree to 360 degree with an increment of
10.
Develop the table for the results.
Formula for
Radian = (Value in Degree X Pi)/180
while loop
% To understand while loop
% while loop is an iteration loop
% It will keep on going round and round until the condition is satisfied.
% This loop is used, when we dont know the exact number of iterations.
clc
clear
Number =50
while Number > 20
Number = Number -5
end
what
dir
ls
type test
delete test
cd a:
chdir a:
pwd
which test
~
&
|
% highest precedence
% equal precedence with or
% equal precedence with and
expression1
% is true
% execute these commands
elseif expression2 % is true
% execute these commands
else
% the default
% execute these commands
end
for x = array