This preview shows pages 1–5. Sign up to view the full content.
9/28/2010
1
Lecture 4:
If statements, While loops
BMEn 2401 Programming for Biomedical Engineers
Rich Beck
September 28, 2010
1
Today’s Lecture
•
Relational and logical operators
•
ifend
statements
•
whileend
loops
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 9/28/2010
2
Review
So far we have learned
•
Scalar variables and operators:
Radius = 2.8;
Sphere_Volume = 4/3*pi*Radius^3
•
Arrays and array operators:
V3 = V1.^2 ./ ( 3*V2 + V2.*V1)
X
= A^1 * b
•
Some MATLAB builtin functions
sqrt(x), sum (v), …
Use Help > Product Help to Search… ex.
rem
3
Test Yourself (Review)
Write a script that queries the user to
input
the height, length,
and width of a cube from the command window and calculate
the volume of the cube
4
9/28/2010
3
Logical Statements and
Relational Operators
Statement = a > b
It is 1 if a is greater than b (true)
It is 0 if a is not greater than b (false)
Example:
Statement1 =
5 > 3 ;
Statement2 = sqrt ( 16 ) >
8 ;
Statement1 =
1
Statement2 =
0
5
Relational Operators
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal
to
==
Equal to
~=
Not equal to
Relational operators
are always performed
elementbyelement in arrays.
Note that the “equal to” operator consists of two “=“ signs.
6
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
4
Test Yourself
What are the values of
statement1
,
statement2
,
and
statement3
?
statement1 = ( 1 / 4 ) < ( 1 / 5 )
x2 = 9;
statement2 = (x2)^2 ~= x2^2
A=[ 0 2; 3 4];
B=[ 0 1; 6 9];
statement3 = A <= B
7
Logical Operators
Logical operators are used to combine logical statements (s1, s2).
Operator
Name
Description
AND
( s1 & s2 ) is true (i.e. 1) only
when both s1 and s2 are true.

OR
( s1  s2 ) is true (i.e. 1) when
either one of s1 or s2 or both of
them are true.
~
NOT
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/10/2011 for the course BMEN 2051 taught by Professor S during the Fall '10 term at Minnesota.
 Fall '10
 S

Click to edit the document details