• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Question on for loop  RSS feed

 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output of above code is infinite loop



Output of second code block is
0
1
2
3
4

Can someone please explain why the difference of output although they both seem to be doing same thing?

Thank you,
Veena
 
Rancher
Posts: 810
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena,

You have to look at what the "++" operator is doing, and when it takes place--the when is the key:

"++" is the auto increment, as a lot of people call it, but in reality it is the "post increment" feature of many languages.

The "++" operator happens only after the function is complete, so i=i++, goes to this i=i; i++. For the context of the loop i starts as 0 and each time through the loop it is assigned i, value of 0, each and every time through the loop. The "++" happens after the assignment happens, so you never see it take effect in the loop.

Les
 
Veena Pointi
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is explanation of OCA by Jeanne Boyarsky and Scott Selikoff for sequence of executiong of for statement

1.Initialization statement executes
2.If booleanExpression is true continue,else exit loop
3.Body executes
4.Execute updateStatements
5.Resturn to step 2

So you see i=0 happens only once.As per this explanation both code blocks above should output same thing that is print 1 2 3 4 . Why in case of infinite loop , i is being initialized to 0 everytime and why not in other case?

 
Les Morgan
Rancher
Posts: 810
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena,
Remember in my original reply I said it was "when" and not what is happening.  In Java and many other languages the increment operator "++" is used in a special way--if it is before the variable it is called a pre-incrment and if it is after the variable it is called a post-increment.  The problem you are having is not in understanding the loop construct, but the expression "i=i++". What you think is happening is the same as in "i=i+1", and that is NOT what is happening.

First the assignment operation is done: "i=i" so now we have i set to the value of i, no big deal, but there has not been an increment done at this time, Java has not even considered the increment yet, but the left side has evaluated already to i, that same value as when it started on that expression. Now after the left side has been evaluated the post-increment comes into play to evaluate the right side of the expression and it 1 is added to the value of 0 which is what i is in evaluating the right side of the expression, so the right side has been evaluated to 1.  All well and good, but that has been done AFTER the left side has been assigned the initial value of i, which was 0.  That is what is called POST-INCREMENT. The incrment happens after the use of the value of the variable.  

Not if you put the incrment operator "++" before the variable, that is called PRE-INCREMENT and this happens for "i=++i", i looks to be assigned, Java encounters the pre-increment and says--before the value can be assigned to the left the pre-increment has to be honored, so the evaluation becomes i=1+0 and is assigned the value of 1.

Since in the loop example you gave a post-increment is used, then the value of i is carried forward in the evaluation of the equation "i=i++", hence i is never incremented in the loop, and therefore the loop is infinite--never proceeding past a count of 0, but ever looking to become 5 or more to stop.

consider the following:



The example illustrates post and pre increment operation.  If you are going to use pre or post increment, then you need to understand "WHEN" the increment happens, because it can make a huge difference in evaluation.

Les

Veena Pointi wrote:Below is explanation of OCA by Jeanne Boyarsky and Scott Selikoff for sequence of executiong of for statement

1.Initialization statement executes
2.If booleanExpression is true continue,else exit loop
3.Body executes
4.Execute updateStatements
5.Resturn to step 2

So you see i=0 happens only once.As per this explanation both code blocks above should output same thing that is print 1 2 3 4 . Why in case of infinite loop , i is being initialized to 0 everytime and why not in other case?

 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:[code=java]for(i=0;i<5;i=i++){

System.out.println(i);

}
Veena

means you are not updating the variable i, instead you are assigning updated value of variable i, to itself so it is assigning not updating that's the reason loop is infinite because condition is always true if value is not updated
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i=i++  means i=i  and then i++ the same i++
 
sohail hussain
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

joseph hu wrote:i=i++  means i=i  and then i++ the same i++

it  is postfix operator so at first Will not update and same value is assigned to i
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!