Introduction to Matlab 02:- Control Structures in MATLAB

19
CONTROL STRUCTURES CONTROL STRUCTURES IN MATLAB IN MATLAB Atush Jain Atush Jain

Transcript of Introduction to Matlab 02:- Control Structures in MATLAB

Page 1: Introduction to Matlab 02:- Control Structures in MATLAB

CONTROL STRUCTURES CONTROL STRUCTURES IN MATLABIN MATLAB

Atush JainAtush Jain

Page 2: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 3: Introduction to Matlab 02:- Control Structures in MATLAB

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];

Page 4: Introduction to Matlab 02:- Control Structures in MATLAB

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)

Page 5: Introduction to Matlab 02:- Control Structures in MATLAB

Control statementControl statement

If ,if elseIf ,if elseFor loop For loop While loopWhile loopSwitch caseSwitch caseBrake ,continue etc.Brake ,continue etc.

Page 6: Introduction to Matlab 02:- Control Structures in MATLAB

IF

price=4500;

if price >5000,

disp('PRICE IS MORE THAN 5000');

end

Page 7: Introduction to Matlab 02:- Control Structures in MATLAB

IF ELSE

price=4500;

if price >5000,

disp('PRICE IS MORE THAN 5000');

else

disp(‘PRICE IS NOT MORE THAN 5000’);

end

Page 8: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 9: Introduction to Matlab 02:- Control Structures in MATLAB

WHILE LOOP

var=20;

while var>0,

disp(var);

pause(1)

var=var-1;

end

disp('variable is zero now');

disp(var);

Page 10: Introduction to Matlab 02:- Control Structures in MATLAB

FOR LOOP

for i=1:10,

disp(i);

end

for i=1:2:11,

disp(i)

end

Page 11: Introduction to Matlab 02:- Control Structures in MATLAB

Nested For Loop

n=3;

for i=1:n,

for j=1:n,

a(i,j)=5;

end

end

disp(a);

Page 12: Introduction to Matlab 02:- Control Structures in MATLAB

SWITCH CASE

var1=10;

var2=5;

switch operation

case 'add'

output=var1+var2;

disp(output);

case {'multiply','product'}

output=var1*var2;

disp(output);

Page 13: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 14: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 15: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 16: Introduction to Matlab 02:- Control Structures in MATLAB

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);

Page 17: Introduction to Matlab 02:- Control Structures in MATLAB

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

Page 18: Introduction to Matlab 02:- Control Structures in MATLAB

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);

Page 19: Introduction to Matlab 02:- Control Structures in MATLAB

THANK YOU THANK YOU