Borthamonth Borthamonth - 19 days ago 5
C++ Question

Negate expression containing && operator

I was making a slot machine that loops until all three

std::string
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

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

Comments