• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Loop Use

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing to do with for loop. question to you : what is the difference between i = i++ and i = ++i?
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 11
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course, you could also just do:



and we have our own FAQ on this topic here
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaks guys...
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Seetharam misunderstood because of your way of posting question,

not really. i was not even consider break; ;)
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:Saloni it is Seetharaman and not Seetharam.

Nice.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@saloni...Thanks for your advice I will keep that in mind from now...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic