[C#] Switch statements and skipping cases.

Started by
4 comments, last by Spa8nky 14 years, 1 month ago
If I have a switch statement in a loop:

            for (int i = 0; i < 7; ++i)
            {
                switch (i)
                {
                    case 0:
                        // Do stuff here
                        break;
                    case 1:
                        // Do stuff here
                        break;
                    case 2:
                        // Do stuff here
                        break;
                    case 4:
                        // Do stuff here
                        break;
                    case 5:
                        // Do stuff here
                        break;
                    case 6:
                        // Do stuff here
                        break;
                }
}

I know I can skip a case using the "continue" keyword. How can I create a flag that will be able to skip multiple cases if necessary. For example, if the result of case 1 is true then I flag case 4 to be skipped. If the result of case 2 is also true then I would like case 6 to be skipped. Therefore case 4 and 6 will now be skipped using the continue keyword based on checking a single flag. Can anyone please explain how this would be possible to achieve without using multiple booleans for each possible case that could be skipped? Thank you.
Advertisement
What are you trying to do? I suspect there is a more elegant way of achieving your goal.
I am testing separating axes:

            for (int i = 0; i < 15; ++i)            {                switch (i)                {                    case 0:                        axis = a.Axis_X;                            // A0                        break;                    case 1:                        axis = a.Axis_Y;                            // A1                        break;                    case 2:                        axis = a.Axis_Z;                            // A2                        break;                    case 3:                        {                            axis = b.Axis_X;                        // B0                            if (axis == a.Axis_X)                            {                                continue;                            }                            break;                        }                    case 4:                        {                            axis = b.Axis_Y;                        // B1                            if (axis == a.Axis_Y)                            {                                continue;                            }                            break;                        }                    case 5:                        {                            axis = b.Axis_Z;                        // B2                            if (axis == a.Axis_Z)                            {                                continue;                            }                            break;                        }                    case 6:                        axis = Vector3.Cross(a.Axis_X, b.Axis_X);   // A0 x B0                        break;                    case 7:                        axis = Vector3.Cross(a.Axis_X, b.Axis_Y);   // A0 x B1                        break;                    case 8:                        axis = Vector3.Cross(a.Axis_X, b.Axis_Z);   // A0 x B2                        break;                    case 9:                        axis = Vector3.Cross(a.Axis_Y, b.Axis_X);   // A1 x B0                        break;                    case 10:                        axis = Vector3.Cross(a.Axis_Y, b.Axis_Y);   // A1 x B1                          break;                    case 11:                        axis = Vector3.Cross(a.Axis_Y, b.Axis_Z);   // A1 x B2                        break;                    case 12:                        axis = Vector3.Cross(a.Axis_Z, b.Axis_X);   // A2 x B0                        break;                    case 13:                        axis = Vector3.Cross(a.Axis_Z, b.Axis_Y);   // A2 x B1                        break;                    case 14:                        axis = Vector3.Cross(a.Axis_Z, b.Axis_Z);   // A2 x B2                        break;                }


In case 3 the statement:

if (axis == a.Axis_X)

is the same as testing the squared length of the cross product of case 6:

if (axis.LengthSquared() < 0.001f)

I would like to be able to skip the later case and just test the squared cross product in case 3.

The same goes for cases 4, 5, 10 and 14.
http://en.wikipedia.org/wiki/Loop-switch_sequence

"A loop-switch sequence (also known as the for-case paradigm[1]) is a programming antipattern where a clear set of steps is implemented as a byzantine switch-within-a-loop. The loop-switch sequence is a specific derivative of spaghetti code."


You almost certainly don't want to be doing this if you are modelling logic with a known sequence.
Sigh.

To use a for-loop, you need to have a logical set of things to iterate over. You don't, here, yet, so you need to generate it, outside the loop.

One approach would be to fill an array with the axes and somehow skip the unnecessary ones. But this is ridiculously complicated.

What you want to do is observe the pattern in the possible axes:

1 axis for each dimension of A.
1 axis for each dimension of B.
1 axis for each pair of (A, B) dimensions, if they are unequal.

Notice the "for each" in there? That's what you loop over: the dimensions.

So you need to structure your data in a way that lets you do that. Instead of Axis_X, Axis_Y, and Axis_Z, you want an array of 3 Axes.

Then you write three loops. Since each one needs to do the same thing with the axes that are generated, you pull that code into its own function.

It looks like:

for (int i = 0; i < 3; ++i) {  test(a.Axes);}for (int i = 0; i < 3; ++i) {  test(b.Axes);}for (int i = 0; i < 3; ++i) {  for (int j = 0; j < 3; ++j) {    if (i != j || a.Axes != b.Axes[j]) {      test(Vector3.Cross(a.Axes, b.Axes[j]));    }  }}


Yes, it really is that simple.
Quote:Original post by Zahlman
One approach would be to fill an array with the axes and somehow skip the unnecessary ones. But this is ridiculously complicated.


Why is it this was the first thing that sprang to my mind? I was going to do it this way based on return0's response.

Thank you very much for the suggestion Zahlman, I'll begin changing the code so I can access the axes as an array.

I think that is the second time I have made you sigh in as little as two weeks!

This topic is closed to new replies.

Advertisement