Is there ever a good time to use elseif?

19 ビュー (過去 30 日間)
Chad Greene
Chad Greene 2015 年 10 月 8 日
回答済み: Walter Roberson 2015 年 10 月 9 日
I've been helping colleagues debug their codes lately, and I keep finding problems stemming elseif statements, particularly if elseif contains any sort of AND or OR statements. Sometimes the issue results from mixing up && and &. Other times the author has attempted to write an elseif statement for every case that could possibly be true, but inevitably some cases are not accounted for. Sometimes multiple elseif statements within the same if block are true.
Reading elseif statements and trying to make sense of them can be a real exercise in bookkeeping. Sometimes I ask the person who wrote the offending statements to talk me through what their code is supposed to be doing. Usually at this point they start fumbling, not sure exactly what logic was intended.
The cynic in me wonders if elseif's appeal lies in letting coders make progress writing their code without making them step back and think about what they're trying to do. I've been telling my colleagues to never use elseif because if statements without elseif and switch / case can always do the task in a more readable, less error-prone way. My question is, Am I right in telling my colleagues never to use elseif? Is there a time when elseif is really the best option?
  4 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 9 日
Careful, if t_desired became a vector, it could be that none of those conditions is true.
Star Strider
Star Strider 2015 年 10 月 9 日
Quite true! I had in mind a scalar start time but wasn’t specific.

サインインしてコメントする。

回答 (2 件)

Kirby Fears
Kirby Fears 2015 年 10 月 8 日
編集済み: Kirby Fears 2015 年 10 月 8 日
There's a pretty good answer in the context of Java here. The efficiency gains of elseif statements are especially impactful when some cases are much more likely than others because you can code your if / else if / else sequence to have the most likely cases near the top.
Also, elseif statements can be a very convenient and easy-to-read logical tool to avoid repeating a conditional statement. E.g.
if (condition1),
elseif (condition2),
else
end
When doing this purely with if-statements, it gets repetitive and ugly:
if (condition1),
end,
if (~condition1 & condition2),
end
if (~condition1 & ~condition2),
end
Here's a concrete use-case for elseif:
if A<B,
% case 1
elseif A<2*B,
% case 2
else
% case 3
end
Writing it this way allows me to get the A>=B condition in the elseif clause without having to type it. It also allows Matlab to avoid processing every block. When A <B happens to be true, the elseif clause will never be evaluated (saving processing time).
Perhaps your colleagues could focus on simplifying the logical conditions required to handle all their cases. They could also try organizing the different cases where some are subsets of others (like the concrete use-case above) so the logical sequence is more natural.

Walter Roberson
Walter Roberson 2015 年 10 月 9 日
It is possible to code multiple logical conditions in a switch/case, but I for one find it very ugly and a lot more error prone than if/elseif .
switch true %yes, the constant!
case (a==b & c>2)
c = c-2;
case (a>b || c==3)
c = 2;
...
otherwise
end
In order to read this properly, the reader has to know that it is even possible to write expressions instead of constants as the cases, and the reader has to know that in MATLAB, the first match will be taken if there are multiple matches (so multiple expressions could evaluate true but it is the first of them that will have effect.)
IMO, this structure has no benefits other than being nagged for the equivalent of a final "else". Instead it is misleading because programmers "know" that only one "case" is true in a "switch" and so programmers feel free to re-arrange the order of the cases, thereby breaking the expression.
There are a lot of languages where the order of the case labels makes no difference and the language implementation is free to compute which one to take in any way it feels is efficient. (C is not such a language because in C you need to "break" explicitly or else you flow into the next case, but it is permitted to use any internal reordering with regards to sections that have a 'break'.) But in MATLAB, the order matters: the MATLAB switch is syntactic sugar over if/elseif.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by