view raw
Borthamonth Borthamonth - 6 months ago 35
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);


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