What is the difference between = and ==?

雨燕双飞 提交于 2020-01-10 03:19:09

问题


What is the difference between = and ==? I have found cases where the double equal sign will allow my script to run while one equal sign produces an error message. When should I use == instead of =?


回答1:


It depends on context as to what = means. == is always for testing equality.

= can be

  1. in most cases used as a drop-in replacement for <-, the assignment operator.

    > x = 10
    > x
    [1] 10
    
  2. used as the separator for key-value pairs used to assign values to arguments in function calls.

    rnorm(n = 10, mean = 5, sd = 2)
    

Because of 2. above, = can't be used as a drop-in replacement for <- in all situations. Consider

> rnorm(N <- 10, mean = 5, sd = 2)
 [1] 4.893132 4.572640 3.801045 3.646863 4.522483 4.881694 6.710255 6.314024
 [9] 2.268258 9.387091
> rnorm(N = 10, mean = 5, sd = 2)
Error in rnorm(N = 10, mean = 5, sd = 2) : unused argument (N = 10)
> N
[1] 10

Now some would consider rnorm(N <- 10, mean = 5, sd = 2) poor programming, but it is valid and you need to be aware of the differences between = and <- for assignment.

== is always used for equality testing:

> set.seed(10)
> logi <- sample(c(TRUE, FALSE), 10, replace = TRUE)
> logi
 [1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
> logi == TRUE
 [1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
> seq.int(1, 10) == 5L
 [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE

Do be careful with == too however, as it really means exactly equal to and on a computer where floating point operations are involved you may not get the answer you were expecting. For example, from ?'==':

> x1 <- 0.5 - 0.3
> x2 <- 0.3 - 0.1
> x1 == x2                           # FALSE on most machines
[1] FALSE
> identical(all.equal(x1, x2), TRUE) # TRUE everywhere
[1] TRUE

where all.equal() tests for equality allowing for a little bit of fuzziness due to loss of precision/floating point operations.




回答2:


= is basically a synonym for assignment ( <- ), but most often used when passing values into functions.

== is a test for equality




回答3:


In the simplest of terms, take these two lines of code for example:

1) x = 10
2) x == 10

The first line (x = 10) means "I am commanding that x is equal to 10."

The second line (x == 10) means "I am asking the question, is x equal to 10?"

If you write "x == 10" first, it will give you an error message and tell you that x is not found.

If you write "x = 10," this will store x as 10.

After you have written "x = 10", then if you write "x == 10," it will respond "TRUE", as in "yes, x does equal 10, because you made x equal to 10." But if you write "x == 11" or "x == 12" or x == anything besides 10, then it will respond that "FALSE," as in "no, x does not equal 11 or 12 or anything besides 10, because you made x equal to 10."




回答4:


  • (=) is a Assignment operator while (==) is a Equal to operator.
  • (=) is used for assigning the values from right to left while (==) is used for showing equality between values.

Example:

$test = 1;
if($test=2){
echo "Hello";
}

if($test==2){
echo "world";
}
//The result is Hello because = is assigning the value to $test and the second condition is false because it check the equality of $test to the value 2.

I hope this will help.



来源:https://stackoverflow.com/questions/28176650/what-is-the-difference-between-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!