CBFalconer wrote:
I think you will find that:
IF a AND b THEN c ELSE IF (NOT a) AND (NOT b) AND d THEN e; or IF a AND b THEN c ELSE IF (NOT (a OR b)) AND d THEN e;
with the parentheses for clarity, covers it exactly, including all do-nothing cases.
But it duplicates a. That is a problem if it has side-effects as Dave pointed out, and if it's a longish expression, it's at least more error-prone.
Of course, you can use a temp variable. But then again, the assignment necessary is an additional statement, and might even require another `begin'/`end' (if otherwise the `if' statement was the only one within a loop or conditional).
I have no problems answering the question 'when is c executed' or 'when is e executed' with the above. With the original I do (have a problem).
Seems so, since you apparently misread the condition for e. It should be `if not a and d then e' (no mention of b), just like Dave said.
But I can't really understand why you find reading the original code harder. To me it clearly reads:
if a [...] else if d then e
(where the indentation makes it clear which `else' belongs to what) which is clearly the same as
if not a and d then e
A Karnaugh map is a very useful thing. It may show that the NOT b term is unnecessary in some applications. Or other economies. In some cases I could conceive of:
IF b THEN c ELSE IF d THEN e;
being satisfactory, even though it doesn't match the original logic, simply by mapping explicit do-nothings and don't cares into a Karnaugh map. This sort of thing can show the uselessness of a, and thus lead to major snippage in the original source. Which is an important form of optimization.
Well, I don't know much about Karnaugh maps, but in this example I assume that a is not useless (otherwise the example would be off). So, that doesn't really address the question at hand.
Frank