Documente Academic
Documente Profesional
Documente Cultură
ifend
if-elseend
if-elseif-elseend
(c) 2003 The Ohio State University
INTRODUCTION TO PROGRAMMING
163164
*
*
*
INTRODUCTION TO PROGRAMMING
163164
RELATIONAL OPERATORS
Relational operator
Meaning
<
Less than.
<=
>
Greater than.
>=
==
Equal to.
~=
164167
165
RELATIONAL OPERATORS, EXAMPLES
>> 5>8
Since 5 is not larger than 8
the answer is 0.
ans =
0
>> a=5<10
a=
=1
=0
=1
167
LOGICAL OPERATORS
! Logical operators have numbers as operands.
! A nonzero number is true.
! A zero number is false.
Logical Operator
Name
Meaning
&
Example: A & B
AND
|
Example: A | B
OR
~
Example: ~ A
NOT
168169
3 AND 7.
3 and 7 are both true (nonzero), so the outcome is 1.
1
>> a=5|0
a=
1
>> x=-2; y=5;
>> -5<x<-1
ans =
0
>> -5<x & x<-1
ans =
1
CONDITIONAL STATEMENTS
172177
172
THE FORM OF A CONDITIONAL STATEMENT
If Conditional expression
consisting of relational
and/or logical operators
Examples:
if a < b
if c >= 5
if a == b
if a ~= 0
if (d<h)&(x>7)
if (x~=13)|(y<0)
(c) 2003 The Ohio State University
if conditional statement 1
commands
end
172177
command group 1
elseif conditional statement 2
command group 2
if conditional statement
command group 1
else
command group 3
else
command group 2
end
end
False
......
......
......
if
172174
MATLAB program.
if conditional expression
True
........
........
........
A group of MATLAB
commands.
end
......
......
......
MATLAB program.
end
ifend
STATEMENT
174175
MATLAB program.
if conditional expression
if
True
end
........
........
........
Group 1 of MATLAB
commands.
........
........
........
Group 2 of MATLAB
commands.
else
end
......
......
MATLAB program.
False
False
elseif
MATLAB program.
if conditional expression
if
True
True
........
Group 1 of MATLAB
........
commands.
........
elseif conditional expression
........
........ Group 2 of MATLAB
........ commands.
else
........
........
........
end
Group 2 of MATLAB
commands.
end
......
......
(c) 2003 The Ohio State University
MATLAB program.
172177
MATLAB ASSIGNMENT 8:
199202