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

Problem with nested loop

 
Clay Bartlett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int i=0;i<=3;i++)
for(int i=1;i<=4;i++)
{
System.out.println(i);
}

I must be blind but I dont see the problem with this
 
Logan Owen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) you are missing the closing bracket
2) you need a diff iterator (not i) for inner loop, because you are setting i back to 1 every time, which will never let the outer loop get to 3, so the loop will go on infinitly.

Possible code:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that this doesn't even compile, am I correct? The reason is that the computer thinks you are declaring the same variable (i) twice, as Logain said. So you need to change the variable used in the inner loop. Typically j is used in such situations.

HTH

Layne
 
Clay Bartlett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks this might be the answer I'll post something later to let you know whether it solves the problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic