Professional Documents
Culture Documents
Lab 3
Lab 3
PROGRAMMING IN MATLAB
Logical Operation
Setting logical value:
true, false
Logical Operations:
& (and), | (or), ~ (not), xor, any, all
Relational Operations:
== (eq), ~=(ne), < (lt), > (gt),
<= (le), >= (ge)
Selection Constructs
1. If-end construct
if condition 1
program 1
end;
Selection Constructs
2. If-else-end construct
if < condition 1 >
< program 1 >
else
< program 2 >
end;
Selection Constructs
3. If-elseif-end construct
if condition 1
program 1
elseif condition 2,
program 2
elseif condition 3,
program 3
.
elseif condition N,
program N
end;
Switch Statement
Switch expression
case 1
do these statements
case 2
do these statement
case n
do these statement
end
If loops
Example 1;
A=10;
if A ~= 0
disp(A is not equal to 0)
end
Example of If loop
Example 2;
A=10;
if A > 0
disp(A is positive)
else
disp(A is not positive)
end
Example of If loop
Example 3;
P1 = 3.14;
P2 = 3.14159;
if P1 == P2
disp(P1 and P2 are equal)
else
disp(P1 and P2 are not equal)
end
For loops
Require the following syntax;
for d = array
% command 1
% command 2
% and so on
end
While loops
The common syntax;
while condition
statements
end