• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Flow Control: "continue"

 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does it gives such errors? Is it not possible to write some statements after continue statement? I agree, they will not be executed, but still...
Well, the for loop's iteration expression is always executed inspite of writing a continue statement, right? Why is it allowed for this statement and why not for the statements written after continue statement?

Thanks in adv.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unconditional abruption of flow:

If the continue is unconditional, the next line after that is meant
to be non reachable. Compiler finds that the next statement will never
be executed thus compiler error.

Otherwise:

if(condition) {
continue;
}
System.out.println("I am reachable, so no problem");

even if you write like:

if(true) {
continue;
}
System.out.println("I am reachable, still no problem");



BTW, refer to this
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can have some statements after continue but it will be an unreacheable code when 'Continue' is going to be called all the time.

Under what condition you want your code to reach there? If there is any, then add that to the loop somewehere so that continue wouldn't get called when that condition is met.

Here is your code with some condition that would make the control reach beyond continue, bypassing continue.


[ May 01, 2007: Message edited by: M Krishnan ]
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks for your kind replies!
Could you please tell me why iteration expression of for loop is executed after continue? Why isn't it "unreachable"?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Could you please tell me why iteration expression of for loop is executed after continue?



After continue??? Could you please give an example of so?
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into the code that I posted. If you comment in continue; and comment out continue foo; and run the code, you'll see some output like this:

Look the output...i is incremented.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pasha,

your question is not so clear to understand where your doubt is,
about continue- it skips rest of the statements existed after it and just continue next iteration of the for loop. I suggest you to understand the continue first

still I can give you a simple sample of code why the statements are not reachable. this is to understand how compiler looks the flow

see the foll code


if you compile above program it gives you the foll error




the reason is,
compiler doesn't know what value I am going to give as args[0]at runtime.
it thinks like
if I give 1 at runtime,
it is fine it satisfies if condition
assigns k by 1 and prints it.
if I give other than 1 as args[0] then
there is no value assigned to k still trying to print it.
in this case it throws the possible error

compiler predicts at compile time itself and intimates user about possibility of the error.

in the same way, if continue exists in the flow every time it goes to next iteration of the inner for loop once it expires it executes outer for loop.
it never reaches the statements existed after continue.

Thanks
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic