Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C Boolean logic... not being very logical

Status
Not open for further replies.

zanza

Programmer
Feb 18, 2002
89
US
can someone explain to me why these two statements are different?

while(!(choice=='u'||choice=='U'||choice=='d'||choice=='D'))
while(choice!='u'||choice!='U'||choice!='d'||choice!='D')

sorry to be asking such a moronic question, but i swear ive used similar statements to the second one before, but it doesnt work now.

žÅNžÅ
 
You also need to change your || into &&

[tt]choice!='u'||choice!='U'[/tt]
is equivalent to
[tt]!(choice=='u'[highlight]&&[/highlight]choice=='U')[/tt]

But such a statement is always true, so maybe that's the problem.


Code:
#include <stdio.h>

int main( void ) {
    int a, b;
    for ( a = 0 ; a <= 1 ; a++ ) {
        for ( b = 0 ; b <= 1 ; b++ ) {
            printf("%d %d %d %d\n", a, b, ( a!=0 || b!=0 ), !( a==0 && b==0 ) );
        }
    }
    return 0;
}

--
 
The first is a set of ors and then the negation of that set.

In the first, if the character is equal to any of those chars then the result is false.

The second is a set of not equal ors.

This one is always true.

Hope that helps.
-JavaDude32
 
hi,

> C-Language speaking:

!( a==b || c==d ) is equivalent to ( a!=b && c!=d )


> Mathematical (Logic bracnh) speaking:

you can find this in the "De Morgan's Laws"

( Google it and look for some simple article)


> In everydaylife you can say:

A doctor suggests: you can eat

vegetables OR fruit

Another suggests you the opposit : you can NOT eat

vegetables AND fruit



bye
 
No such animal as 'special' C logic; it's a common logic. Negation of Predicate1 or Predicate2 is not Predicate1 and not Predicate2.
Negation of A is equal to B is A is not equal to B...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top