• 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

Why does do while work like this?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i =1;
do while(i>1)
System.out.println(i);
while(i<1);

without any indentation and curely braces ? while just next to the do doesn't even cause a compile error anybody pl explain why?

Thnaks
-VK
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai
Ur code is interpreted in this way

Ur code

int i =1;
do while(i>1)
System.out.println("is it possible");
while(i>1);

interpreted like
do{ while (i>1)
System.out.println("is it possible");
}while(i>1);

similiarly u can code like this also
A)
int i =1;
do if(i>1)
System.out.println("is it possible");
while(i>1);
B)
int i =1;
do for(;i<1
System.out.println("is it possible");
while(i>1);

Regards
Mohan
 
Seema Ahuja
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry i hvn't understood the last example.

u r initializing and testing the for loop but no incre or dcremnt is done...will it iterate the next time?

Thnaks
-Vkk
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(;; is even a valid statement. this means that it is a infinite loop.

uzma
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I smiley is taking one ; so I am writing once again

for(;;; ) is a valid loop
uzma
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

do while(i>1)
System.out.println(i);
while(i<1);



first, indentation is irrelevant to the compiler. NO code needs it - it's only used for humans who have to read it.

now, if i were to format this how the compiler sees it, it would look like this:



in other words, you have a do-while loop with the SECOND while. what that do-while loop contains is another while loop.

now i start to skate on thin ice here, but i think it's because the language defines a do-while loop as the word "do", a block/statement, and a while condition.

after your word do, the compiler looks for a valid statement/block. it finds that in a "while" statement, that itself is followed by a statement or block. in this case, that is met by the println statement. we now have our do, our block, and then we find the while that closes the do-while.

i believe you are thinking that as soon as we have a do, we look for a while to close it, and that is incorrect.
[ February 01, 2006: Message edited by: fred rosenberger ]

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and one more thing. "V Kk" does not conform to our Naming Policy. please read it, and follow the link on that page to edit your display name.

Basically, we require a real sounding first and last name, although a first name initial is ok (but not preferred).

Thanks!!!
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

anybody knows if this kind of question (with so strange syntax) are included in the real exam?

TIA...

Nicol�s
 
Seema Ahuja
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred...that cleared my confusion bout the do while syntax. i was wondering how did the complier accept two while loops within the same block of code.

As far as naming policy goes....i guess...if it hadn't ben a valid name it shouldn't even hv let me creat an id with such a name. As long as it isn't abbusive or doens't hurt anybody....i think it should be fine.

and yes Nicholas...to answer your question they do ask such questions...with simple questions the exam would lose it charm wouln't it?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
V Kk,

You are entitled to think what you want about the naming policy. You can even discuss it here. However, that was not a "if you feel like it" request. The policy is what the policy is, and we like it.

So, either change your name, or your account will be locked. Everyone else has to follow the rules, and so do you.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
helo uzma

i think for(;; is not correct one ie for(; is infinite loop
 
reply
    Bookmark Topic Watch Topic
  • New Topic