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

58 ビュー (過去 30 日間)
Akhil  Reddy
Akhil Reddy 2016 年 6 月 7 日
コメント済み: Swapnil Devikar 2020 年 3 月 27 日
switch matlab_review
case 'good' || case 'interesting'
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end
  2 件のコメント
Stephen23
Stephen23 2016 年 6 月 7 日
@Akhil Reddy: did you read the switch documentation? It explains exactly how to do this... and it has examples too!
Akhil  Reddy
Akhil Reddy 2016 年 6 月 22 日
Thank you :)

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

採用された回答

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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by