• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Question 38  RSS feed

 
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!
 
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...
 
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 ]
 
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.
 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!