• 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

Question 38

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for posting on the wrong place!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin why is it unreacheable
[ February 27, 2002: Message edited by: Tosin Adedoyin ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin can't we do something to fix those smilies inside code statements???
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic