aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 38 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question 38" Watch "Question 38" New topic
Author

Question 38

Ben Gao
Greenhorn

Joined: Feb 19, 2002
Posts: 8
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

Joined: Feb 19, 2002
Posts: 8
Sorry for posting on the wrong place!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That does not compile due to unreachable statement problems on lines 1 and 4...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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 ]

not so smart guy still curious to learn new stuff every now and then
Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
Valentin why is it unreacheable
[ February 27, 2002: Message edited by: Tosin Adedoyin ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jan 18, 2002
Posts: 178
Valentin can't we do something to fix those smilies inside code statements???


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question 38