The moose likes Beginning Java and the fly likes statement not reached error in while() loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "statement not reached error in while() loop" Watch "statement not reached error in while() loop" New topic

statement not reached error in while() loop

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
This is a code snippet which Im worried about
Which will be the first line to cause an error in the following code?
1 class Char
2 {
3 public static void main(String arg[])
4 {
5 while(false)
6 {
7 System.out.println("Hello");
8 }
9 while(false)
10 {
11 }
12 do;
13 while(false);
14 do
15 {
16 ;
17 }
18 while(false);
19 }
20 }

answer is Line no. 5 statement not reached
1)why the compiler throws error why doesnt it skip the first while as it evaluates to false n move on to the next line?

2) Will line no 12 and 13 form a do while loop.if it does then the semicolon
after do doesnt give error why?

3)when line no 5 and 9 is replaced by while(true), line no 9 and 12 gives
unreachable statement error.Why?

Thanks in advance to all those who rack their head for a solution!1
regs -Rajesh Chandra
Thomas Westley

Joined: Sep 22, 2005
Posts: 4
If line 5 always evaluates to false the compiler decides that it is pointless having the System.out.println("Hello"); so throws a compiler error. Why would you want a while loop that is always false? Maybe I have misunderstood your question, I am new at this.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thomas is right. Since the compiler sees that the condition for the while statement on line 5 always evaluates to false, it will complate that the SOP at line 7 cannot be reached.

Lines 12 and 13 create a valid do..while loop. To see this, it might be helpful to format it a little differently:

Note that this loop does nothing. However, it is different from a while loop because the empty statement will be executed once before the condition is evaluated. Also, this is equivalent to the do...while loop you have on lines 14 through 18.

If you change the while loop on line 5 to "while(true)", then line 9 causes an error for the same reason that line 5 did originally.

I hope this helps.


Java API Documentation
The Java Tutorial
nik rb

Joined: Sep 25, 2005
Posts: 21
what happens with code like


//no statements


will there still be a code not rechable error?? if so why
and wat if we use the same thing in the "if context"
like will the following code also give the same error

int i;
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

if(false) is a special case in Java. It is used to emulate C/C++'s conditional compiling, where any code inside the if(false) block will not be compiled. The variable does not have to be a literal false, but it must evaluate to one at compile time -- thus, you can use a static final variable to control that.

As far as what happens with

if you tried it, youe would find that yes, it still does cause a code not reachable error. The open and close braces count as a statement. (They're a statement block, which is a kind of statement.) So even though it's an empty block, the block itself cannot be reached, so the compiler lets you know that.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
So if false in the while is the culprit then if i keep while statement at lines 5 and 9 true then line no 9 and 12 gives
unreachable statement error. Is it bcos of the false in the do while loop?
regs- rajesh
I agree. Here's the link:
subject: statement not reached error in while() loop
It's not a secret anymore!