• Sammie (unregistered)

    LOL wtf ...

  • (cs)

    I would rather say that this means a lack of coffee, not a real wtf.


    Even if I didn't ask someone such a question yet, I guess everybody has those weird thoughts from time to time :D

  • (cs)

    This WTF originally racked up 5 pages of comments and alternate solutions!

    What more can be said than...

    int i;

    if(i < 11){
       i++;
       if(i < 11){
          i++;
          if(i < 11){
             i++;
             if(i < 11){
                i++;
                if(i < 11){
                   i++;
                   if(i < 11){
                      i++;
                      if(i < 11){
                         i++;
                         if(i < 11){
                            i++;
                            if(i < 11){
                               i++;
                               if(i < 11){
                                  i++;
                                  if(i < 11){
                                     i++;
                                  }
                               }
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }

     

    *crossing fingers as I click 'Post...'*

  • community (unregistered)

    the real wtf here is that the original code doesn't check for integer overflow

  • (cs)

    <FONT color=#000000>Wouldn't it have been obvious...</FONT>

    variable(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)(++)

    or to mix it up a little

    (++)(++)(++)(++)(++)variable(++)(++)(++)(++)(++)(++)

  • (cs)

    I would have told him the following:
    There is no way to increment by 11. The only way to do this is to use ++ 11 times.
    So if x = 5 and you need to increment by 11, you have to do the following:

    x++;
    x++;
    x++;
    x++;
    x++;
    x++;
    x++;
    x++;
    x++;
    x++;
    x++;

  • (cs)
    Alex Papadimoulis:

    Another slow day, another revisited post. Even if you've seen the original, I highly recommend checking out the comments posted. There you will find a number of solutions (five pages worth) to the problem that Steve Local's ... less gifted ... colleague was having in C# ...

    Nathan: Steve, you know how ++ will increment, right?
    Steve: Right ....
    Nathan: Okay, so how do you increment by 11?



    You use +=.

    This is not a WTF.  It might even be the opposite.  Nathan may never have seen += for whatever reason, or it could be a brief zone-out.

    Sincerely,

    Gene Wirchenko

  • mike (unregistered) in reply to ferrengi

    what's wrong with x + 11;

  • take that spam (unregistered)


    int j = 0;
    while( j != 11 )  { j = rand()  % RAND_MAX;  }

    i = i + j + ( 0 + 1 + 0 - 1 + 0 + 1 + 0 - 1 + 0 ) * cos( 0 ) * (cos(i)*cos(i) + sin(i)*sin(i)) + (number_of_correct_proofs_for_P_equals_NP);




    And there you have it.

  • David (unregistered)

    Actionscript Version

    //Frame 1
    var i=0;
    gotoAndPlay(2);

    //frame 2
    i++;
    nextFrame();

    //frame 3
    if(i <11){
    prevFrame();
    }

  • Anonymous (unregistered) in reply to R.Flowers

    for (i = 0; i &lt; 11; i++) {
        i++;
    }

  • (cs)

    I think I saw something about recursion on the other thread...

            int i = 3;
            inc(ref i, 11);
            ...

        private void inc(ref int num, int amt)
        {
            if (amt == 0)
                return;
           
            amt--;
            num++;
            inc(ref num, amt);
        }

  • (cs) in reply to Charlie Marlow

    Heh... guess that should be "if (amt <= 0)" just to make sure that, while stupid, it won't dig its way to China on negative numbers.

  • Grant (unregistered)

    Too bad none of the suggested solutions are thread-safe. 

  • Chucara (unregistered) in reply to David

    All above solutions are error prone, anyone knows that the following method that will be included in the Java SDK come next major update looks like this:

    int addNumber(int variable, int numberToAdd) {
        int variable2 = variable;
        for(int i = 0; i < numberToAdd; i++) {
            variable2 += (numberToAdd- (numberToAdd--);
        }
        if (variable + numberToAdd == variable2) {
            return variable += numberToAdd;
        } else {
            return (variable < variable2) ? true : FileNotFound;
        }
    }

  • andy (unregistered)

    const int FORTYTWO = 11;

    i += FORTYTWO;

  • jayinbmore (unregistered)

    A Modern C++ version:

    // Abstract class for Incrementor interface
    template<class T>
    class IncrementorBase
    {
    public:
      IncrementorBase() {}
      virtual ~IncrementorBase() {}
      virtual  T operator+(const T& rhs) const = 0;
    };
    // General Purpose Integer Incrementor
    // Assumes T has an over-ride for operator+(int)
    template<class T, int Amount>
    class Incrementor : public IncrementorBase<T>
    {
    public:
     Incrementor()  {}
     virtual ~Incrementor() {}
     virtual  T operator+(const T& rhs) const { return T(rhs

    • Amount); } // it's safe to do this becuase addition is commutative
      };
      typedef Incrementor<int, 11> IntElevenIncrementor;

    int IncrementByEleven(int i)
    {
     IntElevenIncrementor i11;
     return incr+i;
    }



  • Andy (unregistered)

    Here's a nice convoluted Perl version, which could be made more convoluted if it was all in-line.

    ${(++$i)}++;
    ${(++$i)}++;
    ${(++$i)}++;
    ${(++$i)}++;
    ${(++$i)}++;
    $i++;

    The taking a reference ( ( ... ) ) and then dereferencing it ( ${ ... } ) gets around the fact that, in perl, the pre- and post- decrement operators don't return lvalues, thus causing ++$i++ to generate a parse error, and (++$i)++ to bomb with a seemingly custom error message related to exactly this construct.

    Can't modify postincrement (++) in preincrement (++)


  • (cs) in reply to mike
    Anonymous:
    what's wrong with x + 11;


    x++ increments x. x + 11 does not affect the value of x.

  • Adam (unregistered) in reply to andy

    Anonymous:
    const int FORTYTWO = 11;

    i += FORTYTWO;

     

    I love it!

  • (cs)

    i = i + ((i << 3) + (i << 2) - i) / i;

  • (cs) in reply to Adam
    Anonymous:

    Anonymous:
    const int FORTYTWO = 11;
    i += FORTYTWO;

    I love it!


    That's right... can't have any of those pesky "magic numbers" floating around in the code.
  • Calophi (unregistered) in reply to RogerC
    RogerC:
    Anonymous:
    what's wrong with x + 11;


    x++ increments x. x + 11 does not affect the value of x.



    ...it does if you make it this:

    x = x+11;

    So really, even not knowing about a += isn't a valid excuse.
  • (cs) in reply to Calophi

    if(x==5) {
      x *= 2;
      x++;
    }
    else {
      raise IllegalValueException("aaaarrrgh!\n")
    }

  • asdyt (unregistered) in reply to Calophi

    Much simpler in Python.

    i += int(str(ord("\x01"))*2)

  • (cs)

    Just because this comment deserves it.... 



        i++;i--;i++;i--;i++;
    i++; i++;
    i++; i--; i--; i++;
    i++; i--; i--; i++;
    i++; i++;
    i++; i--; i++;
    i++; i++;
    i++; i--; i--; i++;
    i++; i--;i--; i++;
    i++; i++;
    i++;i--;i++;i--;i++;
    														</span></div></BLOCKQUOTE><br>
    
  • EV (unregistered)
    I always thought it was something like this (excuse me my formatting, I really don't know how to use this forum ;)):

    Private
    Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long,
    ByVal dy As Long, ByVal cButtons As Long,
    ByVal dwExtraInfo As Long)
    Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
    Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
    Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Private Const MOUSEEVENTF_MIDDLEUP = &H40
    Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    Private Const MOUSEEVENTF_RIGHTUP = &H10

    Private Sub inc(iNumToInc11Times As Integer)
    SetCursorPos 32, Screen.Height / 15 - 10
    mouse_event MOUSEEVENTF_ABSOLUTE, 32, Screen.Height / 15 - 10, 1, 1
    mouse_event MOUSEEVENTF_LEFTDOWN, 32, Screen.Height / 15 - 10, 1, 1
    mouse_event MOUSEEVENTF_LEFTUP, 32, Screen.Height / 15 - 10, 1, 1
    SendKeys ("R")
    SendKeys ("calc")
    SendKeys (vbKeyReturn)
    SendKeys (iNumToInc11Times + "+11")
    SendKeys (vbKeyReturn)
    ' TODO: Add some keys to copy it to the clipboard
    inc = Clipboard.GetText()
    End Sub
  • Seriously_WTF (unregistered)

    How about:

    const long Eleven1th         = 11/1
    inline long incrementByEleven(long val) return val + Eleven1th

  • Kiss me, I'm Polish (unregistered) in reply to TankerJoe

    Why not use tools that you have for free.


    function AddElevenToX( int x ) {
      System.Clipboard.put( x );
      System.run("calc.exe");
      System.keyboard.press("V", "Ctrl");
      System.keyboard.press("+", none);
      System.keyboard.press("1", none);
      for ( i = 1; i <= 11; i++ ) {
        System.keyboard.press( "=", none);
      }
      System.keyboard.press("C", "Ctrl");
      System.Clipboard.get( x );
     return x;
    }
  • GuruBuckaroo (unregistered) in reply to jayinbmore
    Anonymous:
    A Modern C++ version:

    // Abstract class for Incrementor interface
    template<class t="">
    class IncrementorBase
    {
    public:
      IncrementorBase() {}
      virtual ~IncrementorBase() {}
      virtual  T operator+(const T& rhs) const = 0;
    };
    // General Purpose Integer Incrementor
    // Assumes T has an over-ride for operator+(int)
    template<class t="" int="" amount="">
    class Incrementor : public IncrementorBase<t>
    {
    public:
     Incrementor()  {}
     virtual ~Incrementor() {}
     virtual  T operator+(const T& rhs) const { return T(rhs + Amount); } // it's safe to do this becuase addition is commutative
    };
    typedef Incrementor<int, 11=""> IntElevenIncrementor;

    int IncrementByEleven(int i)
    {
     IntElevenIncrementor i11;
     return incr+i;
    }


    And this is exactly why I refuse to learn C++.
    </int,></t></class></class>
  • poss (unregistered)

    Unfortunately for Nathan, adding eleven is one of the most laborious tasks in programming:

    if (x == 1) { x = 12; }
    else if (x == 2) { x = 13; }
    else if (x == 3) { x = 14; }
    ...


  • Lobachevsky (unregistered)

    Tom Lehrer says that the important thing [in new math] is to understand what you're doing, rather than to get the right answer:

    #!/usr/bin/perl
    my $val = shift;
    my @digits = split(//,$val);
    $digits[$#digits]++;
    $digits[$#digits -1]++ if $val > 9;
    $pow10 = 0;
    $x = 0;
    for ($j = $#digits; $j >= 0; $j--) {
    $x += (10 ** $pow10) * $digits[$j];
    $pow10++;
    }
    print "$x\n";
  • (cs) in reply to Kiss me, I'm Polish

    Since we are being silly.

    i += 011 + 1 + 1;

     

  • (cs) in reply to Rick

    Why haven't anyone said the obvious:

    // inc i by 11
    (++(++(++(++(++(++(++(++(++(++(++i)))))))))));

  • (cs)

    No, it's something like this:

    i = ((i / 11) + 1) * 11 + (i % 11);

    When you do numbers larger than 1, you have to split up the plusses you see.

  • (cs) in reply to Omnifarious

    This is much faster in SQL, so he should use a database:

    declare @numTable  table ( input int identity, answer int )
    declare @i int
    set @i = 5000 --substitute the value of i you need to increment as a param in stored proc

    declare @counter int
    set @counter = 0

    insert into @numtable values ( @counter + 12 ) -- identity seed is 1, so we have to use 12

    set @counter = @counter + 1
    while ( select max(answer) from @numtable ) < ( @i + 11 )
        begin
            insert into @numtable values ( @counter + 12 )
            set @counter = @counter + 1
        end

    select answer from @numtable where input = @i




  • (cs) in reply to asdyt
    Anonymous:
    Much simpler in Python.

    i += int(str(ord("\x01"))*2)


    First I thought, this afro-american be trippin'. Then...


    >>> 'WTF'*2

    'WTFWTF'


    Brillant!

  • Cheem (unregistered) in reply to Joost_

    You clowns are all doing it wrong. Here's what you do:

    public int incrementBy11(int thisIsTheNumberThatYouWantToIncrementBy11)
    {
      for(int i = 0; i < 1; i++)
      {
    
       //Loop is unrolled for tEh 1337 f457
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;  
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;  
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
           thisIsTheNumberThatYouWantToIncrementBy11 =
               thisIsTheNumberThatYouWantToIncrementBy11 + 1;
      }
    }
    
    
  • (cs) in reply to Anonymous
    Anonymous:
    for (i = 0; i < 11; i++) {
        i++;
    }

    Nothing cracks me up more than seeing critics try to write "funny" code - only to have it backfire.

  • Mihai (unregistered)

    template <class T>
    T inc11( T x ) {
        return ++++++++++++++++++++++x;
    }

  • Mike (unregistered)

    I get this sorta stuff from my coworker all the time!

    <FONT color=#0000ff>coworker</FONT>: i have a question...
    <FONT color=#ff0000>me</FONT>: alright
    <FONT color=#0000ff>coworker</FONT>: i have two subtotal amounts... 31.90 and 49.90... when i add them up in my code, i get the total amount of 99.80... which is way wrong
    <FONT color=#0000ff>coworker</FONT>: my code is: this_subtotalprice = subtotal + subtotal
    <FONT color=#0000ff>coworker</FONT>: any ideas why it's giving me an extra 19?

    *sound of head banging on desk reverberate across world*

  • (cs)

    Was this a one-time question, or does he come back with another number?

    Nathan:  Okay Steve... I incremented 11 times.
    Steve: How bout that!
    Nathan: Yeah - Turns out, it was too much.
    Steve: Sorry to hear that.
    Nathan: So... you know how the -- will decrement right?

  • (cs) in reply to olddog
    olddog:
    Nathan: So... you know how the -- will decrement right?
    lol
  • (cs)

    I get to use templates! Nobody told me I could use templates!

    template <size_t incval, typename T>
    class _inc_class {
     public:
       static T &incby(T &v) {
          return ++_inc_class<incval - 1, T>::incby(v);
       }
    };
    

    <span "color: #000080">template <<span "color: #000080">typename T> <span "color: #000080">class _inc_class<0, T> { <span "color: #000080">public: static T &incby(T &v) { <span "color: #000080">return v; } };

    <span "color: #000080">template <size_t incval, <span "color: #000080">typename T> T &incby(T &v) { <span "color: #000080">return _inc_class<incval, T>::incby(v); }

    There, now you can increment by lots of different values! Just do incby<11>(i);

  • (cs) in reply to olddog

    Then Steve responds:

    Steve: Nathan...hang on a minute...
    Steve: { gets on the phone with "coworker" from the thread above }
    Steve: coworker...hey, it's Steve.
    Steve: Listen, Nathan's in my office... "N-A-T-H-A-N" yeah.
    Steve: He's got a project that the two of you should collaborate on.
    Steve: He'll be right up.

    -- sorry Mike.

  • (cs) in reply to olddog

    A few days later...

    Nathan: Hey Steve, thanks, that project really came out great.
    Steve: sniggers Yeah, great.
    Nathan: Do you mind if I just ask you one more question?
    Steve: Er, ok, go for it.
    Nathan: Well, I figured out how to get it back to 5 from 11, but I want to increment it by another half. How do I do that?
    Steve: <_<

  • (cs)

    <FONT face=Garamond>Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative:</FONT>

    <FONT face=Garamond color=#008000>double number_you_want_to_increment = 0;</FONT>

    <FONT face=Garamond color=#008000>double numero = 11;</FONT>

    <FONT face=Garamond color=#008000>double temporary_variable_to_hold_stuff = number_you_want_to_increment;</FONT>

    <FONT face=Garamond color=#008000>while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11)</FONT>

    <FONT face=Garamond color=#008000>{</FONT>

    <FONT color=#008000>   <FONT face=Garamond>numero = numero / 2 ;</FONT></FONT>

    <FONT color=#008000>   <FONT face=Garamond>number_you_want_to_increment += numero;</FONT></FONT>

    <FONT face=Garamond color=#008000>}</FONT>

    <FONT face=Garamond>I think this gets puuuuurty close to incrementing a number by 11. [6]</FONT>

    <FONT face=Garamond></FONT> 

  • (cs) in reply to eimaj2nz
    eimaj2nz:
    Anonymous:

    Anonymous:
    const int FORTYTWO = 11;
    i += FORTYTWO;

    I love it!


    That's right... can't have any of those pesky "magic numbers" floating around in the code.


    But Insidious and Elusive numbers are no problem at all!

    How aboot:

    x += 0xB

    x += 013

    x += 1110.toDecimal()
  • (cs) in reply to dhromed

    I'd like to see some code that almost adds 11.

    Preferrably by introducing floating point errors.

  • KewlKat (unregistered) in reply to BiggBru
    BiggBru:

    <FONT face=Garamond>Now, see, Nathan never said he wanted to increment by 11 QUICKLY. So, here is one alternative:</FONT>

    <FONT face=Garamond color=#008000>double number_you_want_to_increment = 0;</FONT>

    <FONT face=Garamond color=#008000>double numero = 11;</FONT>

    <FONT face=Garamond color=#008000>double temporary_variable_to_hold_stuff = number_you_want_to_increment;</FONT>

    <FONT face=Garamond color=#008000>while (numer_you_want_to_increment < temporary_variable_to_hold_stuff + 11)</FONT>

    <FONT face=Garamond color=#008000>{</FONT>

    <FONT color=#008000>   <FONT face=Garamond>numero = numero / 2 ;</FONT></FONT>

    <FONT color=#008000>   <FONT face=Garamond>number_you_want_to_increment += numero;</FONT></FONT>

    <FONT face=Garamond color=#008000>}</FONT>

    <FONT face=Garamond>I think this gets puuuuurty close to incrementing a number by 11. [6]</FONT>

    <FONT face=Garamond></FONT> 

    Heh, never quite gets there, does it?

Leave a comment on “If ++ Increments ... (++)”

Log In or post as a guest

Replying to comment #54970:

« Return to Article