File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes do while and while 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 "do while and while" Watch "do while and while" New topic
Author

do while and while

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Again from k&b, this example has me confused:



k&b says that in the above code it is two independent loops. one is do while and the other is while.

but, when i delete the last line "while(i>1)" and compile, my compiler doesn't like it. how does this work?

also, i thought, for do...while the statements if any, would be inbetween do and while. ex:

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
It means that,
look below at the code

Now if u remove the last while, which means ur do-while doesnt terminate properly.

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
You expect lot of question like this in certification Exam. If curly braces are not in while loop it executes only next line of the while loop.
for example

if curly braces are not placed in above code it work like above.
[ April 06, 2005: Message edited by: Raghu Shree ]

Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Shivakanth Thyagarajan
Ranch Hand

Joined: Mar 28, 2005
Posts: 41
Hi Animesh,
public static void main( String args[] ) {
int i = 1 ;
do
while( i < 15 )
System.out.println( "Ok" ) ;
while( i < 1 );
}
}

No if the above code is modified in this manner

public static void main( String args[] ) {
int i = 1 ;
do
while( i < 15 ) ;
System.out.println( "Ok" ) ;
//while( i < 1 );
}
}

It's throwing up an error, "while expected",

Does this convey to us that b/w do and while an statement should exist else compiler will throw up an error.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Yeah there has to be a loop body or statement in between Do-While.

The above code also works, even though there is no statement inside the loop body,

also, this works:

Here just a statement is described
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Wow!! Great explanations. Thanks so much guys.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi

do
while( i < 15 ) //always true
System.out.println( "Ok" ) ;
while( i < 1 );


isn't this result in infinite loop.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Indeed its an infinite loop and the jvm will go just running it forever.
Well, about its being unreachable there is a JLS topic u can go thru it.
unreachable
Well,
Its quite confusing and questions related to these topics usually dont come. Well i havent given the exam so i cant say u exactly whether such questions come or not.
So i suggest u can just skip ths topic.

U can go thru this thread for more info
unreachable
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do while and while