mercredi 25 mai 2016

Boolean logic, If statement reduction

Probably a very simple question but I'm interested in what options there are. I have three conditions each of which should produce a different output

// special cases
if(!A && B)
     return -1;
if(A && !B)
     return 1;
if(!A && !B)
     return 0;

// general case
return someOtherFunction(a, b);

Which I can get down to -

if(!A) {
    if(!B)
        return 0;
     return -1;
}
if(A && !B)
    return 1;

return someOtherFunction(a, b);

Can I simplify this further? This is in C++ so I am limited to using language specific operators and functions (including STL) etc.

Aucun commentaire:

Enregistrer un commentaire