aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ++ and -- Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "++ and --" Watch "++ and --" New topic
Author

++ and --

Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
A simple question for java gurus:
I would like to request some detailed explainaition on ++ and -- operator.

I know in the above case it will display 0

but it confuses me when used in for loop

1. In the above statement at what point, i will be incremented ?
2. If I want to see the value immediately after the change, then at what point should I execute the println method ?

Now if i++ in for loop is changed to ++i the output still remains the same.
So exactly at what point I can see the changed value ?

Thank You!
Albert Bustamante
Greenhorn

Joined: Nov 29, 2004
Posts: 7
'i' will be incremented after 'i<=2' has been run once. the initial println will be displayed until the i<=2; condition is met.

Im a java newb and this is mu first answer to anyones question ... so you experts feel free to chime in here...

hope this helps
-later
chorizo
Saheed Adepoju
Ranch Hand

Joined: Jun 23, 2004
Posts: 267
Originally posted by Jugal Hans:
A simple question for java gurus:
I would like to request some detailed explainaition on ++ and -- operator.

I know in the above case it will display 0

but it confuses me when used in for loop

1. In the above statement at what point, i will be incremented ?
2. If I want to see the value immediately after the change, then at what point should I execute the println method ?

Now if i++ in for loop is changed to ++i the output still remains the same.
So exactly at what point I can see the changed value ?

Thank You!

Hi
Well as i know pretty well, when ever you use ++ or -- in a for loop disregard the rules of post increment and pre-increment, it doesnt for FOR loops.
Well as for a detailed explanation:
e.g
class pre{
public static void main(String args[]){
int i =10;
System.out.println(i++);//gives 10 still cause u print then increment
System.out.println(i);//gives 11 cause present value now 11
System.out.println(--i);//gives 10 decrease then print
System.out.println(i--);//gives 10 still cause print then decrease but ..
System.out.println(i);//gives 9 cause present value is 9
System.out.println(i++);//gives 9 still but present value is 10 after print
}// end of main

}// end of pre

Hope this clears it a little! So get a compiler and try it out and hope you understand! If compiler says otherwise please reply to this posting.. However good luck!
Saheed.
SCJP 1.4
SCJD(preparing..)
Imagination is better than Knowledge..


Saheed Adepoju<br />SCJP 1.4<br />SCJD (B&S..In progress)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When you execute a "for" loop, you do things in a particular order. Here's the general syntax of a "for" loop:



When you first reach the loop, the init is executed. After that, the condition is checked. If the condition is true, the body is executed. When the body has completed, the update is executed. The condition is again checked and, if true, we continue the cycle.

The key is that the update is executed at the end of each body execution.

You can read the JLS, if you're interested: §14.13 The for Statement


SCJP Tipline, etc.
Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
Thanks for ur replies..
ok, so according to Corey McGlone, there is no way to find out the changed value immed after the compiler actually changes it, right ?

Then is it safe to assume that pre and post increment operator doesnt really matter in FOR loop ? This is what Saheed Adepoju the prevoius poster also thinks !!

Thanks
seemapanth Joshi
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
Try to look at this way, each part of the for statement is an individual statement itself, so they have no effect of pre and post because the get executed one by one. E.g.

for (int i=0; i<=2; i++ ) ; try to read as (only for understanding purposes :

for {

int i=0;

i <= 2;


System.println.out( "value of i " + i ); (Sorry did not include this earlier)

i++;

}


Also if you want to see the effect of pre and post in the for loop try the following :-


and then change i++ to ++i and see the effect.

sorry made an error while typing..

[ December 02, 2004: Message edited by: seemapanth Joshi ]

[ December 02, 2004: Message edited by: seemapanth Joshi ]

[ December 02, 2004: Message edited by: seemapanth Joshi ]
[ December 02, 2004: Message edited by: seemapanth Joshi ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11302
    
  16

for the code you have written, using a pre- or post-increment operator will indeed make no difference.

but if you do something wacky like

for (int x = 0, y= 0; x < 7; x += ++y)

vs.

for (int x = 0, y= 0; x < 7; x += y++)

it WILL make a diference.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Short of using a debugger (probably in Eclipse or BlueJ, rather than jdb) you could rewrite your code to get something nearly the same:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
Thank you all.....
Feels good
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++ and --