[Bluej-discuss] For Loop doubt:
Davin McCall
davmac at bluej.org
Wed Mar 8 01:42:39 GMT 2006
Hi Namit,
The difference is demonstrated by the following snippet of code:
int j = 0;
System.out.println("j = " + j);
System.out.println("j = " + ++j); // pre-increment
System.out.println("j = " + j++); // post-increment
System.out.println("j = " + j);
The post-increment is the most interesting. You will see that it prints
the value of j as being 1, but the final statement prints a value of 2.
The key is that j has had one added, but the expression using the
post-increment still uses the original value of j (the value before the
increment). For the pre-increment, j is incremented and the incremented
value is used in the expression.
Davin
Namit TClaw wrote:
>
> I am a student of BlueJ and had a doubt.
>
>
>
> Is there any difference between :
>
> for(int j=1;j<=5;j++)
>
> System.out.println(j);
>
>
>
> and
>
>
>
> for(int j=1;j<=5;++j)
>
> System.out.println(j);
>
>
>
> The output is the same, but I wanted to know if any internal operation
> is different.
>
>
>
>
>
> Regards,
>
> Namit.
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060308/a6a6fb06/attachment.html
More information about the bluej-discuss
mailing list