Relational ops

Hi,

C) If it is true that x != y and it is also true that x != z, does that mean that
z != y is true?

I thought not: for example x=3,y=4,z=4.

But the following returns 1.
1
2
3
4
int x,y,z;
x=!y;
z!=x;
cout<<(x!=z);

This prints "no":
1
2
3
4
5
6
int x,y,z; 
if (x=!y && z!=x)
    {
     cout<<(x!=z);
    }
else cout<<"no";



I thought a variable is assigned 1 when initiated.Why would it print no?

More problems like that :
A) If it is true that x > y and it is also true that x < z, does that mean y < z
is true?
B) If it is true that x >= y and it is also true that z == x, does that mean that
z == y is true?


Last edited on
I have understood nothing about your first two examples.

As for your last two examples then all is obvious and I wonder why you are asking such questions.

Let consider example A

It can be rewritten as

y < x < z.

Now you have three attempts to guess whether y < z.:)

Another example B.

If y <= x and z == x then the first expression can be rewritten as y <= z. Why do you think that z == y ?

Regarding the code for problem C, x = !y is NOT the same as x != y. !y basically takes the value of y and, if it's a non-zero, makes it 0. Remember, ! is the logical NOT operator. It's supposed to be used to invert boolean values, 1 and 0, or true and false. True, converted to any int type is non-zero. False, converted to an int type, is 0. So, as long as y is not 0 in your first part, you are assigning the value of 0 to x. If y is equal to zero, then you are assigning the value of 1 to x. You make the same error in your IF statement. This is why it always prints out no. You are testing the values of 2 expressions and ANDing the results. Since y > 0, !y == 0, and you are assigning the value 0 to x. This expression then evaluates to 0 which is false. Since you are ANDing those two tests, the whole test expression is then false, and the execution skips the FOR block and begins again at the ELSE block.

I hope that makes sense. :) Also, please use the code tags (the <> button to the right when editing or making a new post) to help make code easier to read. :)
Hi,
Vlad:
you're right about the last examples.

Raezzor:
It all makes sense,there's also error in the first example I wrote,
In the first question I was sure of the answer but not sure how to code it.
I tried to initialize the int outside main, and it returns three zeros.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int x,y,z;
int main()
{
  cout<<(x!=y);
  cout<<(x!=z);
  cout<<(y!=z);

}


Thanks!
Topic archived. No new replies allowed.