By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

unreachable statment??

P: n/a
I am trying to understand why "continue" keyword is used for, therefore I
compiled this code but I got this error message.. why
====================
C:\Program Files\JCreator LE\MyProjects\meme\test.java:24: unreachable
statement
System.out.println(t);
^
1 error
=====================
public class test{
public static void main(String arg[])
{
for (int t=0;t<5;t++)
{
System.out.println(t);
continue;
System.out.println(t); // the problem with this statement (this
unrechable) why

}
}
}

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> for (int t=0;t<5;t++)
{
System.out.println(t);
continue;
System.out.println(t); // the problem with this statement (this
unrechable) why

}


'continue' means skip the rest of the code in the loop and go to the
next iteration, hence the second println(t) always gets skipped and is
deamed 'unreacheable'. If you change 'continue;' to 'if (true)
continue;' however, it will work (that is, you won't get an error).

SteveE

Jul 17 '05 #2

P: n/a
is skipping a part of code is ERROR??
why the error message.. what is the use of continue??
so much confusing!!!!!

"SteveE" <ea****@btinternet.com_> wrote in message
news:Qu*********************@news-text.cableinet.net...
for (int t=0;t<5;t++)
{
System.out.println(t);
continue;
System.out.println(t); // the problem with this statement (this
unrechable) why

}


'continue' means skip the rest of the code in the loop and go to the
next iteration, hence the second println(t) always gets skipped and is
deamed 'unreacheable'. If you change 'continue;' to 'if (true)
continue;' however, it will work (that is, you won't get an error).

SteveE

Jul 17 '05 #3

P: n/a
leza wrote:
is skipping a part of code is ERROR??
why the error message.. what is the use of continue??
so much confusing!!!!!


It's a 'clever' feature of the compiler. If it can determine for sure
that a piece of code can never be executed, it will flag it up as such.
You can fix the code if it's a bug, or remove it if it's redundant. It
can be quite handy in complicated code with lots of throws or try/catch
blocks, etc.
Think of continue as a 'goto' end of loop (or a label for nested loops).

Explanation and examples are here:
http://java.sun.com/docs/books/tutor....html#continue

SteveE

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.