| Author |
For Loop Use
|
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
Its(for loop) always confused me ,,Now i have a situation below....
Here it is going to infinite loop...
I dont find why...And more over if I change something in the loop for the first time the compiler is not recognizing it..So basically printing the old output only...Break statement only limits the output to one time print.
It works fine in case of i=++i...
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i?
|
 |
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
Seetharaman Venkatasamy wrote:nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i? 
i=i++ Post Increment.
i=0;
i=i++;
so first time int i=0.
sically System.out.println(i++); will give me the out put 0,where int i=0;
i=++i Pre Increment.
where in this case it will print 1.
Is my understanding is right...:O
|
 |
Amrish Jhaveri
Greenhorn
Joined: Dec 27, 2011
Posts: 11
|
|
@Aneek Banerjee:
As far as the output of the above code is concerned it would be:
start
Starting1...
end
Do take note that you are using a unlabeled break, so it will come out of(break out of) the IMMEDIATE for loop(after executing just once).
There might be some problem with your java installation, re-install Java Development Kit.
@Seetharaman Venkatasamy :The difference between i++ and ++i is that it is post increment and pre-increment operators.For the given code i=i++ or i=++i wouldn't make a difference,since it breaks out of the for loop without checking the iteration expression!!!
To make it clearer for you, take a example:
int i=1;
int b=i++; // Assigning current value of i to b and THEN incrementing the value of i by 1.
int a=++i; // First increment the value of i by 1 and THEN assign it to a.
|
OCJP 6 (91%)
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
Aneek Banerjee wrote:
Seetharaman Venkatasamy wrote:nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i? 
i=i++ Post Increment.
i=0;
i=i++;
so first time int i=0.
sically System.out.println(i++); will give me the out put 0,where int i=0;
i=++i Pre Increment.
where in this case it will print 1.
Is my understanding is right...:O
Yes you are right. a good way to remember about post/pre increment/decerement operators is to repeat their names in your mind. for e.g post means after, so post increment means that we have to do increment AFTERWARDS. i.e. first use it and THEN increment it. same analogy goes for pre increment/decrement operator
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
Amrish Jhaveri wrote:@Aneek Banerjee:
As far as the output of the above code is concerned it would be:
start
Starting1...
end
Do take note that you are using a unlabeled break, so it will come out of(break out of) the IMMEDIATE for loop(after executing just once).
There might be some problem with your java installation, re-install Java Development Kit.
@Seetharaman Venkatasamy :The difference between i++ and ++i is that it is post increment and pre-increment operators.For the given code i=i++ or i=++i wouldn't make a difference,since it breaks out of the for loop without checking the iteration expression!!!
To make it clearer for you, take a example:
int i=1;
int b=i++; // Assigning current value of i to b and THEN incrementing the value of i by 1.
int a=++i; // First increment the value of i by 1 and THEN assign it to a.
|
 |
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
|
@Amrish if I comment the break statement then its moving to print the output in an infinite loop...does it happening because i is incremented to 0 for the first time,(for i=0;i<10;i=i++)
|
 |
Amrish Jhaveri
Greenhorn
Joined: Dec 27, 2011
Posts: 11
|
|
@Aneek Banerjee:
I found the problem!! it is with i=i++!! You also found that it doesn't loop infinitely when you used i=++i.
I just tried this code to see what happens to the value of i:
The out was:
start
0
1
end
So as you can see when you do i=i++; it actually doesn't increment the value of i!!!(POST INCREMENT i.e first assign the current value to i, but NO INCREMENT is done)
But if you use i=++i i.e PRE INCREMENT , then it assigns the incremented value of 'current i' to i.
The reason for this is :
i++ means post increment. It means, the value will be incremented after the operation is performed on it. It doesnt mean, the statement will be completed before execution. That is the value of the variable 'i' will be stored in a temporary location, then the value is incremented and then, the actual operation is performed, in this case assignment, on the value in the temporary location.
Hence,
i = i++;
is equivalent to,
int temp = i; // temp = 0
i++; // i=1
i = temp; // i = 0
I found it on http://skeletoncoder.blogspot.in/2006/09/java-tutorials-i-i.html
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
of course, you could also just do:
and we have our own FAQ on this topic here
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
Thaks guys...
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
Seetharaman Venkatasamy wrote:nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i? 
[Aneek Banerjee] Seetharam misunderstood because of your way of posting question, you are posting a code in which for loop will execute only once and you're telling that output is infinite execution of for loop ??? Come on, it is not so tough to post at least correct output so rancher will think to help you.
|
Tell the difficulties that i am difficult.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
saloni jhanwar wrote:Seetharam misunderstood because of your way of posting question,
not really. i was not even consider break; ;)
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
saloni jhanwar wrote:
Seetharaman Venkatasamy wrote:nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i? 
[Aneek Banerjee] Seetharam misunderstood because of your way of posting question, you are posting a code in which for loop will execute only once and you're telling that output is infinite execution of for loop ??? Come on, it is not so tough to post at least correct output so rancher will think to help you.
Saloni it is Seetharaman and not Seetharam.
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
gurpeet singh wrote:Saloni it is Seetharaman and not Seetharam.
Nice.
|
 |
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 30
|
|
@saloni...Thanks for your advice I will keep that in mind from now...
|
 |
 |
|
|
subject: For Loop Use
|
|
|