noob noob - 1 year ago 105
C Question

is switch case without break a bad practice?

I have a switch in which I check a property of some sort and if the check yields a specific value, I want to check another value, so I did something like this:

case NA:
if(value_0 == property_B)
property_A = value_a;
else if(value_1 == property_B)
property_A = value_b;
case value_0:
case value_1:

So, I know this solves my problem, but I don't know if this is a good idea or maybe should I go about this another way

# the NA case is something like a default case but not exactly, because it does tell me something, but not enough

Answer Source

It depends on what you want to do. If you get to the case NA, without the break keyword, the value_0 case will be executed after one of the two if branches is finished. So if that's the behaviour you want, it's OK not to use break, but I don't think that's what you wanted to do.

I suggest you simply move the if - else statement above the switch and remove the NA case. This way, you will first assign the correct data to property_A and then you can do whatever you want with it in the switch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download