![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Not a user?
Join Date: Sep 2007
Posts: 272
Rep Power: 2
![]() |
Re: Hey Pf
or better yet
sum+=i; count++; |
|
|
|
|
|
#22 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,032
Rep Power: 5
![]() |
Re: Hey Pf
Quote:
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 |
|
|
|
|
|
|
#23 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#24 |
|
Not a user?
Join Date: Sep 2007
Posts: 272
Rep Power: 2
![]() |
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++; but if you do a preincrement cout<<++count; feel free to correct me if i'm wrong 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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |