Even though there are already good answers, I thought that this approach might be even more intuitive to someone who is new to Boolean algebra then to evaluate a truth table.
First thing you want to do is look, under which conditions you want to execute C. This is the case when (a & b)
. Also when !a
.
So you have (a & b) | !a
.
If you want to minimize you can go on. Just like in "normal" arithmetic's, you can multiply out.
(a & b) | !a = (a | !a) & (b | !a)
.
a | !a is always true, so you can just cross it out, which leaves you with the minimized result: b | !a
.
In case the order makes a difference, because you want to check b only if !a is true (for example when !a is a nullpointer check and b is an operation on the pointer like @LordFarquaad pointed out in his comment), you might want to switch the two.
The other case (/* ... */) is will be always executed when c is not executed, so we can just put it in the else case.
Also worth mentioning is that it probably makes sense either way to put action c into a method.
Which leaves us with the following code:
if (!A || B)
{
doActionC() // execute method which does action C
}
else
{
/* ... */ // what ever happens here, you might want to put it into a method, too.
}
This way you can also minimize terms with more operands, which quickly gets ugly with truth tables. Another good approach are Karnaugh maps. But I won't go deeper into this now.