• 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

statement not reached error in while() loop

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Layne
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens with code like

while(false){

//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

if(false){
int i;
i=1;
}
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic