Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query on for loop

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please look at the following code
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
how come the output is

Total 0
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant total < 30
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry ,yes the conditional expression is total < 30
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you actually RAN it the other way, the for loop would never get executed and the answer would be 0.
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks cindy,
it was so simple and i did not pay attention to that point it was really total > 30, that the for loop was never executed
but there is another piece of code creating problems

public class Q46 {
public static void main(String[]args) {
int x;
int y;

for (x = 0, y = 5 ; x < y ; x=x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
why this one turned to be an infinite one???
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. in the first example, it seems to me the it does not matter if we did a post or a pre ++,-- on i and j.
but it shd shdnt it?
if j=10 at start. then --j shd mean that j is decremented by 1 (leaving j=9) and the body is executed.
this gets to me all the time an my exam time is getting near!...
 
sai challa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by preeti dengri:
thanks cindy,
it was so simple and i did not pay attention to that point it was really total > 30, that the for loop was never executed
but there is another piece of code creating problems

public class Q46 {
public static void main(String[]args) {
int x;
int y;

for (x = 0, y = 5 ; x < y ; x=x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
why this one turned to be an infinite one???

I think in the statement x=x++ x is always assigned 0 always as it's a postincrement operator and hence x is always less than y.Therefore the loop is infinite.
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
acc to me , the x=0,y=5 part is executed only once at the beginning of the for loop then after testing the condition the increment part is performed which should change the value of x to 1 for the second looping???shouldn't it be that way .please correct me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic