• Post Reply Bookmark Topic Watch Topic
  • New Topic

for loop  RSS feed

 
srilavanyaa subramanian
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could any one explain me the logic behind this code....i just want to know how the nested loop is wrking
public static void main(String args[])
{
int c = 0;
A: for(int i = 0; i < 2; i++)
{
B: for(int j = 0; j < 2; j++)
{
C: for(int k = 0; k < 3; k++)
{
c++;
if(k>j) break;
}
}
}
System.out.println(c);
}
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

the magical word is indentation of your




OK?

Bu.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand such loops insert System.out.println statements at interesting places in your program. Print out the variables at those stages.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!