Within a decision structure, does the order of terms change the outcome?

Asked

Viewed 243 times

9

Is there any difference between:

if (variavelA == variavelB)

and

if (variavelB == variavelA)

In Java and C#, is there a difference between the examples? I stress that the question is not limited to the operator only ==, but to all who can be used within decision-making frameworks.

  • In what language? This varies.

  • Any language: Java, C#, VB, Shellscript, Ruby, PHP...

  • The problem is reticence. I can’t speak for all these languages. But in C++, for example, the order of the operands can make a difference depending on how the operator was implemented. I can’t guarantee that all languages "abc" == "abcdef" and "abcdef" == "abc" give equal results. In fact I know that few people know that a slightly different way gives true in a case and false in another. Perhaps if you explain more where you want to get this question, you can produce more interesting answers.

  • In PHP 1 == "1" gives true but 1 === "1" gives false. The question is about the if, on the equality operator, the specific operator == or about relational operations? I find it difficult to answer without having language or type of the known variable, besides having clear about what the question is.

  • 1

    Another factor can be how the language treats the null or variable not started, also note that some languages convert types automatically and this could change results in some way, apart from this "equal" should be "equal".

  • 1

    In this case, the question relates to the order of the terms.

  • At least we know that the issue has nothing to do with if. Order of terms in what? Order specifically on ==? What if the language doesn’t have exactly that operator? What if it has other forms of equal operator? What if it uses one < or >=? Is this important to the question or just the == even?

  • 2

    "Independent of compilers or languages" can not answer! No one knows all the languages that exist, and it is quite possible that there is one where the order of the factors makes a difference (me I know none, but I know few languages). There is no universal operator definition for all languages, each implements this with its own terms.

  • 1

    And I didn’t even mention the typing. What type are the two "terms"? In some languages this may not make a difference, there is language that makes. I am trying to save the question, for this these questions need to be turned. Otherwise, it is too broad.

  • 2

    The order of the assessment of both sides of equality can also affect the outcome. Imagine that one operand is a variable, and the other is a function that changes the value of that variable... Any language that has side effects is subject to this situation (perhaps except if the language makes a rigid separation between procedure and function - and not let subroutines with side effects return values). And this without getting into the merit of Weak Typing, overload of operators, etc.

  • This will depend on the language!!! so if you could improve the question is to limit which! who knows improvement and would be of great value this question @Patrick.

  • 1

    Okay, I made a few changes and limited it to two languages I use.

  • It has improved a lot. I still have doubts if it is easy to answer because to have a correct answer, one needs to understand about both languages. I had not been able to speak properly about Java. And if it is to have separate answers, perhaps I will fall into this problem http://meta.pt.stackoverflow.com/questions/289/o-que-fazer-com-questions-que-podem-ter-multiplas-answers. Even to answer in C# although it brings a good learning, at the beginning of the research I’ve realized that there may be so many unusual cases, I doubt I’ll cover all.

  • So the answer would be: under normal conditions the order of equality and inequality affects nothing. But there are countless really extraordinary situations that can make that not true. In other relational operators can already make all the difference. Simple: 1 > 3 and 3 > 1 produce obviously different results.

  • 2

    What about 1 > 3 and 3 < 1?

  • @Patrick if you change the operator you are making another completely different comparison, you are no longer talking about order terms in the same operation.

  • I do not know if terms would be the most appropriate word... But in short, it would be this: operators and values.

  • I tried to save it. This question is very broad and without a complete chapter of a book it can only be partially answered (the answers show this), even after the editions and attempts of clarification (which I think has worsened the situation, became even wider). The problem is very complex. The only way to respond succinctly is "anything can happen". Only one example of things that can affect can be found in http://answall.com/questions/18910/qual-a-diferenca-no-uso-do-metodo-equals-para-o-operator/18920#18920. Many other factors exist.

Show 13 more comments

4 answers

6


The order can change the result in languages where there is operator overload.

Differences with overloaded operators

In C#, for example, you can create a specific implementation for the comparator == and several other operators. See a example:

