# Thread: Boolean prob

1. ## Boolean prob

Hi, I'm quite new to cpp programming, and I made this small script using Dev-C++.

Code:
```#include <iostream>

using namespace std;

int main()

{
int x;

cout<<"Enter 1 or 0: ";
cin>>x;
cin.ignore();
if ( x = ( 1 || 0 ) ) {
cout<<"Hello World";
}
else if ( x = ( 1 && 0 ) ) {
cout<<"World Hello";
}
cin.get();
}```
Problem here is, if I enter 0, it should be printing World Hello instead of Hello World, which doesn't happens. Ofcourse I could easily solve this by adding a NOT operator, but I want to know about the AND and OR operators in specific. Could someone help me out?

2. You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.

You would want something like this:
Code:
```if(x = 1)
cout << "Hello World";
else
cout << "World Hello";```

3. Originally Posted by xmltorrent
You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.

You would want something like this:
Code:
```if(x = 1)
cout << "Hello World";
else
cout << "World Hello";```
Aha! Ofcourse I understand what was in you code, but I was just playing around with the booleans. Thank you.

4. You're first if statement is checking to see if the value of x is EITHER a 1 or 0. If it is EITHER of those values, then it prints Hello World.

In a literal sense, your second if statement is saying if the value of x is BOTH 1 and 0, then it prints World Hello.
Not exactly.

Code:
`if ( x = ( 1 || 0 ) )`
1 || 0 always evaluates to a true, which you then assign to x, automatically making it execute.

Code:
`else if ( x = ( 1 && 0 ) )`
1 && 0 evaluates to a false, which you then again assign to x, and making it not execute.

5. couple of problems:

>> if ( x = ( 1 || 0 ) )

first off, your assigning a value to x, use == for comparison. the above expression would then evaluate to:

if(x == 1)

since (0 || 1) == 1

so the correct expression would be:

if (x == 1 || x == 0)

6. That solved the problem, assigned them to x, instead of checking them, stupid me. Tnx.

Popular pages Recent additions