• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

CHECK THIS

 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hie Friends, Here is something I think is interesting.Try to answer without compiling.
Do you think the following snippet will compile ? If it does, what is the output ?
-----------------------------------------------------------------
int i =1;
do while (i>1)
System.out.print(i);
while(false);
---------------------------------------------------------------
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll bite!
I interpret this as follows:
int i =1;
do{
while (i>1) System.out.print(i);
}while(false);
It should compile, go through the do once, not enter the inner while, and exit with no output. How'd I do?
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaNut, hang on and and let others play with it also.
Herbert.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think it will compile.
Reason: hanging while stmt.
Will stay tuned .
Regds.
- satya
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'll go with Java nut.
It will compile, but there won't be any output!!
Regards,
Suresh.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will compile, but will not produce any output. Though the control enters into the do loop, since i>1 is false the inner while loop will not get executed. The println is inside the inner while loop, so nothing gets printed. while(false) is always false, so the do loop ends!.
Am I close?
Ajith
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a tricky and good question. It will compile and run but there will be no output printed as the condition in while(i>1) is not satisfied. The outer loop is a do loop and hence the body is executed and then the condition of while(false) is checked. Here false is of boolean result kind and hence is valid. Since it is false, the loop ends. If instead we had true, then it would have been an indefinite loop.

Suma.
[This message has been edited by Suma Narayan (edited June 01, 2000).]
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a try......
To me the code looks like
int i = 1;
do {
while (i>1)
{ System.out.print(i); }
}
while(false);
The program will compile fine. Nothing will be printed on the screen.... Tricky one.... going to compile it right now....
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai friends,
The above program will compile and will not produce any result provided it is written inside a class with proper main signature
If we interpret it as follow then
public class T1
{
public static void main(String args[])
{
int i =1;
do
{
}
while (i>1)
{
System.out.print(i);
}
while(false);
}
}
it will not compile because of the missing ;
if we interpret it with ; then will work fine
if we interpret as follows
public class T1
{
public static void main(String args[])
{
int i =1;
do{
while (i>1)
{
System.out.print(i);
}
}
while(false);
}
}
then it will compile.But no results will be produced since the while loop is never executed.
I believe that the compiler will consider it as in the later code.If not please correct me.
thanks.
preeth
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
Code will compile and run fine , no error will be generated.
The thing is that whenever u dont use brackets then any java keyword like do , while and all others will effect only the very next line,
but here the intersting part is that everyother line is attached with the very next line, thats y it goes fine. (plz correct me if i m wrong)
( One more thing , i m new in this forum with the refernce of my friend who has just done Java certification, i m 24/male and i have worked on oracle but i m very much attracted towards java thats y i have started working on it and i m glad that i have found such a good forum with great helping ppl, i have done course in java in college and some experience of servlets as well.)
Regards
khurram fakhar
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Khurram Fakhar
When you become SCJP you will also get some more friends here right ? Welcome again.
regds
maha anna
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ijtri i rh bhu ruhua h usfedhusa ushfeusahefu
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Fred,
Until all of us find some time( after completing SCJP ) to learn other languages, can we please converse in English? I know multilingual talents are enviable assets, but not here.
Ajith
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic