Thread: C 'goes to' operator. ;)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    82

    C 'goes to' operator. ;)

    What is the name of this operator: "-->"? - Stack Overflow

    http://groups.google.com/group/comp....0d58dd20?pli=1

    This is pretty nice, I've never seen this one before, definitely works as a subtle joke, lol.
    I feel like the FOR statement is convoluted with too many terms after being introduced to this method...
    On a more serious note, how do you feel about using WHILE in this way over FOR? It feels wrong, but looks nice.

    loll! you guys are really going to enjoy the comments on this reddit article, best c programming jokes I've ever read;
    http://www.reddit.com/r/programming/...e_littleknown/

    This is fantastic right here, :]

    Code:
    #include <stdio.h>
    
    #define BEGIN       int main() {
    #define CREATE      int
    #define EQUALING    =
    #define OK          ;
    #define DECREMENT   --
    #define IF          if (
    #define IS          ==
    #define THEN        ) {
    #define STARTPRINT  printf(
    #define ENDPRINT    )
    #define ELSE        } else {
    #define ENDIF       }
    #define END         return 0; }
    
    BEGIN
        CREATE a EQUALING 2 OK
        CREATE b EQUALING 3 OK
        DECREMENT b OK
    
        IF a IS b THEN
            STARTPRINT "I like apples." ENDPRINT OK
        ELSE
            STARTPRINT "I like oranges." ENDPRINT OK
        ENDIF
    END
    Last edited by since; 12-30-2009 at 12:56 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    That's hilarious!

    I try to avoid using while. In almost any while loop, there is some kind of initialization step, some kind of advancement step, and of course, the condition. It's a rare case where these cannot all be cleanly combined into a for loop.

    Normally, I only resort to a while loop if the equivalent for loop would have required a continue or break statement, both of which I also try to avoid. And there are cases where you really do need to do the advancement somewhere in the middle of the loop, but I try extremely hard to rework the loop to not require that, before choosing to use while.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    brewbuck,

    for (int x=10; x --> 0; ) { }

    That's it, done deal. :]

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by since View Post
    Code:
    for (int x=10; x --> 0; ) { }
    You introduce illegibility and the awkwardness of having to deal with a decremented x inside the for loop. I pass.

    while loops have their uses if you don't have direct, easy, or inexpensive access to the loop boundaries. Database access is one area where you will find yourself using while a lot more and where for is either expensive or entirely unavailable. Eventually you can always find a for replacement, but I don't see any reason why since "while" reads better anyway, given the semantics of the operation being performed.
    Last edited by Mario F.; 12-30-2009 at 02:49 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Digraphs and trigraphs have always seemed like strange operators to me . . . in fact, when I saw your post my first instinct was to see if "-->" was a trigraph. (It isn't, of course, because trigraphs start with ??.)
    Code:
    int x <:8:>=<%8%>;/??/
    **/
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    You introduce illegibility and the awkwardness of having to deal with a decremented x inside the for loop. I pass.
    Illegibility is a little bit reaching, I can understand the statement just fine, I guess you can't read it. 'awkwardness of having to deal with a decremented x inside the for loop' ... what?! There's plenty of cases where decrementing a variable makes sense, why would that be a valid criticism of that style?

    I just like the syntax that reads 'For x is 10 to 0' instead of 'For x is 10, x is greater than 0, decrement x.' In my mind I read it as a drastically simplified statement, which is neat... though I probably won't really use it in practice.

    edit: spoke about incrementing 'x' when I meant to speak about decrementing 'x'.
    Last edited by since; 12-30-2009 at 07:20 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by since View Post
    I just like the syntax that reads 'For x is 0 to 10' instead of 'For x is 0, x is less than 10, increment x.' In my mind I read it as a drastically simplified statement, which is neat... though I probably won't really use it in practice.
    You say that as if to suggest that
    Code:
    for (int x=10; x --> 0; ) { }
    and
    Code:
    for (int x=10; x > 0; x--) { }
    would produce identical results.
    Last edited by SlyMaelstrom; 12-30-2009 at 06:07 PM. Reason: Nagging double space typo bothering me.
    Sent from my iPadŽ

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Heh, good catch. But if this is counting upwards over the range [0, n),
    Code:
    for(int i = 0; i < n; i ++) {}
    then this is counting downwards over the range [0, n).
    Code:
    for(int i = 10; i --> 0; ) {}
    It has a nice symmetry, don't you think?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by dwks View Post
    Heh, good catch. But if this is counting upwards over the range [0, n),
    Code:
    for(int i = 0; i < n; i ++) {}
    then this is counting downwards over the range [0, n).
    Code:
    for(int i = 10; i --> 0; ) {}
    It has a nice symmetry, don't you think?
    If this was art class, I'd give you an A.

    If this was the IOCCC, I'd give you a D.

    ... but for the purposes of clean programming, I think I'd take syntactical consistency over syntactical symmetry any day.
    Sent from my iPadŽ

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, certainly. I wouldn't use that in code (well, unless it was for an art class!).

    I should mention that I like for and while loops equally, and I don't even mind using do-while loops. break and continue I use if I must. But never, ever goto.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Eventually you can always find a for replacement, but I don't see any reason why since "while" reads better anyway, given the semantics of the operation being performed.
    Well, the point is to find something that's more expressive and readable. If translating the loop from while to for makes it look terrible, I wouldn't do it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by since View Post
    Illegibility is a little bit reaching, I can understand the statement just fine, I guess you can't read it.
    I can. It seems you were the one who actually didn't read it properly and understood the consequences.

    I just like the syntax that reads 'For x is 0 to 10' instead of 'For x is 0, x is less than 10, increment x.' In my mind I read it as a drastically simplified statement, which is neat... though I probably won't really use it in practice.
    It's not a simplification. It's an alteration. I thought I had made that clear.

    Edit: and btw, I'm confused as to how that is any simpler than while (x-- > 0) {}.
    Last edited by Mario F.; 12-30-2009 at 06:32 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    I just learned the IOCCC has an award for the most abused use of the C programming language. Awesome. :]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM