Hi just a simple question, Which is a better use, 'if' or 'switch'? Thanx P

댓글 수: 1

Jan
Jan 2011년 9월 20일
Please add the relevant part of the code to your question. without seeing any details a decision is not possible.

댓글을 달려면 로그인하십시오.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 20일

4 개 추천

I guess you are asking to compare switch-case with nested if-elseif. I prefer switch-case. I think the efficiency is the same but switch-case provides better readability and the flexibility to provide multiple value for each choice.
For example
switch Str
case {'a','b'}
disp('It is a or b');
case 'c'
disp('it is c');
otherwise
disp('not valid');
end
Compare to
if strcmp(Str,'a') || strcmp(Str,'b')
disp('It is a or b');
elseif strcmp(Str,'c')
disp('it is c');
else
disp('not valid');
end

댓글 수: 8

PChoppala
PChoppala 2011년 9월 20일
Hi
Is there any computational complexity advantage in using 'switch'?
Fangjun Jiang
Fangjun Jiang 2011년 9월 20일
I don't think so. In terms of how many conditions to check before reaching a particular condition, I think the two approaches are the same.
Let's see if anyone else want to chime in.
PChoppala
PChoppala 2011년 9월 20일
Hmm... Okay then.
I wrote a function that gives the output y=x^2 / 25*x + sqrt(Q).
I want to use it twice in the main program as
y1=x^2 / 25*x + sqrt(Q)
and
y2=x^2 / 25*x
Although I can use switch and if, can I use nargin?
Fangjun Jiang
Fangjun Jiang 2011년 9월 20일
What do you mean? It sounds like a totally different question.
PChoppala
PChoppala 2011년 9월 20일
Is it?
I used switch in the function to call
either
y1=x^2 / 25*x + sqrt(Q)
or
y2=x^2 / 25*x
I am just wondering if I can do it using nargin or nargout instead of switch?
Oleg Komarov
Oleg Komarov 2011년 9월 20일
nargin tells you how many inputs were supplied, nargout how many outputs were asked for, so how does it relate to switch an if?
Jan
Jan 2011년 9월 20일
@praveen: It is not clear how you use IF, SWITCH or NARGIN to perform the calculations. Therefore it is impossible give a meaningful advice.
Please insert the relevant part of the code in your original question by editing - and do *not* insert it as comment to an answer.
Fangjun Jiang
Fangjun Jiang 2011년 9월 20일
If you have a function like y=f(x,varargin) where Q be the optional input argument, then yes, you need to use nargin. Whether use if-elseif or switch-case probably doesn't make a difference. My suggestion, if you are literally design function y1=x^2 / 25*x + sqrt(Q), I would suggest making y1=x^2 / 25*x a function and add sqrt(Q) as needed. But of course, if you want to practice using nargin,if-elseif or switch-case, that is a different story.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 9월 20일

2 개 추천

Switch evaluates the value of the switch portion, and then it evaluates each case expression in the list until finally one of the cases matches the switch value. This is pretty much the same complexity as the corresponding if/elseif cascade.
Most people write code as if the switch cases must be constants, but that is not the case in MATLAB: they can be full expressions with function calls and all.
This needs to be compared to other computer languages such as C, in which the case labels do need to be constants. In those other languages, it is possible to do preprocessing such that the time required to decide which branch to use is decided in constant time (or near constant time.) In those languages, switch is more efficient than if/elseif chains.
In MATLAB, if/elseif chains are often able to optimize groups of cases by using extended logical conditions with && and || operators; it is difficult to do that kind of optimization using switch.

댓글 수: 1

PChoppala
PChoppala 2011년 9월 20일
Yeah, that's right. Thanks. Can you look into my comment to Fangjun?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by