Can I use case 1 || case 2 while using switch thread?

132 次查看(过去 30 天)
switch matlab_review
case 'good' || case 'interesting'
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end

采纳的回答

Guillaume
Guillaume 2016-6-7
As per the documentation of switch, case, otherwise, to include several possible match for a case, group them in a cell array.
switch matlab_review
case {'good', 'interesting'}
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end
It's always a good idea to read the documentation rather than trying to make up your own syntax.
  1 个评论
Swapnil Devikar
Swapnil Devikar 2020-3-27
Just an observation here. The documentation talks about combining test cases as you have correctly pointed out. But it doesn't talk about combining a case and a "default" case. For instance, in your example, it is not cleat if it is acceptable to combine {'good', 'otherwise'}.
It matters because "otherwise" is a special case.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by