Thread: A question about '=' and '=='

  1. #1
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Arrow A question about '=' and '=='

    In C, the symbol '=' means 'assign'. but in the selection structure, the '==' means check whether the condition is true, eg.
    Code:
    if (i == 2)
    but if you write the statement in this way:
    Code:
    if (i = 2)
    the check statement still works sometimes, my question is that why the '=' symbol in the check statement works, did the equality returns some value? and how the '==' works? Thanks!

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Code:
    if (i = 2) // assigns the value 2 to i
    // so now you can look at it like
    if(i)
    // which is really the same as
    if(2) 
    // which is true of course
    Remember you can do stuff like
    Code:
    while (1) 
        // do stuff
    
    if (100) 
        // do stuff
    // etc etc
    I hope that makes sense. Remember 0 is false, nonzero is true. Goodluck
    Last edited by SourceCode; 06-26-2003 at 06:03 AM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    its still better to use == for safety =p

    if (i=2)
    this works because it it checking for integer value...
    but if u put a string value eg,
    if (i="as")
    there will be an error as it is checking for string comparison..
    so using == to compare will be much safer..

    thats what i think

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well, if you go:
    Code:
    if (i == 2)
      <code>
    else
       <code>
    it will check if i is 2 and then determine where to keep executing. But you are right. If you go
    Code:
    if (i = 2)
      <code>
    else
       <code>
    ... i is tested, but not the way you would expect. In that line of code, 2 is assigned to i and then i is tested. An assignment returns the value that is assigned. So you end up with this:
    Code:
    if (i) // where i = 2
      <code>
    else
       <code>
    Now, in C/C++, you can do that and it tests i for a non-zero value. That means zero = false, anything else = true. So if i = zero, which it isn't, then it skips the if code and goes on to the else

    == works by returning 1 or true (either way its not zero) if the two operands are equal.

    = works by assigning the right value into the left variable and returning the value of the left variable.

    A neat little thing you can do with = is this:
    Code:
    x = y = z = 3;
    It sets all 3 variables to 3. Why? = has right-left associability. First it will assign 3 to z. Then the value of that (3) is set to y, then x. Then the final value of that statement is 3 but your semicolon discards that value and execution keeps on going.

  5. #5
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    NightWalker

    if (i="as")
    there will be an error as it is checking for string comparison..
    so using == to compare will be much safer..
    I think this statement should be:
    Code:
    if (strcmp(i, "as") == 0)
    Actually I do not know whether you can do this comparison, I just follow the things I learned from my teacher.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You are correct, NightWalker.

  7. #7
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    Then the final value of that statement is 3 but your semicolon discards that value and execution keeps on going.
    I thinks I do not totally understand this sentence, can you explain it to me with an example? Thank you very much!

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well, what happens if you don't put the semicolon? You would have something like:

    x = y = z = 3

    You can stick that anywhere you want. Say you stick it in another if statement

    if (x = y = z = 3) <code>

    Now, the value 3, which is returned by the assignments is being used. If you place a semicolon, it marks the end of a statement and whatever value is left over, is discarded.

    x = y = z = 3;
    i++;

    ^ the value of the first statement, 3, is discarded and execution moves on to the next statement (i++;)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. Need an extra pair of eyes ....
    By Whoputthatthere in forum C Programming
    Replies: 5
    Last Post: 06-02-2006, 12:19 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Tic-Tac-Toe-HELP!!!!
    By Tesita in forum Game Programming
    Replies: 5
    Last Post: 12-19-2001, 11:01 PM