473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Choose between x++ or x--?

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?
Oct 3 '07 #1
12 1244
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
Oct 3 '07 #2
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
Oct 3 '07 #3
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
Oct 3 '07 #4
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;

}
Oct 3 '07 #5
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
Oct 3 '07 #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;

}

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
Oct 3 '07 #7
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;
}
}

Oct 3 '07 #8
On Oct 4, 9:07 am, Phil Endecott <spam_from_usenet_0...@chezphil.org>
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);

Oct 3 '07 #9
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;
}

Oct 4 '07 #10
On Oct 3, 11:22 pm, Old Wolf <oldw...@inspire.net.nzwrote:
On Oct 4, 9:07 am, Phil Endecott <spam_from_usenet_0...@chezphil.org>
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:ja*********@gmail.com
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
Oct 4 '07 #11
On Oct 4, 1:17 am, Rolf Magnus <ramag...@t-online.dewrote:
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

Oct 4 '07 #12
Diego Martins wrote:
On Oct 4, 1:17 am, Rolf Magnus <ramag...@t-online.dewrote:
>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
Oct 4 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DaBoo | last post by:
Hi! I'm creating dynamic a XML file. in this file are store some informations about images i use in HTML datasheets. i'm going to transform the XML file with XSLT. My Problem is, that {image}...
2
by: Ruthless | last post by:
Hello. I've got a question about xsl:choose I've got an element <person> with the sequence of <first_name><last_name><date_bith> and <info> and the info is not obligatory(minOccurs="0") ...
14
by: Howard Kaikow | last post by:
Can I use a bookmark to selectively choose the code to be run? For example. Suppose we have the following in an HTML file: <html>
6
by: Penny | last post by:
Hi all, I've built a simple search <Form> on a web page that is intended to allow the user to search a record store database. There is a drop down box where the user can choose either 'Artist'...
3
by: Anders Borum | last post by:
Hello! I've come across a strange error that occurs, when you try to return a nodelist from a variable with a choose/where/otherwise statement. I'm not quite sure whether it's a bug or simply...
3
by: giloosh99 | last post by:
Hello I have a report that needs to printed by a specific printer rather than the computers default printer? Is there a way to dynamically choose a specific printer to print a report through VBA...
12
by: Steve | last post by:
Can the Choose function be used to set the criteria for a field in a query to either "Is Null" or "Is Not Null" based on the value of an option Group on a form? Such as:...
14
ADezii
by: ADezii | last post by:
The tendency of VBA code to evaluate all expressions, whether or not they need to be evaluated from a logical standpoint, makes the use of the IIf(), Switch(), and Choose() Functions inefficient and...
5
by: lsllcm | last post by:
Hi All, I have one question about many "or" operation make system choose incorrect index There is one table TT ( C1 VARCHAR(15) NOT NULL, C2 VARCHAR(15) NOT NULL, C3 VARCHAR(15) NOT NULL,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.