Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big if else statement

Tags:

c++

algorithm

If have a big ( about 100 plus) if else statement like below and the if else condition might be irregular(for example some depends on 3 variables, some on 4), is there any way of making it simpler?

Basically I have a table of around 100 plus rows, with the a,b,c and d as the column. Based on a,b,c and d, I need to perform 3 different type of function.

The table describes a set a business rules.

 uint8 a;
 uint8 b;
 uint16 c;
 uint8  d;

 if      ( a == 1 &&   b == 1           && c == 0)           { functionA();}
 else if ( a == 5 &&   b == 5           && c == 2 && d == 2) { functionB();}
 else if ( a == 1 && (b ==36 || b == 7) && c == 0)           { functionC();}  
 else if ( a == 3 &&   b == 3                     && d == 50) {functionA();}
    :
    :
like image 285
leslieg Avatar asked Jun 08 '11 02:06

leslieg


People also ask

What is an example of an if-else statement?

Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false. if (testScore > 60) cout << "You pass" << endl; if (testScore > 90) cout << "You did great" << endl; For example, before noon (AM) and after noon (PM) are mutually exclusive.

Is there a limit for else if statements?

Yes, it's almost always unbounded recursion. a large amount of else if won't overflow the stack or cause a crash, but if you have pages and pages of it then it's a sign that the code should be structured better.

Which is faster if or else?

if / else if / else is more flexible (hence better), but switch is slightly faster because it just computes the condition once and then checks for the output, while if has to do this every time.

What is better than if-else?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.


1 Answers

There are lots of ways to make it simpler, for example:

  • you can populate a map from a struct holding a, b, c, and d values to check for to the function to call (the code to populate the map may still be a mess, but it'll be faster and cleaner executing; can add two keys for cases ala b == x || b == y)
  • you can manually factor the conditions: given your example, if (a == 1) if (b == 1 && c == 0) functionA(); else if ((b == 36 || b == 7) && c == 0) functionC();. switch statements may make this cleaner. In such a factoring, you can also use <, <=, >, and/or >= to divide larger search spaces, improving performance to from O(N) to O(log2N).
  • for the common simple case of testing a, b, c, and d once, use a macro ala #define ELIF_ABCD(A, B, C, D, F) else if (a == (A) && b == (B) && c == (C) && d == (D)) F();. Add macros as necessary for other combinations of tests e.g. ABD, ABC, AD.
  • (may make the code more cryptic), but could explore bit-shifting and ORing together the values into a large enough type (int64_t) then e.g. binary search an array of function pointers

Something to look out for though is that the if/else chain may contain things like:

if (a == 1 && c == 3 && d == 2) functionY();
else if (a == 1 && b == 2 && c == 3) function X();

Here, the order is significant as an input can match both. This aspect can easily get lost or altered if the searches are factored differently or some manner of indexing to function pointer is used, which is one argument in favour of the macro approach above.

like image 185
Tony Delroy Avatar answered Oct 06 '22 01:10

Tony Delroy