How can I incoporate logics of and/or in switch case problem
    8 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Eng. Fredius Magige
      
 le 28 Août 2017
  
    
    
    
    
    Réponse apportée : Walter Roberson
      
      
 le 28 Août 2017
            How can I use/incorporate and adoption of logic (and/or) combination in switch case problem. Any assistance is appreciated too
2 commentaires
  Jan
      
      
 le 28 Août 2017
				The question is too general for a meaningful answer. What does "incorporate and adoption of logic" mean? Something like:
switch (a || b) && c
case 0, ...
case 1, ...
end
?
  KALYAN ACHARJYA
      
      
 le 28 Août 2017
				More clarification needed for exact answer.
o_p=A && B;
o_p=1 or 0;
switch(o_p==1)
statement;
switch(o_p==0)
Réponse acceptée
  Walter Roberson
      
      
 le 28 Août 2017
        You can do "or" by providing a cell array of all of the matches
switch mod(x, 4)
  case {0, 3}
    disp('first')
 case {1, 2}
    disp('second')
 otherwise
    disp('non-integer')
 end
You can get more complicated by matching on arrays or cell arrays:
switch [mod(x,2), y+2]    %not recommended
 case [0, 7]
   disp('first')
 case [0, 8]
   disp('second')
 case [1, 7]
   disp('third')
 case [1, 8]
   disp('fourth')
end
switch {mod(x,2), S}      %not recommended
  case {0, 'low'}
    disp('first')
  case {0, 'hi'}
    disp('second')
  case {1, 'low'}
    disp('third')
  case {2, 'hi'}
    disp('fourth')
  end
Doing "or" is more complicated:
   switch true                    %blech!
     case x == 0 || strcmp(S, 'low')
       disp('first')
     case x == 1 || strcmp(S, 'hi')
       disp('second')
     otherwise
       disp('third')
     end
0 commentaires
Plus de réponses (0)
Voir également
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!



