|
Another way ... and a more generalized way ... to approach a problem like this one is to split the problem into two consecutive halves.
First, you decide whichever one of however-many mutually exclusive "states" you are now in ... Then, you carry out the appropriate response for each "state."
For example, consider logic such as the following:
state = 0;
if (a > b) state = state + 1;
if (d > e) state = state + 2;
if (f > g) state = state + 4;
...
(Now, state has a unique value from 0 to 4+2+1=7, representing every possible combination of the above decisions. So...)
switch (state) {
case 0: ...
case 1: ...
...
}
In this way, you have explicitly separated the first concern, of "deciding what to do" (and of "how to make that decision"), from the second concern of "now, go and do the appropriate thing." In this way, you have now paved the way to expressing fairly-arbitrary decision logic in a way that will always be clear.
|