Thread: while loop's "=" question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    while loop's "=" question

    Hello,

    I am going through my C book when I ran across something I don't understand about the while loop. The book uses the below option1 and says I can replace it with option2. It does work. I just am clueless on why.

    My 3 questions about option2 are:
    1) How does the "=" sign work in the condition spot?
    2) The "=" works but is it proper? I am use to the true/false conditions...
    3) This will probably be answered in question 1 but, how does the "\0" at the end of the string get copied?


    option1
    Code:
    while(str2[count2]) /* Copy up to null from str2 */
        str1[count1++] = str2[count2++];
    str1[count1] = '\0';    /* Make sure we add terminator  */
    option2
    Code:
    while((str1[count1++] = str2[count2++]));
    Thanks!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The code is interpreted something like this:
    Code:
    whiletop:
      tmp = (str1[count1] = str2[count2]);
      count1++;
      count2++;
      if (tmp)
        goto whiletop;
    Remember that the "value" of an assignment statement is the value assigned to the left-hand side. As long as that value is non-zero, it's considered true.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    What happened was than Denis Ritchie, who invented C, originally thought it would be a good idea to write C full of terse loops like this. It was pretty quickly realised that this is in fact a bad idea, and you need to lay out code to kmake it simple to read.

    So while(x = y) sets x to y, and then terminates if the result is zero. So on the last interation, x is zero, y is zero, and the loop body isn't executed.

    If we make it

    while(str[count1++] = str[count2++]]

    the same thing happens, but str[count1] is is to zero. Then count1 is incremented for one last time, so it indexes the value after the zero.

    But you don't want to write code like that if you can help it. Keep things as clear as possible.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Malcolm McLean View Post
    What happened was than Denis Ritchie, who invented C, originally thought it would be a good idea to write C full of terse loops like this.
    That terseness is sometimes elegant (...it seems to me...), though.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by manasij7479 View Post
    That terseness is sometimes elegant (...it seems to me...), though.
    Readability trumps elegance every time!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    one motivation was that Ritchie was using a 300baud teletype in the beginning, so keeping it terse mattered.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks! I am pretty sure I understand. Now I should be able to remember 'the "value" of an assignment statement is the value assigned to the left-hand side.' Kind of feel like I need a C puzzle book with explanations to help burn the rules and tips in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question: "do while" loop parsing twice?
    By Toscano in forum C Programming
    Replies: 2
    Last Post: 12-19-2011, 06:39 PM
  2. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  3. Problem with ">", "<" in a for loop
    By Isolda_ in forum C Programming
    Replies: 6
    Last Post: 09-11-2007, 01:56 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM