Beruflich Dokumente
Kultur Dokumente
LESSON OUTCOMES
structures
in
Outline
1.0 Programming with Matlab
1.1 Relational Operators
1.2 Conditional Statements
1.2.1 if statement
Meaning
<
Less than
<=
>
Greater than
>=
==
Equal to
~=
Not equal to
Exercise
>> x=[6,3,9]
>> y=[14,2,9]
>> z=(x<y)
>> z=(x~=y)
>> z=(x>8)
ans??
ans??
ans??
Exercise
Arithmetic operators +,-,*,/ and \ have precedence
over the relational operators
>> z=5>2+7
z=??
>> z=5>(2+7) z=??
Parentheses can be used to change the order of
precedence
>> z=(5>2)+7 z=??
1.2.1 if statement
Basic form:
if logical expression
statements
end
Exercise
y x
for x 0
>> x=6
y=??
>> x=-4
y=??
Exercise
y x
for x 0
y e x 1 for x 0
>> x=4
y=??
>> x=-9
y=??
Exercise
y ln x
for x 10
y x
for 0 x 10
y e x 1 for x 0
>> x=20
y=??
>> x=5
y=??
>> x=-4
y=??
Example
Start
for k= m:s:n
statements
end
Set k = m
k>
n?
Increment k
by s
False
Statement
s
End
Statement
s following
the End
statement
True
Exercise
Start
Logical
Expressio
n
True
Statements
(which increment
the loop variable)
End
Statements
following the
End statement
False
Example
5
9
17
1
2
3
4