Choose between x++ or x--?

Discussion in 'C++' started by desktop, Oct 3, 2007.

  1. desktop

    desktop Guest

    I have a function that increments x:

    void fun(int arg) {
    int x = 10;
    x++;


    }

    is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    Or do I have to make an if statement?
    desktop, Oct 3, 2007
    #1
    1. Advertising

  2. desktop

    Shadowman Guest

    desktop wrote:
    > I have a function that increments x:
    >
    > void fun(int arg) {
    > int x = 10;
    > x++;
    >
    >
    > }
    >
    > is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    > Or do I have to make an if statement?


    void fun(int arg) { int x = 10; x += arg; }

    fun(1); //increment
    fun(-1); //decrement

    or


    void fun(int arg) {
    int x = 10;
    switch(arg) {
    case 0:
    x++;
    break;
    case 1:
    x--;

    }
    }

    fun(0); //inc
    fun(1); //dec

    But what's wrong with an if?


    --
    SM
    rot13 for email
    Shadowman, Oct 3, 2007
    #2
    1. Advertising

  3. On 2007-10-03 16:53, desktop wrote:
    > I have a function that increments x:
    >
    > void fun(int arg) {
    > int x = 10;
    > x++;
    >
    >
    > }
    >
    > is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    > Or do I have to make an if statement?


    You mean if arg is less then 0 you want to decrement it instead?

    x < 0 ? --x : ++x;

    It is more or less the same thing as

    if (x < 0)
    --x;
    else
    ++x;

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 3, 2007
    #3
  4. Erik Wikström wrote:
    > On 2007-10-03 16:53, desktop wrote:
    >> I have a function that increments x:
    >>
    >> void fun(int arg) {
    >> int x = 10;
    >> x++;
    >>
    >>
    >> }
    >>
    >> is there someway to change 'x++' to 'x--' based on the argument
    >> 'arg'. Or do I have to make an if statement?

    >
    > You mean if arg is less then 0 you want to decrement it instead?
    >
    > x < 0 ? --x : ++x;


    arg < 0 ? --x : ++x;

    >
    > It is more or less the same thing as
    >
    > if (x < 0)


    if (arg < 0)

    > --x;
    > else
    > ++x;


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 3, 2007
    #4
  5. desktop

    desktop Guest

    Shadowman wrote:
    > desktop wrote:
    >> I have a function that increments x:
    >>
    >> void fun(int arg) {
    >> int x = 10;
    >> x++;
    >>
    >>
    >> }
    >>
    >> is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    >> Or do I have to make an if statement?

    >
    > void fun(int arg) { int x = 10; x += arg; }
    >
    > fun(1); //increment
    > fun(-1); //decrement
    >
    > or
    >
    >
    > void fun(int arg) {
    > int x = 10;
    > switch(arg) {
    > case 0:
    > x++;
    > break;
    > case 1:
    > x--;
    >
    > }
    > }
    >
    > fun(0); //inc
    > fun(1); //dec
    >
    > But what's wrong with an if?
    >
    >


    The problem is that I have that conditional in a loop that potentially
    gets executed many times. So it would be nice if it was possible to make
    the test once before the loop.

    if (arg == 1) {

    somevar = x++;
    } else {
    somevar = x--;

    }

    while (running) {

    somevar;

    }
    desktop, Oct 3, 2007
    #5
  6. desktop wrote:
    > Shadowman wrote:
    >> desktop wrote:
    >>> I have a function that increments x:
    >>>
    >>> void fun(int arg) {
    >>> int x = 10;
    >>> x++;
    >>>
    >>>
    >>> }
    >>>
    >>> is there someway to change 'x++' to 'x--' based on the argument
    >>> 'arg'. Or do I have to make an if statement?

    >>
    >> void fun(int arg) { int x = 10; x += arg; }
    >>
    >> fun(1); //increment
    >> fun(-1); //decrement
    >>
    >> or
    >>
    >>
    >> void fun(int arg) {
    >> int x = 10;
    >> switch(arg) {
    >> case 0:
    >> x++;
    >> break;
    >> case 1:
    >> x--;
    >>
    >> }
    >> }
    >>
    >> fun(0); //inc
    >> fun(1); //dec
    >>
    >> But what's wrong with an if?
    >>
    >>

    >
    > The problem is that I have that conditional in a loop that potentially
    > gets executed many times. So it would be nice if it was possible to
    > make the test once before the loop.
    >
    > if (arg == 1) {
    >
    > somevar = x++;
    > } else {
    > somevar = x--;
    >
    > }
    >
    > while (running) {
    >
    > somevar;
    >
    > }


    Are you optimizing prematurely? How much improvement do you really
    expect from eliminating a single 'if'?

    Now, to get dir of the 'if' entirely you _could_ (does not mean that
    you should) duplicate the 'while' and do

    if (arg == 1) {
    while (running) {
    ...
    ++x;
    ...
    }
    }
    else {
    while (running) {
    ...
    --x;
    ...
    }
    }

    Or even extract it into two separate functions. Once you do that,
    you can actually compare the performance with the code that has
    the 'if' in it. Unless the '...' in my example represent almost no
    code, the difference is going to be single percent points. In my
    book it's usually not worth the headache of maintaining duplicated
    code.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 3, 2007
    #6
  7. desktop

    Shadowman Guest

    desktop wrote:
    > Shadowman wrote:
    >> desktop wrote:
    >>> I have a function that increments x:
    >>>
    >>> void fun(int arg) {
    >>> int x = 10;
    >>> x++;
    >>>
    >>>
    >>> }
    >>>
    >>> is there someway to change 'x++' to 'x--' based on the argument
    >>> 'arg'. Or do I have to make an if statement?

    >>
    >> void fun(int arg) { int x = 10; x += arg; }
    >>
    >> fun(1); //increment
    >> fun(-1); //decrement
    >>
    >> or
    >>
    >>
    >> void fun(int arg) {
    >> int x = 10;
    >> switch(arg) {
    >> case 0:
    >> x++;
    >> break;
    >> case 1:
    >> x--;
    >>
    >> }
    >> }
    >>
    >> fun(0); //inc
    >> fun(1); //dec
    >>
    >> But what's wrong with an if?
    >>
    >>

    >
    > The problem is that I have that conditional in a loop that potentially
    > gets executed many times. So it would be nice if it was possible to make
    > the test once before the loop.
    >
    > if (arg == 1) {
    >
    > somevar = x++;
    > } else {
    > somevar = x--;
    >
    > }
    >
    > while (running) {
    >
    > somevar;
    >
    > }
    >
    >

    It's hard to believe that a single if statement is your program's
    bottleneck, but there are certainly ways around it, as Victor suggested.

    --
    SM
    rot13 for email
    Shadowman, Oct 3, 2007
    #7
  8. desktop wrote:
    > I have a function that increments x:
    >
    > void fun(int arg) {
    > int x = 10;
    > x++;
    >
    >
    > }
    >
    > is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    > Or do I have to make an if statement?


    and later wrote:
    > if (arg == 1) {
    >
    > somevar = x++;
    > } else {
    > somevar = x--;
    >
    > }
    >
    > while (running) {
    >
    > somevar;
    >
    > }



    Try something like this:

    void fun(int arg) {
    int x = 10;
    int inc = arg ? +1 : -1;

    while (running) {
    x += inc;
    }
    }
    Phil Endecott, Oct 3, 2007
    #8
  9. desktop

    Old Wolf Guest

    On Oct 4, 9:07 am, Phil Endecott <>
    wrote:
    > desktop wrote:
    >
    > > is there someway to change 'x++' to 'x--' based on the argument 'arg'.
    > > if (arg == 1) {

    >
    > Try something like this:
    >
    > void fun(int arg) {
    > int x = 10;
    > int inc = arg ? +1 : -1;
    >
    > while (running) {
    > x += inc;
    > }
    > }


    For bonus obfuscation points (and no ternary operator):
    x += 1 - 2 * (arg != 1);
    Old Wolf, Oct 3, 2007
    #9
  10. desktop

    Rolf Magnus Guest

    desktop wrote:

    > I have a function that increments x:
    >
    > void fun(int arg) {
    > int x = 10;
    > x++;
    >
    >
    > }
    >
    > is there someway to change 'x++' to 'x--' based on the argument 'arg'.


    Based on it how? If arg is always either -1 or +1, you could of course
    simply do:

    void fun(int arg)
    {
    int x = 10;
    x+=arg;
    }
    Rolf Magnus, Oct 4, 2007
    #10
  11. desktop

    James Kanze Guest

    On Oct 3, 11:22 pm, Old Wolf <> wrote:
    > On Oct 4, 9:07 am, Phil Endecott <>
    > wrote:
    >
    > > desktop wrote:


    > > > is there someway to change 'x++' to 'x--' based on the
    > > > argument 'arg'. if (arg == 1) {


    > > Try something like this:


    > > void fun(int arg) {
    > > int x = 10;
    > > int inc = arg ? +1 : -1;


    > > while (running) {
    > > x += inc;
    > > }
    > > }


    > For bonus obfuscation points (and no ternary operator):
    > x += 1 - 2 * (arg != 1);


    I think you meant: "inc = 1 - 2 * !arg ;" If arg is true, you
    add +1, and if arg is false, -1.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Oct 4, 2007
    #11
  12. On Oct 4, 1:17 am, Rolf Magnus <> wrote:
    > desktop wrote:
    > > I have a function that increments x:

    >
    > > void fun(int arg) {
    > > int x = 10;
    > > x++;

    >
    > > }

    >
    > > is there someway to change 'x++' to 'x--' based on the argument 'arg'.

    >
    > Based on it how? If arg is always either -1 or +1, you could of course
    > simply do:
    >
    > void fun(int arg)
    > {
    > int x = 10;
    > x+=arg;
    >
    > }



    what about a pointer to function (to the incrementor or decrementor
    function)

    see the ugly code below (coded on the fly and not tested)

    void inc(int &x);
    void dec(int &x);

    void lalala(int arg)
    {
    void (*func)(int &);
    if( arg == condition for increment ) {
    func = &inc;
    } else {
    func = &dec;
    }

    while( whatever ) {
    ...
    func(x);
    ...
    }
    }

    Diego
    HP
    Diego Martins, Oct 4, 2007
    #12
  13. Diego Martins wrote:
    > On Oct 4, 1:17 am, Rolf Magnus <> wrote:
    >> desktop wrote:
    >>> I have a function that increments x:

    >>
    >>> void fun(int arg) {
    >>> int x = 10;
    >>> x++;

    >>
    >>> }

    >>
    >>> is there someway to change 'x++' to 'x--' based on the argument
    >>> 'arg'.

    >>
    >> Based on it how? If arg is always either -1 or +1, you could of
    >> course simply do:
    >>
    >> void fun(int arg)
    >> {
    >> int x = 10;
    >> x+=arg;
    >>
    >> }

    >
    >
    > what about a pointer to function (to the incrementor or decrementor
    > function)
    >
    > see the ugly code below (coded on the fly and not tested)
    >
    > void inc(int &x);
    > void dec(int &x);
    >
    > void lalala(int arg)
    > {
    > void (*func)(int &);
    > if( arg == condition for increment ) {
    > func = &inc;
    > } else {
    > func = &dec;
    > }
    >
    > while( whatever ) {
    > ...
    > func(x);
    > ...
    > }
    > }


    Really? *That's* going to be /faster/ than a single if?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 4, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew FPGA
    Replies:
    19
    Views:
    869
    Jim Lewis
    Oct 11, 2006
  2. Replies:
    11
    Views:
    561
  3. baibaichen

    choose between script and c++

    baibaichen, Nov 30, 2005, in forum: C++
    Replies:
    1
    Views:
    247
    mlimber
    Nov 30, 2005
  4. Anthony Irwin

    Trying to choose between python and java

    Anthony Irwin, May 15, 2007, in forum: Python
    Replies:
    34
    Views:
    818
    Bruno Desthuilliers
    Jul 16, 2007
  5. Hamilton, William

    RE: Trying to choose between python and java

    Hamilton, William, May 15, 2007, in forum: Python
    Replies:
    3
    Views:
    271
    Alex Martelli
    Jul 16, 2007
Loading...

Share This Page