I have a large number of test condition that I need to evaluate based upon 2 values (one is a double, the other is an int). For example, here is what I have now...

if ( strcmp(myString, "CONDX") == 0 )

if (( fVal >=3.0 && score >= 344 ) ||

( fVal >= 4.5 && score >= 294) ||

( fVal >=6.0 && score >= 240 ) ||

// 20 more conditionals go here... )

{

}

else if ( strcmp(myString, "LIMTD") = 0 )

if (( fVal >=3.0 && score >= 487 ) ||

( fVal >= 4.5 && score >= 428) ||

( fVal >=6.0 && score >= 294 ) ||

// 20 more conditionals go here... )

{

}

There is no formula that can be applied between fVal and score.

The breaks for fVal are ALWAYS the same (3, 4.5, 6, etc - for all 23 comparisons).

Is there a more elegant way to write this? Is the performance of this approach fairly fast (this comparison is done ALOT).

if ( strcmp(myString, "CONDX") == 0 )

if (( fVal >=3.0 && score >= 344 ) ||

( fVal >= 4.5 && score >= 294) ||

( fVal >=6.0 && score >= 240 ) ||

// 20 more conditionals go here... )

{

}

else if ( strcmp(myString, "LIMTD") = 0 )

if (( fVal >=3.0 && score >= 487 ) ||

( fVal >= 4.5 && score >= 428) ||

( fVal >=6.0 && score >= 294 ) ||

// 20 more conditionals go here... )

{

}

There is no formula that can be applied between fVal and score.

The breaks for fVal are ALWAYS the same (3, 4.5, 6, etc - for all 23 comparisons).

Is there a more elegant way to write this? Is the performance of this approach fairly fast (this comparison is done ALOT).

gnessh's idea of storing your condition values in arrays is a good one, though, as long as performance here is not too critical (I don't imagine it would be a bottleneck) - it will improve legibility.

*/

const int MAX_STR_IDX = 2;

const int MAX_BOUND_IDX = 23;

char *str[MAX_STR_IDX] = [

"CONDX",

"LIMTD"

];

double fValBound[MAX_FVAL_IDX] = [3.0, 4.5, 6.0, ...];

int scoreBound[MAX_STR_IDX][MA

[344, 294, 240, ...],

[487, 428, 294, ...]

];

/* you can improve performance a little by sorting both sets of parallel arrays above by likelihood of each case

*/

int i, j, conditionMet;

for (i = 0; i < MAX_STR_IDX && strcmp(myString, str[i]); i++);

if (!strcmp(myString, str[i])) {

for (j = 0, conditionMet = 0; j < MAX_BOUND_IDX && conditionMet == 0; j++) {

if (fVal >= fValBound[i][j] && score >= scoreBound[i][j]) {

conditionMet = 1;

}

}

if (conditionMet) {

/* here is where your conditional code goes */

/* note that the variable i contains the index of the string that myString matched, if you need that. */

}

}

int iVal = 10 * fVal;

if ( strcmp(myString, "CONDX") == 0 )

if ((iVal >=30 && score >= 344 ) ||

( iVal >= 45 && score >= 294) ||

( iVal >=60 && score >= 240 ) ||

// 20 more conditionals go here... )

{

etc.

If you want more speed, arrange your conditional so the most common cases are first. If you want more maintainability, use the arrays suggested above and leave the conditionals in natural order.

*/

int priority[MAX_BOUND_IDX] = [3, 1, 2, ...];

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.