File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sierra & Bates book do while question

 
Carol Bloch
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not figure out what I am missing here. this is question 8 at the end of chapter 4 in the SCJP study guide by Sierra & Bates

public class Test {
public static void main(Strinf []args){
int I=1;
do while (I < 1)
system.out.print("I is " + I);
while (I>1) ;
}
}

The quiz answer says that no output is produced, but doesn't a do while always execute at least once? The answer also states the while is nested in the do while, but it is not. I haven't been able to find any posts aying that the answer to this is wrong. If it doesn't print anything, can someone tell me why not?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about if you format the code like this ?
 
Carol Bloch
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code was formatted exactly as I typed it. My first thought was that it was a compile error, because I thought that the while had to be separated from the do, but that wasn't the answer they gave. Is it because the while is at the front of the code that it does not even execute once?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carol,

I just want to make sure that it's clear that Barry's response represents EXACTLY the same functionality as the code in the book! With Barry's added parentheses it should be a lot clearer how this code works, and the reason we didn't add the parentheses in the book is because we need to toughen you up for the real exam - where the evil test creators (who shall remain unnamed) will often use confusing, parentheses-sparse code in an attempt to trick you

hth,

Bert

p.s. If you study the posts in the results forum, you'll discover that almost everyone who does well on the exam writes a LOT of little Java programs! We totally recommend that when you have a question like this, you write yourself a little code - go wild and add a few println()s
[ November 21, 2005: Message edited by: Bert Bates ]
 
Carol Bloch
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks! I get it. Boy those test creators (they who shall not be named) are very evil.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic