Please explain me this negative logic

A

aleksa

int* test1 (int* src)
{
int var1, var2;

do {
var1 = *src++;
var2 = *src++;
} while ( (var1 == 1) || (var2 == 2) );

return src;
}

int* test2 (int* src)
{
int var1, var2;

while (1) {
var1 = *src++;
var2 = *src++;

if ( (var1 == 1) && (var2 == 2) ) break;
};

return src;
}

test1 and test2 work the same.

They read var1 and var2 from src++ and return src
when both var1==1 *and* var2==2.

So, why does while loop need || instead of && ?

Can this negative logic also happen anywhere else?

Thanks,
Aleksandar
 
S

Shao Miller

aleksa said:
int* test1 (int* src)
{
int var1, var2;

do {
var1 = *src++;
var2 = *src++;
} while ( (var1 == 1) || (var2 == 2) );

return src;
}
This loop will continue if 'var1 == 1' does not yield 0. That doesn't
mean it will break if 'var1 == 1' yields 0.
This loop will continue if 'var2 == 2' does not yield 0. 'var2 == 2'
will not even be tested if 'var1 == 1' does not yield 0.
This loop will break if either 'var1 == 1' yields 0 or if 'var2 == 2'
yields 0.
int* test2 (int* src)
{
int var1, var2;

while (1) {
var1 = *src++;
var2 = *src++;

if ( (var1 == 1) && (var2 == 2) ) break;
};

return src;
}
This loop will continue if 'var1 == 1' yields 0. That does not mean
that it will break if 'var1 == 1' does not yield 0.
This loop will continue if 'var2 == 2' yields 0. 'var2 == 2' will not
even be tested if 'var1 == 1' yields 0.
This loop will break only if both 'var1 == 1' does not yield 0 and 'var2
== 2' does not yield 0.
test1 and test2 work the same.
I disagree, based on the details above.
 
S

Shao Miller

Shao said:
This loop will continue if 'var1 == 1' does not yield 0. That doesn't
mean it will break if 'var1 == 1' yields 0.
This loop will continue if 'var2 == 2' does not yield 0. 'var2 == 2'
will not even be tested if 'var1 == 1' does not yield 0.
This loop will break if either 'var1 == 1' yields 0 or if 'var2 == 2'
yields 0.
I apologize. A correction:

This loop will break if both 'var1 == 1' yields 0 and if 'var2 == 2'
yields 0.
 
A

aleksa

Sorry, I've made a mistake in my first post.
This is the corrected version:

int* test1 (int* src)
{
int var1, var2;

do {
var1 = *src++;
var2 = *src++;
} while ( (var1 != 1) || (var2 != 2) );

return src;
}

int* test2 (int* src)
{
int var1, var2;

while (1) {
var1 = *src++;
var2 = *src++;

if ( (var1 == 1) && (var2 == 2) ) break;
};

return src;
}

test1 and test2 work the same.

They read var1 and var2 from src++ and return src
when both var1==1 *and* var2==2.

So, why does while loop need || instead of && ?

Can this negative logic also happen anywhere else?

Thanks,
Aleksandar
 
I

Ian Collins

Sorry, I've made a mistake in my first post.
This is the corrected version:

int* test1 (int* src)
{
int var1, var2;

do {
var1 = *src++;
var2 = *src++;
} while ( (var1 != 1) || (var2 != 2) );

return src;
}

int* test2 (int* src)
{
int var1, var2;

while (1) {
var1 = *src++;
var2 = *src++;

if ( (var1 == 1)&& (var2 == 2) ) break;
};

return src;
}

test1 and test2 work the same.

They read var1 and var2 from src++ and return src
when both var1==1 *and* var2==2.

So, why does while loop need || instead of&& ?

Can this negative logic also happen anywhere else?

It has nothing to do with the loops. It is the equivalence of

(var1 != 1) || (var2 != 2) and
(var1 == 1) && (var2 == 2)

That is significant.
 
B

Ben Bacarisse

aleksa said:
Sorry, I've made a mistake in my first post.
This is the corrected version:
} while ( (var1 != 1) || (var2 != 2) );
while (1) {
if ( (var1 == 1) && (var2 == 2) ) break;
};
So, why does while loop need || instead of && ?

One gives a condition to keep going and the other specifies when to
stop. Look up De Morgan's Laws to see the relationship between the
two.

<snip>
 
N

Nick Keighley

Sorry, I've made a mistake in my first post.
This is the corrected version:

int* test1 (int* src)
{
    int var1, var2;

        do {
            var1 = *src++;
            var2 = *src++;
        } while ( (var1 != 1) || (var2 != 2) );

    return src;

}

int* test2 (int* src)
{
    int var1, var2;

        while (1) {
            var1 = *src++;
            var2 = *src++;

            if ( (var1 == 1) && (var2 == 2) ) break;
        };

    return src;

}

test1 and test2 work the same.

They read var1 and var2 from src++ and return src
when both var1==1 *and* var2==2.

So, why does while loop need || instead of && ?

draw out the truth table
Can this negative logic also happen anywhere else?

not (A and B) is equivalent to (not A or not B)
not (A or B) is equivalent to (not A and not B)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top