logical and relational operators

Mar 21, 2014 at 9:13pm
closed account (EwCjE3v7)
Is this right?

 
  i != j < k // these are int values 


would this compare k with the bool returned i != j
Mar 21, 2014 at 9:19pm
When in doubt (unless you are absolutely sure), use parentheses.
( i != j ) < k or i != ( j < k )
Mar 21, 2014 at 9:22pm
!= equals not equal to. ! equals not. And I have tried that before, and it did not work. You cannot even do:
 
i = j < k;

That, and say that out loud. It doesn't even SOUND right in pseudo-code.
'i' equals 'j' less than 'k'

That and you cannot just do i != j; either.

P.S: I am not actually testing these out as I typed this, but I just remembered me trying that a couple of months ago and it not working.

PPS: Be a bit more specific in your questions.

PPPS: Last P.S. here, but when you come on this forum, I wouldn't recommend just putting a line of code and asking if it is right. Programming is trial and error most of the time, so before you do that, try it in your IDE. It would save you more time.
Mar 21, 2014 at 9:28pm
closed account (EwCjE3v7)
When in doubt (unless you are absolutely sure), use parentheses.
( i != j ) < k or i != ( j < k )


Its an exercise which I forgot to state, and it wanted be to explain that. Sorry that fault was on my behalf, this section was supposed to show me to use parentheses and what would happen if you didn`t

!= equals not equal to. ! equals not. And I have tried that before, and it did not work. You cannot even do:

i = j < k;

That, and say that out loud. It doesn't even SOUND right in pseudo-code.
'i' equals 'j' less than 'k'

That and you cannot just do i != j; either.

P.S: I am not actually testing these out as I typed this, but I just remembered me trying that a couple of months ago and it not working.

PPS: Be a bit more specific in your questions.

PPPS: Last P.S. here, but when you come on this forum, I wouldn't recommend just putting a line of code and asking if it is right. Programming is trial and error most of the time, so before you do that, try it in your IDE. It would save you more time.


My fault, this code does compile but it`s an exercise and it wants me to explain, what would this do.

And for the line of code I`m sure it dosn`t say anywhere in the forums that you aren`t allowed :P. I wanted to know if what would happen was right
Mar 21, 2014 at 9:30pm
> You cannot even do: i = j < k;

If i, j and k are of type int, why can't we do i = j < k ;?


> Its an exercise which I forgot to state

The relational operator < has a higher precedence than !=
http://en.cppreference.com/w/cpp/language/operator_precedence
Last edited on Mar 21, 2014 at 9:36pm
Mar 21, 2014 at 10:44pm
Really? I tried that like, 20 minutes ago when I came inside and I got a compiler error. i = j < k; did not work for me. What compiler are you using? I am using MinGW.
Mar 21, 2014 at 11:14pm
closed account (EwCjE3v7)
It works and is true and i forgot that does have higher precedence so
i will be compared to true or false depending on j < k
Mar 21, 2014 at 11:33pm
@AceDawg
It works perfectly fine, see http://ideone.com/5aoTDI

Maybe you were trying to use std::cout and stream insertion operators along with the logic operators? In that case operator precedence would have given an error along the lines of 'invalid operands to operator!= (types are std::basic_ostream<char>, int) ' (or something like that).
Mar 22, 2014 at 8:18pm
Oh wow I was. I just tried it the way this thread is about and it worked perfectly fine. You learn something new everyday, I guess. haha
Last edited on Mar 22, 2014 at 8:19pm
Topic archived. No new replies allowed.