public static DBBool operator ==(DBBool x, DBBool y) 
{
   if (x.value == 0 || y.value == 0) return dbNull;
   return x.value == y.value? dbTrue: dbFalse;
}

So variavelB and variavelA may be of different types and consequently the method overloaded == may have been in implemented differently in one or even in the two types.

Some languages that implement operator overload use them as simple method calls. In Ruby, for example, you can even call the operator as any method.

Consider the examples below. Both sum operations result in value 3:

a = 1 + 2 
b = 1.+(2)

The same goes for comparison:

if a == b
if a.==(b)

This might be different than:

if b == a
if b.==(a)

Obviously the above examples may differ if a and b are different classes.

No differences when it is not possible to extend the language

In languages such as Java or PHP, the order of comparison of variables is not important, because the operator == will always compare:

  1. The values for primitive variables.
  2. If they are objects, if they both point to the same instance.

Final considerations

The concepts presented here may vary subtly or abruptly from one language to another or even between versions of the language itself.

The important thing is that the developer understands the current mechanism that the language uses underneath the scenes.

  • I agree, without extending the language the result has to be equal and extend, can do anything in reality...

  • So it is not possible to overwrite or overwrite an operator in Java?

  • 1

    @Patrick No. It scared me a little bit when I started working with language. Basically what you do is you use the method equals() to compare equality and compareTo() to know if one object is larger than another. Obviously the order matters and the left object cannot be null. Also, numerical classes like BigDecimal implement methods with add, subtract, multiply and divide. This makes it very difficult to create new types and use them as the primitive ones, but it is something that gets used.

2

I will give a partial response, aimed at . NET and Mono.

The behaviour of the operator == varies as follows:

  • For reference types, it checks whether the references are equal, i.e.: if both objects are actually the same;
  • For types by value, it will perform a proper equality implementation of the type. Every type by value has to have its own logic of equality. Try to define a struct, and then create two instances of that struct and try to see if they’re the same ==. Your code will not even compile ;) Note that in general the Framework’s native structs (bool, int, float etc.) have their own implementation for that operator.

Note that in both cases it is possible to overload the operator ==. In fact, it’s obligatory overload it in the second case if you are going to use it in your code.

If you overload the operator, then the logic of equality is completely yours. You know which operators are left and right, and you can do whatever you want with them. In this case, whether the order matters or not is a decision of the developer. If the operator is not overloaded, the order really makes no difference.

The same goes for comparison operators (>, <, >= and <=), if you refer to comparisons that must be equivalent, i.e.: a < b in relation to b > a. It depends only on the operators being overloaded, and the logic that was used in the overload. For comparable standard types in . NET and Mono (int, float and even DateTime), the comparison is strictly numerical, so for these types whatever.

  • 1

    agree, if the operator is not overloaded, the result is the same or does not exist (because it is no longer an equality but an extension of the concept to compare objects)

1

For the operator "equal" (==) the order of conditions (variables, values, etc.) do not change the result.

Language-independent.

  • 6

    This information is not true. There are languages that makes or at least can make a difference.

  • @bigown is that Apollo is not referring only to the languages mentioned by OP?

  • @Renan the question was edited (take a look at the history), when he answered it was another question and clearly he answered upon this previous premise. He even highlighted this in the reply. But the answer also does not answer something correct for these two languages. I started researching and the subject is complicated.

  • 1

    +1 - I think it can only make a difference if the concept of equality is redefined (operator overload, equality between structures or expressions that change when they are evaluated for example). By mathematical definition the equality is symmetric (therefore by definition (a == b) has to have the same result of (b == a). What you don’t obey is no longer equality. Actually to contradict, just show an example in a language where an equality (unmodified) has resulted different when evaluated symmetrically and so far not seen any.

  • @bigown could you exemplify a language where the order of the "variables" influence the outcome of the equality condition? Exactly Laurent, my answer is based on the mathematical concept of equality.

  • C++, Clipper, and any other Overload operator and the programmer want to make a difference. I know there are others but I’m not going to research, that’s the problem of questions that are too wide and should be closed.

Show 1 more comment

0

In Java it makes no difference.

I’ve done it several times using the equality operator == and the result is the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.