Turning logical expression around
Posted
by
BluePrint
on Stack Overflow
See other posts from Stack Overflow
or by BluePrint
Published on 2013-11-10T08:46:30Z
Indexed on
2013/11/10
9:53 UTC
Read the original article
Hit count: 145
I have the following code:
bool s = true;
for (...; ...; ...) {
// code that defines A, B, C, D
// and w, x, y, z
if (!(A < w) && s == true) {
s = false;
}
if (!(B < x) && s == true) {
s = false;
}
if (!(C < y) && s == true) {
s = false;
}
if (!(D < z) && s == true) {
s = false;
}
}
This code is working well. However, I want to, for several (unimportant) reasons, change the code so that I can initiate s = false;
and set it to true inside the if-statement. It tried the following:
bool s = false;
for (...; ...; ...) {
// code that defines A, B, C, D
// and w, x, y, z
if (A >= w && s == false) {
s = true;
}
if (B >= x && s == false) {
s = true;
}
if (C >= y && s == false) {
s = true;
}
if (D >= z && s == false) {
s = true;
}
}
However, this is not working properly as the code above is working. I know thought wrong somewhere in the logic, but I can't figure out where. Does anbyone see my probably obvious error?
EDIT: Added three more if-statemets. Missed them since they were commented away.
© Stack Overflow or respective owner