Borthamonth Borthamonth - 1 year ago 63
C++ Question

Negate expression containing && operator

I was making a slot machine that loops until all three

s have the same value. I'm confused as to why in this code the
|| operator
is giving the desired result rather than the
&& operator

std::string slotA,slotB,slotC;
do {
//Fill slotA, slotB and slotC with some data
} while (slotB != slotC || slotB != slotA);

Answer Source

Your stop condition should be:

while (!(slotB == slotC && slotB == slotA));

However, in Bool-Algebra !(slotB == slotC && slotB == slotA) is equal to slotB != slotC || slotB != slotA

This rule is called De Morgan Law

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download