Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 4th, 2007, 6:38 PM   #21
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: Hey Pf

or better yet
sum+=i;
count++;
Jabo is offline   Reply With Quote
Old Nov 4th, 2007, 7:20 PM   #22
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,032
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Hey Pf

Quote:
Originally Posted by The Dark
    sum = sum += i;
    count = count++;
The first line is adding i in twice.
The second line is adding 1 to count and then resetting it back to what it was.
You should just be using sum=sum+i and count++
Both of these will yield undefined behavior, as you are modifying the same variable multiple times between sequence points.

In the first case, you are doing two assignments. Since sum += i is shorthand for sum = sum + i, the expression sum = sum += i evaluates to sum = (sum = sum + i). In the second case, remember that the postincrement operator modifies the variable's value, as does the assignment. As the intention is obviously to simply increment the variable's value, using the postincrement alone is sufficient. Furthermore, as the result of the expression is not needed, it's best to use preincrement rather than postincrement:
sum += i;
++count;
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 4th, 2007, 7:21 PM   #23
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Hey Pf

@Jabo: better for typing? It emits the same code.

@lectric: preincrement on an int is neither better nor worse than post increment. Again, examine the emitted code. For overloaded increment operators, there may be a couple instructions worth of difference.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 4th, 2007, 10:17 PM   #24
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: Hey Pf

the difference between post and pre is, pre will increase the variable before using it and post will increment it afterwards. this is only relevant if the variable is being used in another function on the same line. if the increment is by itself, there is absolutely no difference.

say you use the variable in a cout statement, if you say
cout<< count++;
it will print the value of count before incrementing it
but if you do a preincrement
cout<<++count;
it will increment the variable before printing it

feel free to correct me if i'm wrong

Quote:
Originally Posted by DaWei View Post
@Jabo: better for typing? It emits the same code.
yes, better for typing, not necessarily better for learning, but since OP already included the statement in his post, I figured he at least knew about it but just didn't understand how to use it.
Jabo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello PF maligree Community Introductions 1 Oct 7th, 2007 10:01 PM
Hey all eyeball_kid Community Introductions 3 Oct 3rd, 2007 2:00 PM
Hey Yall Kelvoron Community Introductions 4 Aug 19th, 2007 1:07 PM
Hey Hey Want2learn Community Introductions 2 Jul 8th, 2007 2:26 PM
Hey benders0 Community Introductions 3 Jun 23rd, 2007 3:29 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC