mralexlau
mralexlau

Reputation: 2003

Is there an algorithm for comparing if statements?

Is there an algorithm and/or tool for detecting if two sets of if statements with the same conditions are equivalent? When I say "equivalent" - I mean that they execute the same code for all given inputs. For example:

# set 1
if a
  if b
    someMethod()
  end
end

# set 2
if a && b
  someMethod()
end

# set 3
if a || b
  someMethod()
end

Given these 3 sets of if statements, you could consider set 1 and set 2 to be "equivalent" in that someMethod only gets executed when a and b are true. Likewise set 1 and set 3 are not equivalent since someMethod won't get executed by set 1 when a is true and b is false, but will be executed in set 3 under those same conditions.

Upvotes: 2

Views: 228

Answers (1)

Spektre
Spektre

Reputation: 51845

I would use Karnaugh Maps which is standard for non sequential logic synthesis and analysis.

  1. create Karnaugh Map for each set of ifs

  2. compare the maps

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

Sorry for a short answer but I do not have a clue what else to add because this is basic knowledge.

[edit1] just your case ...

example

  • set1 == set2
  • set3 is different

Upvotes: 4

Related Questions