Etienne
Etienne

Reputation: 95

Optimize/Reduce condition expression in if statement

The goal is to optimize big condition expression by finding values dependencies between variables in order to reduce the logical OR statements.

Let's say we have the following condition:

if((A == 0 && B == 0) ||  (A == 0 && B == 1) || (A == 0 && B == 2) ...

a certain number of time... Is there a way to reduce this kind of example by having automatically the following condition:

if(A == 0 && (B >= 0 && B <= 2))

The numbers involved in the first condition are only known just before the condition, it can't be typed manually. There can be also hundreds of logical OR operators involved in the condition. There can be gap between values (maybe use of % operator is needed) but there is always a pattern.

Any library or existing algorithm which can find the dependencies between the variables ?

Let's have another example:

if((A == 0 && B == 0) ||  (A == 0 && B == 2) || (A == 0 && B == 4)

can be translated to:

if((A == 0 && B%2 == 0)

One more:

if((A == 0 && B == 0 && C == 0) ||  (A == 0 && B == 2 && C == 0) || (A == 0 && B == 4 && C == 0) || (A == 0 && B == 0 && C == 1) ||  (A == 0 && B == 2 && C == 1) || (A == 0 && B == 4 && C == 1))

would be transformed into:

if(A==0 && B%2==0 && C>=0 && C<=1)

For the variables involved, I have all the values per terms.

I have something like [[0,0,0],[0,2,0],[0,4,0],[0,0,1],[0,2,1],[0,4,1]] (e.g. the last example)

Thanks for your time and answers !

Upvotes: 1

Views: 120

Answers (1)

Amir Kadyrov
Amir Kadyrov

Reputation: 1288

if((A == 0 && B == 0) ||  (A == 0 && B == 2) || (A == 0 && B == 4)

cannot be translated to

if((A == 0 && B%2 == 0)

cuz

B == 6

and etc

Upvotes: 1

Related Questions