Choose between x++ or x--?

D

desktop

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?
 
S

Shadowman

desktop said:
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?
 
G

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?

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;
 
D

desktop

Shadowman said:
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;

}
 
V

Victor Bazarov

desktop said:
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
 
S

Shadowman

desktop said:
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.
 
P

Phil Endecott

desktop said:
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 said:
> 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;
}
}
 
O

Old Wolf

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);
 
R

Rolf Magnus

desktop said:
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;
}
 
J

James Kanze

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.
 
D

Diego Martins

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
 
V

Victor Bazarov

Diego said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top