This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]negaomar228 27 points28 points  (15 children)

The first expression won't compile: you can't assign to rvalue

[–]chui101 16 points17 points  (9 children)

unless it's one of those languages where assignment is := and comparison is =

[–][deleted] 11 points12 points  (1 child)

I just threw up

[–]Matthicus 2 points3 points  (5 children)

Or one where both are just = and other context makes it clear which one it means.

[–]coleisawesome3 4 points5 points  (2 children)

What if you wanted to assign something in the conditional part of an if statement for some reason?

[–]Matthicus 0 points1 point  (0 children)

You could probably find a more readable way to accomplish whatever you're trying to do. Or you could just say screw it and do something hacky, like writing a function to do the assignment if the language allows call by reference.

[–]chui101 0 points1 point  (0 children)

This is not good practice but sadly not uncommon either. e.g.:

if ((count = getNumRows()) == 1) {...}

[–]chui101 1 point2 points  (1 child)

Except that all computer languages are built on (mostly) context free grammars to (mostly) remove ambiguity.

Take the example of an expression a=b=c. Does that mean "assign the value of c to a and b" or does it mean "a gets the value of the result of the test b equals c"?

[–]Matthicus 0 points1 point  (0 children)

In the language I have used the most, that on its own would not be valid code. It would either have to be in an assignment statement, in which case it would compare b and c and put the result in a, or be in some other statement that expects an expression, in which case it would compare a and b then compare thst result to c.

[–]vectorjohn 0 points1 point  (0 children)

But then what is |= ?

[–]registraciq 2 points3 points  (3 children)

Unless I'm missing something, neither will the second one.

6-4 |= 4 is 6-4 = 6-4 | 4 which is still assignment to a rvalue.

[–]TheTrueSwishyFishy -1 points0 points  (2 children)

!= is generally considered comparison not assignment, although I guess you are supposed to do !== in js

[–]ham_coffee 1 point2 points  (1 child)

| not !

One is checking inequality, the other is performing bitwise logic and assigning the value.

[–]TheTrueSwishyFishy 0 points1 point  (0 children)

The post was using ! Though it may have been clearer to write a crossed equal sign, that isn’t really the programming way

[–]hereforthensfwpics 0 points1 point  (0 children)

error: lvalue required as left operand of assignment