Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Pre and Post increment operators in C++ or Java

 
webmaster forum
Activity
0%
 
New Coder
Posts: 24
Topics: 21
July 24, 2009, 02:56:41 AM

int a=10;
cout<< ++a; // Pre increment      a becomes 11 after this statement   prints 11
cout<< a++, // Post increment     a becomes 12 after this statement   prints 11
There is many times a confusion between these two operators. So, let it be clear. Preincrement operator increment value by one and displays the incremented result(on cout ++a).  Postincrement operator increment value by one and displays the non-incremented result(on cout a++).
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
July 24, 2009, 03:07:53 AM

You are missing the bigger picture - it is a question of precidence.

Pre ++a (certainly in C, I assume the same in Java) has (amongst) the highest precidence, Post a++ has amonst the lowest precidence.

So, more generally,

int a=2;
int b=10;
int c = a + b++;
c=a + ++b;

The two values of c will be different

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
Regular Coder
Posts: 53
Topics: 2
August 03, 2009, 09:19:01 PM

What's more fun is putting this stuff into a chain and trying to figure out what it means.

#include<cstdio>

int main(){
  int a = 1, b = 2, c = 1, d = 0;
  (a+d-c)?a++:--a = (--a)?(c--)?--d:a++:b--;
  printf("%d\n",a);
}

At least I think that will compile...
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
August 04, 2009, 04:14:31 AM

lol, its not fun when they are asking at a job interview what the value of a certain variable is though! Wink

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
Regular Coder
Posts: 53
Topics: 2
August 04, 2009, 07:05:06 AM

We have different definitions of fun then. =P

I have a teacher that can absolutely flabbergast an entire class with obfuscation. Of course, he also is the original author of the Jargon file, so what would you expect?
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
August 04, 2009, 07:14:43 AM

lol, I assume you have seen this well known piece of C before?

Code:
#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
  :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
Regular Coder
Posts: 53
Topics: 2
August 04, 2009, 07:41:15 AM

Oh yeah, I love that song.
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
August 04, 2009, 07:46:45 AM

Oh yeah, I love that song.

 Cheers

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
New Coder
Posts: 20
Topics: 2
April 13, 2011, 02:03:47 PM

I have a teacher that can perfectly bowl over an entire class with obfuscation.  Of course, he also is the original author of the Jargon file.

cabinet doors | games | de-duplication software
 
webmaster forum
Activity
0%
 
New Coder
Posts: 25
Topics: 1
WWW
April 20, 2011, 08:57:03 PM

Pre increment means incrementing value the using it. Post incrementing means using value first then increasing it

Nursing Assistant | Nursing Assistant Job | Nursing Assistant Salary
 
webmaster forum
mak14317  Offline
Activity
0%
 
Skilled Coder
Real name: Janne
Location: california
Gender: Male
Age: 26
Posts: 108
Topics: 1
WWW
May 02, 2011, 03:02:40 AM

int a=5,i;

i=++a + ++a + a++;
i=a++ + ++a + ++a;

    a=++a + ++a + a++;
 answer is 20 in both cases...........

herbal vaporizer
Happy vappy vaporizer
Eclipse Vape
Ivape
 
webmaster forum
Activity
0%
 
Skilled Coder
Posts: 125
Topics: 0
May 16, 2011, 02:08:36 AM

Post Increment means the value is returned before the increment is made. Pre Increments means the value is returned after the increment is made,

Network Management Service
 
webmaster forum
john14317  Offline
Activity
0%
 
Skilled Coder
Posts: 149
Topics: 0
June 04, 2011, 02:03:49 AM

The pre-increment was specifically designed for "C" on the DEC PDP-11 (remember "C" has only fundamental types), because there was a specific hardware instruction that performed this operation directly on the mrmory location, without the overhead of a load,add,store sequence. The result was significantly faster.

There are still a few processors that can do this more efficiently (than the post increment of a fundamental type), but processors are often 1000's of times faster today and the situations are extremely rare. The only place I can (possibly) recomment the use of a post increment is:

payday loans
instant online cash advance
 
webmaster forum
Life Is Good!
Activity
0%
 
Professional Coder
Gender: Female
Posts: 242
Topics: 3
WWW
June 09, 2011, 06:24:36 AM

surely, everyone gets confused with the increments stuff here.. lol..

Affordable Custom Web Design Services
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 08, 2012, 07:00:14 AM