Switch

using switch can we match for more than one values..

eg:

switcha(a)
{
case 1, 2, 3: printf("ddd");
break;
case 4, 5, 6: printf("mmm");
break;
}

In this case wat i found was only for the last value, i.e 3 and 6 the switch works.

I'd greateful if someone can help me in solving this out(using switch or any other alternative).

abey

{
case 1:
case 2:
case 3: printf("ddd"); break ;
case 4:
case 5:
case 6: printf("mmm"); break ;
}

This should work.

vino

thnx, tht works....

is there anyother option to implement this apart from switch, that wil reduce the number of lines.

You can go ahead with the if-else-if combo.

vino

yup, i'l try tht too...

Once again thnx for the help...

abey

Actually that's what you use switch for, it is much easier to read/debug switch statements than the if-else-if-else... combo. It also makes it easier to extend the program if you want to check for more conditions - you just have to add case statements as required instead of entire if-else blocks.

If you have more than two conditions to check for, you are best off using switch.

use if......
if((a==1)||(a==2)||(a==3))

Does anyone know if it is possible to have a case statement that appears as follows:
{
case int1 && int2: cout << "String1" << endl;
break;

case int1 && int3: cout << "String2" << endl;
break;
}
Where the "&&" portion of the case statement works to have two independent variables that must satisfy the case requirement to produce the output?

  -- OR \--

Should I be using the example provided:
{
case int1:
case int2: cout << "String1" << endl;
break;
case int1:
case int3: cout << "String2 << endl;
break;
# ad infinitum until end switch
}
Thanks in advance.

You cannot put multiple values in one case statement, period. The && also suggests to me you think the example operates differently than it does -- the case values in a switch statement must be integer constants. If it works at all, 'case 1 && 3' would probably reduce to 'case 1', since 1 && 3 evaluates to true.

Thank you for the response, not the answer that I was looking for (as far as "&&"), but one never knows unless the question is asked :slight_smile:
I was hoping that it would interpret the line as being a requirement for the values (of the variables listed. The variable names would have been removed and the values inserted. It was shown this way for "clarity") to be "1 && 2" for the first case to be true, and "1 && 3" for the second case to be true.
I think that perhaps verturing down the road of enumeration might be a work-around for this.
It's a pretty straight-forward problem (A + B = C; not really a mathematical equation, this just shows the AND condition). All I need to do is output a specific string ("C" in this example) for a given set of conditions, but the conditions for "A" and "B" vary and can at times be the same numeric value.
Thanks again.

I think that I have a solution to my problem... an array.
I talked it over with a programmer at work, and he showed me a small (very small) program that does what I am wanting to do, display a value (or string) based on two known inputs. Now the only problem that I have is to organize the array (easier said than done, it will require A LOT of typing - "buf[20][100+]"; hopefully I can come up with a script file that will make my life easier).
I appreciate your input and help. Like I said, it wasn't what I wanted to hear, but sometimes the truth hurts :slight_smile:

Thanks agian!

Now... off to scripting :eek:

Nope, won't work either. An enumerated type can have several aliases that are all the same value, but it can't give you several values for one alias!

What it CAN do, is make your switch statements a lot easier to read. I reccomend it.

I went with the array option. It compiled and worked like a dream!
I entered the two values and was presented with the string that I expected.
Now all that's left for me to do is to write a script that will format the output in C++-like "code" so that it can be copied be copied into the original source file to account for the expected array size changes.
Thanks again for the help, brought me back into reality :slight_smile:
Isn't coding FUN?!? :smiley:

I all seriousness, THANK YOU!