Introduction to Matlab 02:- Control Structures in MATLAB
-
Upload
atush-jain -
Category
Education
-
view
30 -
download
3
Transcript of Introduction to Matlab 02:- Control Structures in MATLAB
CONTROL STRUCTURES CONTROL STRUCTURES IN MATLABIN MATLAB
Atush JainAtush Jain
Relation Relation
<< Less thanLess than
<=<= Less than or equal toLess than or equal to
>> Greater thanGreater than
>=>= Greater than or equal toGreater than or equal to
==== Equal toEqual to
~=~= Not equal toNot equal to
RELATIONS
WHEN APPLIED TO SCALAR, RELATION IS ZERO OR ONE DEPENDING ON WHETHER THE RELATION IS TRUE OR FALSEa=3; b=2;c= ( a>b) ; It means c=1c=(a<b); It means c=0a= = b answer is 0a~=b answer is 1
AND WHEN APPLIED TO MATRICES OF SAME SIZE, RELATION IS MATRIX OF 0’s and 1’s GIVING VALUE OF RELATION BETWEEN CORROSPONDING ENTERIES.d=[1 2]; e=[1 1];f= (d= =e); It means f=[1 0];
Logical oprator Logical oprator
A & BA & B and(A, B)and(A, B)
A | BA | B or(A, B)or(A, B)
~A~A not(A)not(A)
Control statementControl statement
If ,if elseIf ,if elseFor loop For loop While loopWhile loopSwitch caseSwitch caseBrake ,continue etc.Brake ,continue etc.
IF
price=4500;
if price >5000,
disp('PRICE IS MORE THAN 5000');
end
IF ELSE
price=4500;
if price >5000,
disp('PRICE IS MORE THAN 5000');
else
disp(‘PRICE IS NOT MORE THAN 5000’);
end
IF ELSEIF
price=4500;
if price >5000,
disp('PRICE IS MORE THAN 5000');
elseif (1000<=price)&(price <=5000),
disp('PRICE IS BETWEEN 1000 AND 5000');
else
disp('PRICE IS LESS THAN 1000');
end
WHILE LOOP
var=20;
while var>0,
disp(var);
pause(1)
var=var-1;
end
disp('variable is zero now');
disp(var);
FOR LOOP
for i=1:10,
disp(i);
end
for i=1:2:11,
disp(i)
end
Nested For Loop
n=3;
for i=1:n,
for j=1:n,
a(i,j)=5;
end
end
disp(a);
SWITCH CASE
var1=10;
var2=5;
switch operation
case 'add'
output=var1+var2;
disp(output);
case {'multiply','product'}
output=var1*var2;
disp(output);
SWITCH CASE CONTINUED
case {'subtract','sub'}
output=var1-var2;
disp(output);
case 'divide'
output=var1/var2;
disp(output);
otherwise
disp('What else you want?');
end
SWITCH CASE CONTINUED
case {'subtract','sub'}
output=var1-var2;
disp(output);
case 'divide'
output=var1/var2;
disp(output);
otherwise
disp('What else you want?');
end
SWITCH CASE CONTINUED
var=5;
switch var
case 1
disp('variable is one');
case 2
disp('variable is two');
case 3
disp('variable is three');
otherwise
disp('variable is not between one and three');
end
BREAK STATEMENT
var=20;
while var>0,
disp(var);
pause(1)
if var==10
break;
end
var=var-1;
end
str=sprintf('Now variable is %d',var);
disp(str);
INPUT
name=input('Please enter your name : ','s');
fprintf('\nHello %s !\n',name);
account=input(' Please enter your account number : ');
if (25 <account)&(account<50)
disp('Welcome');
else
disp('You are not a valid user');
end
FORMATED OUTPUT
name='Joshph';
age=32;
salary=18000;
fprintf('\nEmployee Name : %s\t Age : %d ….
\t Salary : Rs %0.2f',name,age,salary);
fprintf('\n I am writing 123456 in exponential ….
form : %e',12356);
a=[1.2 1.3 1.4 1.5 1.6];
fprintf('\n %f ',a);
THANK YOU THANK YOU