• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested for loops

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooookkkkk.... now i have understood.... thanks a lot...
 
reply
    Bookmark Topic Watch Topic
  • New Topic