Programming Fundamentals - The MathWorks - #801

/ 840


catalogue search
P. 801
P. 802
P. 803
P. 804
P. 805
P. 806
P. 807
P. 808
P. 809
P. 810
P. 811
P. 812
P. 813
P. 814
P. 815
P. 816
P. 817
P. 818
P. 819
P. 820
P. 821
P. 822
P. 823
P. 824
P. 825
P. 826
P. 827
P. 828
P. 829
P. 830
P. 831
P. 832
P. 833
P. 834
P. 835
P. 836
P. 837
P. 838
P. 839
P. 840


See other catalogues for The MathWorks

Text version of the page
Program Control
Using switch Versus if
It is possible, but usually not advantageous, to implement switch-case statements using if-elseif instead. See pros and cons in the table.
switch-case Statements
if-elseif Statements
Easier to read.
Can be difficult to read.
Can compare strings of different lengths.
You need strcmp to compare strings of different lengths.
Test for equality only.
Test for equality or inequality.
MATLAB® case Evaluates Strings
A useful difference between switch-case statements in MATLAB® and C is that you can specify string values in MATLAB case statements, which you cannot do in C.
switch(method) case 'linear'
disp('Method is linear') case 'cubic'
disp('Method is cubic')
end
Multiple Conditions in a case Statement
You can test against more than one condition with switch.Thefirstcase below tests for either a linear or bilinear method by using a cell array inthecasestatement.
switch(method)
case {'linear', 'bilinear'}
disp('Method is linear or bilinear') case (<and so on>)
end
Implicit Break in switch-case
In C, if you do not end each case with a break statement, code execution falls through to the following case. In MATLAB, case statements do not fall
12-41

pageCatalog pdf di En 2012-06-22-01