my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Nested for loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested for loops" Watch "Nested for loops" New topic
Author

Nested for loops

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
Given:

outer:
for (int i=0; i<5; 1++) {
for (int j=0; j<5; j++) {
System.out.println("Hello");
continue outer;
} // end of inner loop
System.out.println("outer"); // Never prints
}
System.out.println("Good-Bye");

Running this code produces

Hello
Hello
Hello
Hello
Hello
Good-Bye

Can somebody please explain me this code....
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by sweety singh:
..
Can somebody please explain me this code....


Please post your code using Code Tags which always helps the people to get the code in a clean way.

A good indentation helps to understand the flow to a good extent in the beginning stage.



Well. You have two for loops one inside another. The outer for loop is labeled as 'outer'.

The outer for loop contains two segments. The first one is the inner for loop and the second is a println statement which prints "outer".

The inner for loop does have two statements, one prints "Hello" and the next is a "continue" statement. Soon after reaching this continue statement, the runtime environment proceeds its flow of execution to place where it is instructed to do so. Here it is asked to proceed with the 'outer' that's why the second statement of outer for loop does NOT get a chance to get executed at all.

Once both the for loops meets their execution condition being false ("i" and "j" reach 5), the println statement present next to the outer for loop gets executed and prints "Good-Bye".

Please read about 'break' and 'continue' statements here. You will get to know the tactics.

Does that help?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
hey thanks.. i understood very well... but one thing i dint get... why does it print hello only five time... the outer loop is also there.... every time the outer loop is incremented.. the inner loops runs... isn't it...???
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by sweety singh:
... why does it print hello only five time... the outer loop is also there.... every time the outer loop is incremented.. the inner loops runs... isn't it...???


Yes exactly. As i said and you see in the code, the outer for loop contains the inner for loop. So for every incremented execution of outer loop the inner loop is supposed to execute as the outer loop contains the inner loop as its segment.
Nikhil Reddy Lingala
Greenhorn

Joined: Dec 06, 2007
Posts: 15
The code was explained excellently by Raghavan..

why does it print hello only five time... the outer loop is also there.... every time the outer loop is incremented.. the inner loops runs... isn't it...???


It prints Hello only five times because...
The sequence of execution is like this...
Outer for loop(i=0)->Inner for loop(j=0)->print "hello"->continue outer->
Outer for loop(i=1)->Inner for loop(j=0)->print "hello"->continue outer->
Outer for loop(i=2)->Inner for loop(j=0)->print "hello"->continue outer->
Outer for loop(i=3)->Inner for loop(j=0)->print "hello"->continue outer->
Outer for loop(i=4)->Inner for loop(j=0)->print "hello"->continue outer->
this time entering outer for loop fails..!!
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
ooookkkkk.... now i have understood.... thanks a lot...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loops
 
Similar Threads
continue outer loop
For Stament with continue
Label (Continue)
unreachable code-K&B p343
labeled continue statement?