Thread Subject: switch statement with more than one switch expressions

Subject: switch statement with more than one switch expressions

From: Patrick

Date: 9 Jun, 2006 06:04:48

Message: 1 of 5

Hello

I have two variables

test1 = true;
test2 = true;

Is is possible to use these two expressions in a switch statement?

Like

switch(test1,test2)
  case(true,true)
  ...
  case(true,false)
  ...
end;

it would be more readable if I could do it this way than using
if/else.
Because I have to work of the 4 different possible combinations of
test1 and test2

Subject: switch statement with more than one switch exp

From: Perry

Date: 9 Jun, 2006 07:30:43

Message: 2 of 5

I think you're better using an IF THEN statement as in my experience
the switch case cannot handle anything more complicated than a single
comparison :

if (~var1 && ~var2)
    disp('both false')
elseif (~var1 && var2)
    disp('var2 true')
elseif (var2 && ~var2)
    disp('var1 true')
else
    disp('both true')
end

Perry

Patrick wrote:
>
>
> Hello
>
> I have two variables
>
> test1 = true;
> test2 = true;
>
> Is is possible to use these two expressions in a switch statement?
>
> Like
>
> switch(test1,test2)
> case(true,true)
> ...
> case(true,false)
> ...
> end;
>
> it would be more readable if I could do it this way than using
> if/else.
> Because I have to work of the 4 different possible combinations of
> test1 and test2

Subject: switch statement with more than one switch expressions

From: John D'Errico

Date: 9 Jun, 2006 07:55:21

Message: 3 of 5

Patrick wrote:
>
>
> Hello
>
> I have two variables
>
> test1 = true;
> test2 = true;
>
> Is is possible to use these two expressions in a switch statement?
>
> Like
>
> switch(test1,test2)
> case(true,true)
> ...
> case(true,false)
> ...
> end;
>
> it would be more readable if I could do it this way than using
> if/else.
> Because I have to work of the 4 different possible combinations of
> test1 and test2

Its quite easy to solve, at least for
boolean tests. Simply use a binary to
decimal encoding.

switch(2*test1 + test2)
 case 3 % true,true
 ...
 case 2 % true,false
 ...
 case 1 % false,true
 ...
 case 0 % false,false
 ...
end

This trick can be very useful for
encoding multiple tests. I've used it
for at least 4 concurrent tests in an
application.

HTH,
John D'Errico

Subject: switch statement with more than one switch exp

From: Jos

Date: 9 Jun, 2006 09:01:24

Message: 4 of 5

Patrick wrote:
>
>
> Hello
>
> I have two variables
>
> test1 = true;
> test2 = true;
>
> Is is possible to use these two expressions in a switch statement?
>
> Like
>
> switch(test1,test2)
> case(true,true)
> ...
> case(true,false)
> ...
> end;
>
> it would be more readable if I could do it this way than using
> if/else.
> Because I have to work of the 4 different possible combinations of
> test1 and test2

You might convert them into a string

a = true ; b = false ;
str = sprintf('%d',[a b]) ;
switch str,
    case '00', disp('both false') ;
    case '10', disp('true and false') ;
    case '01', disp('false and true') ;
    case '11', disp('both true') ;
end

hth
Jos

Subject: switch statement with more than one switch exp

From: Loren Shure

Date: 9 Jun, 2006 12:00:54

Message: 5 of 5

Jos wrote:
> Patrick wrote:
>>
>> Hello
>>
>> I have two variables
>>
>> test1 = true;
>> test2 = true;
>>
>> Is is possible to use these two expressions in a switch statement?
>>
>> Like
>>
>> switch(test1,test2)
>> case(true,true)
>> ...
>> case(true,false)
>> ...
>> end;
>>
>> it would be more readable if I could do it this way than using
>> if/else.
>> Because I have to work of the 4 different possible combinations of
>> test1 and test2
>
> You might convert them into a string
>
> a = true ; b = false ;
> str = sprintf('%d',[a b]) ;
> switch str,
> case '00', disp('both false') ;
> case '10', disp('true and false') ;
> case '01', disp('false and true') ;
> case '11', disp('both true') ;
> end
>
> hth
> Jos

Or you could have nested switch statements, the outer one for the first
variable, the inner one for the second.

--
--Loren
http://blogs.mathworks.com/loren/

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com