This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop" Watch "for loop" New topic
Author

for loop

srilavanyaa subramanian
Greenhorn

Joined: May 30, 2006
Posts: 26
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

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

the magical word is indentation of your




OK?

Bu.


all events occur in real time
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To understand such loops insert System.out.println statements at interesting places in your program. Print out the variables at those stages.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: for loop