Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Choose between x++ or x--?

Reply
Thread Tools

Choose between x++ or x--?

 
 
desktop
Guest
Posts: n/a
 
      10-03-2007
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?
 
Reply With Quote
 
 
 
 
Shadowman
Guest
Posts: n/a
 
      10-03-2007
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
 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      10-03-2007
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2007
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


 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      10-03-2007
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;

}


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2007
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


 
Reply With Quote
 
Shadowman
Guest
Posts: n/a
 
      10-03-2007
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
 
Reply With Quote
 
Phil Endecott
Guest
Posts: n/a
 
      10-03-2007
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;
}
}

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      10-03-2007
On Oct 4, 9:07 am, Phil Endecott <(E-Mail Removed)>
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);

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-04-2007
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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Choose between 2 different ISP David17 General Computer Support 1 09-16-2006 03:41 AM
please help me choose between dimage x21 and x20 Mike Henley Digital Photography 0 05-21-2004 07:50 PM
If you had to choose between these two cameras.... rev Digital Photography 1 02-05-2004 01:03 PM
If you had to choose between these two cameras.... rev Digital Photography 1 02-02-2004 05:31 AM
If you had to choose between these two cameras.... rev Digital Photography 0 02-02-2004 03:44 AM



Advertisments