• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Label (Continue)

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks

code:
---------------
public TestCont
{
public static void main(String args[])
{
outer:
for (int i=0; i<5; i++) {
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

My question is why it doesnot prints "Outer"

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you'll never reach the end of the inner loop.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The continue in the loop stops the execution of current iteration and goes to the next iteration.
When the iteration starts, it checks for the condition in the loop and if follows the execution of the loop if the condition is satisfied. If the condition is false, it won't enter into the loop and can't execute that statement.

Hope this is clear....
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,



Here it goes:
Inside inner loop:
prints Hello, continue outer-> go to outer loop, i(1)<5 = true
prints Hello, continue outer-> go to outer loop, i(2)<5 = true
prints Hello, continue outer-> go to outer loop, i(3)<5 = true
prints Hello, continue outer-> go to outer loop, i(4)<5 = true
prints Hello, continue outer-> go to outer loop, i(5)<5 = false (exit from outer loop)

Question: Is statement "System.out.println("outer");" encounter anytime?
Answer: Never!
But compiler can't complaint about unreachable code, because continue is
inside the inner loop. If you make that in the outer loop, it will complaint, because of the unreachability of line.


Regards,
cmbhatt
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you folks now it's clear to me.



 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic