• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question 38

 
Ben Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Question38 {
public static void main(String[] args){
while(false);//line 1
if(false);//line 2
do{}while(false);//line 3
for(;false ; ) ;//line 4
}
}

This code compiles and runs fine,but the answer is "Compilation error on lines 1 and 4.".
Is it correct?
Thank you!
[Code edited by Valentin]
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Ben Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting on the wrong place!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not compile due to unreachable statement problems on lines 1 and 4...
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Gao:
public class Question38 {
public static void main(String[] args){
while(false);//line 1
if(false);//line 2
do{}while(false);//line 3
for(;false ; ) ;//line 4
}
}

This code compiles and runs fine,but the answer is "Compilation error on lines 1 and 4.".
Is it correct?
Thank you!

Ben,
I tried to compile your code as it is & the result:
Question38.java:6: ')' expected
for(;false ; //line 4
^
2 errors
Once change the code flaged by compiler with
for (;false ; ) ; <- the correct way.
everything runs OK even nothing got printed.
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin why is it unreacheable
[ February 27, 2002: Message edited by: Tosin Adedoyin ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no loops are infinite (false condition !!!)
The semicolon ';' IS a statement and that semicolon is unreachable because you cannot give a false literal condition to a while and for loop.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin can't we do something to fix those smilies inside code statements???
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add more spaces... I'll edit the code...
or check the "Disable smilies in this post" checkbox
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic