Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do while and while

 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shivakanth Thyagarajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow!! Great explanations. Thanks so much guys.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic