   The difference between = and ==
  if (time_step=intercept_time)if (time_step==intercept_time)both compiled. But the one above have right effect for my game. What is the differnece?   
  The top one is assigning "intercept_time" to "time_step" whereas the bottom one is comparing "intercept_time" to "time_step".   ;  I'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.Example :a = 2;b = 3;if (a == b) { printf("a == b is TRUE\n");}if (a = b) { printf("a = b is TRUE\n");}if (a == b) { printf("a == b is TRUE\n");}This program will output :a = b is TRUEa == b is TRUE   ;  = is the assignment operator.  == is the equality operator.  (A = B) changes A's value to that of B and returns/evaluates to a reference to B.  (A == B) doesn't change A or B, but returns/evaluates to true if A's value is the same as B's, or false if it isn't.  If statements should almost always be using ==, not =; using == with an if statment tests for equality, while using = change's the left operand's value and then tests if that value is nonzero.  For clarity and to make tracking down bugs easier, if you find yourself in a situation where if(A = B) actually does what you want, you should probably move the assignment to it's own line above the if statment and then stick an explicit test of A's value in the it statement, such as (0 != A).   ;Quote:Original post by Anonymous PosterI'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.You sure of that ?I think thatif( x = expr() )evaluates the same asif( expr() )and x = expr();if( x )Am I wrong ? At least do I think it's like that in C++ (and probably C) I don't remember well about Java, It might always evaluate to true.   ;those 2 instructions are totally different:if (time_step=intercept_time)first it makes the assignment time_step=intercept_time.then, it evaluates to a sentence like thisif(time_step)where time_step has the value of intercept_timethen, time_step is turned into a boolean and the if is "executed" with this valuein the other sentence:if (time_step==intercept_time)first, it compares time_step with intercept_timeif they are equal, the if is "executed" with a true argumentif they are not equal, the if is "executed" with a false argumentSo... they are really different, pay close attention to this because it can make hard to find bugs in your code.Cheers,Sebastian Montabone   ;  In addition to what peple said, many FAQs advise you that in case of comparing a variable to another value, you doif( func() == var )if( 10 == var )rather thanif( var == func() )if( var == 10 )so as in case you just miss an equal sign, the compiler notice there is something very wrong.if( 10 = var ) // Compiler will go on strikeif( var = 10 ) // you will have a hard time finding such a bug;Quote:Original post by jantaQuote:Original post by Anonymous PosterI'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.You sure of that ?Janta is correct, the = operator does NOT always evaluate to TRUE.  It evaluates to TRUE iff the value that's being assigned is non-zero. so...(x = 100) -> TRUE(x = 0) -> FALSEThat's how you can do statements like:if ( MyObject *foo = findObjectInDatabase() ){    //this will execute iff findObjectInDatabase returns a non-NULL value}else{    //this will execute if findObjectInDatabase returns NULL}-me;  Also, in Java, = does not evaluate to anything, and so trying if( foo = bar ) will get you a nice compiler error.   ;Quote:Original post by jantaQuote:Original post by Anonymous PosterI'm assuming you're using C, C++, Java, or a syntaxic similar language. Then the = is for assignations, and the == tests for equality. The = always evaluates to TRUE, whereas == evaluates to TRUE if and only if the left and right parts of the expression are equal.You sure of that ?I think thatif( x = expr() )evaluates the same asif( expr() )and x = expr();if( x )Am I wrong ? At least do I think it's like that in C++ (and probably C) I don't remember well about Java, It might always evaluate to true.You're both wrong, but the AP is totally wrong.a=b evaluates to a after the assignmenta==b evaluates to the result of the operator== with a and b as the arguments.AlsoT a = b;if( b ){}Is not necessarily the same asif(a = b){}Consider the following:#include <iostream>// _index of -1 means uninitialized// when converted to bool the object will evaluate to true// if the index is initialized and false if it isn't.class array_index{    int _index;public:    array_index() : _index(-1) {}    array_index( int raw_index ) : _index(raw_index){}    array_index& operator=(int rhs)    {   _index = rhs;   return (*this);    }    operator bool()const    {   return (_index != -1);    }};int main(){    array_index x;    if( x = 0 )    {   std::cout << "x = 0 is true" << std::endl;    }    else    {   std::cout << "x = 0 is not true" << std::endl;    }    if( 0 )    {   std::cout << "0 is true" << std::endl;    }    else    {   std::cout << "0 is not true" << std::endl;    }    return 0;}Janta: What if MyObject isn't an integer type? "if ( obj )" tries to implicitly convert obj to a boolean. [integer = signed char, char, unsigned char, signed short int, unsigned short int, signed int, unsigned int, unsigned long int or signed long int]If an obj is of type bool no conversion will go on.If obj is of type integer, then obj will be converted to false iff obj is zero, else it will be converted to true.If obj is of type float or double a compile time error will occur.If obj is a non-primitive type it will try to make the shortest implcit conversion from the type to bool, which could be T -> bool if an operator bool is present, T -> integer -> bool if an operator int is present and an operator bool isn't, etc.   