Effacer les filtres
Effacer les filtres

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

125 vues (au cours des 30 derniers jours)
Akhil  Reddy
Akhil Reddy le 7 Juin 2016
Commenté : Swapnil Devikar le 27 Mar 2020
switch matlab_review
case 'good' || case 'interesting'
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end
  2 commentaires
Stephen23
Stephen23 le 7 Juin 2016
@Akhil Reddy: did you read the switch documentation? It explains exactly how to do this... and it has examples too!
Akhil  Reddy
Akhil Reddy le 22 Juin 2016
Thank you :)

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 7 Juin 2016
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 commentaire
Swapnil Devikar
Swapnil Devikar le 27 Mar 2020
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by