Is there an algorithm for comparing if statements?

Is there an algorithm and / or tool to detect if two sets of operators if

with the same conditions are equivalent? When I say equivalent, I mean that they execute the same code for all data. For example:

# set 1
if a
  if b

# set 2
if a && b

# set 3
if a || b


Given these three sets of operators if

, you can consider set 1 and set 2 to be the equivalent in that it someMethod

only holds when a and b are true. Likewise, set 1 and set 3 are not equivalent, since it someMethod

will not execute over set 1 if a is true and b is false, but will run on set 3 under the same conditions.


source to share

1 answer

I would use Karnaugh Maps , which is a standard for sequential logical synthesis and analysis.

  • create a Karnaugh map for each set of if


  • compare cards

    if the mapping is the same, then the if statements are equivalent, if not, then they are different.

Sorry for the short answer, but I don't know what to add because this is basic knowledge.

[edit1] just your case ...


  • set1 == set2

  • set3

    is different


All Articles