• Post Reply Bookmark Topic Watch Topic
  • New Topic

No Output  RSS feed

 
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i run this is it:
A) No output after successful compilation.
B) No output because of compilation error.

i would say it is A because when i run it i get no error but can anybody explain why its A or B? 
 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see the options mentioned,

B says its a compile error, I don't see any compile error in that code.

A says it will compile (so far so good)... Next, it also says that there will be no output. This is true since the loop is never executed even once.

Maybe they meant to say no output when you execute it
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has 3 loop variations: for, while and do-while
The for loop and the while loop first check the condition and then execute the code in it. The do-while loop first executes the code and then checks the condition. Only a do-while loop is guaranteed to run once.

 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Lets see the options mentioned,

B says its a compile error, I don't see any compile error in that code.

A says it will compile (so far so good)... Next, it also says that there will be no output. This is true since the loop is never executed even once.

Maybe they meant to say no output when you execute it

I was thinking the same i just wanted to see if anybody can explain it to me so i can understand it and not just say its right because it is and you did, Thanks!!
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

While this question looks very simple, it can become tricky fairly quick.

There are cases when such code wouldn't compile. i.e.:

As you see all 3 examples are meant to do the same. Let's see if you can find the reason, why none of those 2 codes snippets I provided won't compile? When you come across such code snippets in exam or similarly, don't rush, have a look at it few times.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: have a look in the Java® Language Specification (=JLS). Beware: the JLS can be very difficult to read.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it does not work for if conditions:



But thats kinda strange in my opinion
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Note that it does not work for if conditions: . . . But thats kinda strange in my opinion
The definition of reachability for the if statement in that JLS link I posted yesterday includes:-
the JLS wrote:• An if-then statement can complete normally iff it is reachable.
    The then-statement is reachable iff the if-then statement is reachable.
Loops have a different definition of reachability:-
A while statement can complete normally iff at least one of the following is true:
•    The while statement is reachable and the condition expression is not a constant expression (§15.28) with value true.
•    There is a reachable break statement that exits the while statement.
The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.
So while (false) ... includes unreachable code. I have heard an explanation for the difference, but have forgotten the details.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is reaching back a little bit but it may have something to do with the lack of conditional compiler directives like C's #ifdef in Java. 

When you change the constant to false, the code still needs to compile.

After looking around a little bit, that's actually exactly how the JLS explains it. See the bottom part of this page: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done finding that
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing that info guys have a cow.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the cow
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alvaroo Hernandez wrote:

When i run this is it:
A) No output after successful compilation.
B) No output because of compilation error.

i would say it is A because when i run it i get no error but can anybody explain why its A or B? 


Good question...the answer is very simple...
while(false) --> results in a compilation error because the compiler will never compile a code that never gets executed. Its like asking me to teach a class which has no participants.
The same theory is applicable when you put statements after break/continue/throw etc. Bottom line is compiler never compiles any code that has no chance of getting executed.
while(<var_name>) --> compiles successfully because at some point of time during execution, the statements might get executed because the call can come from some other class passing a different value. There's a slight chance somewhere...so compiler cannot stop you from writing such code.

Hope you got the point now. Happy Learning!!!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:
Alvaroo Hernandez wrote:

When i run this is it:
A) No output after successful compilation.
B) No output because of compilation error.

i would say it is A because when i run it i get no error but can anybody explain why its A or B? 


Good question...the answer is very simple...
while(false) --> results in a compilation error because the compiler will never compile a code that never gets executed. Its like asking me to teach a class which has no participants.
The same theory is applicable when you put statements after break/continue/throw etc. Bottom line is compiler never compiles any code that has no chance of getting executed.
while(<var_name>) --> compiles successfully because at some point of time during execution, the statements might get executed because the call can come from some other class passing a different value. There's a slight chance somewhere...so compiler cannot stop you from writing such code.

Hope you got the point now. Happy Learning!!!



I am using eclipse for me this code is compiling and giving no output which is OK since the condition in the While loop is 'False" to start with

In fact if I add a print statement just where the While loop exits it is getting printed.
The following code is printing "Out of while loop" as an output of this program, which proves that the code is getting compiled.

 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is printing "Out of while loop" as an output of this program, which proves that the code is getting compiled.

It is, but try this:

You will find that the program does not compile.  That is because the compiler now knows that token must be false. 
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
The following code is printing "Out of while loop" as an output of this program, which proves that the code is getting compiled.

It is, but try this:

You will find that the program does not compile.  That is because the compiler now knows that token must be false. 


I am not sure following is my two cents on it. I would love to have Sheriff Campbell's views on this.

The compiler always know that the variable "token" is false, but since it is a variable it can be changed during the course of "While" loop. But when you mark it as "Final" you make it a constant and it cannot be changed and there is not point is executing the "While" loop hence compilation error.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to read what the JLS says. The difference between a boolean valued false and what Knute showed is that now the compiler “knows” that boolean is always false, and the loop counts as unreachable, by the definition earlier in this discussion. You shou‍ld have believed Knute all along.
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You will have to read what the JLS says. The difference between a boolean valued false and what Knute showed is that now the compiler “knows” that boolean is always false, and the loop counts as unreachable, by the definition earlier in this discussion. You shou‍ld have believed Knute all along.


I am sorry, did not notice that. Knute is right.

Regards
Anurag
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Apology accepted


You Rock!!!
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag Singh wrote:
Shibu Kurian wrote:
Alvaroo Hernandez wrote:

When i run this is it:
A) No output after successful compilation.
B) No output because of compilation error.

i would say it is A because when i run it i get no error but can anybody explain why its A or B? 


Good question...the answer is very simple...
while(false) --> results in a compilation error because the compiler will never compile a code that never gets executed. Its like asking me to teach a class which has no participants.
The same theory is applicable when you put statements after break/continue/throw etc. Bottom line is compiler never compiles any code that has no chance of getting executed.
while(<var_name>) --> compiles successfully because at some point of time during execution, the statements might get executed because the call can come from some other class passing a different value. There's a slight chance somewhere...so compiler cannot stop you from writing such code.

Hope you got the point now. Happy Learning!!!



I am using eclipse for me this code is compiling and giving no output which is OK since the condition in the While loop is 'False" to start with

In fact if I add a print statement just where the While loop exits it is getting printed.
The following code is printing "Out of while loop" as an output of this program, which proves that the code is getting compiled.



I think that is what i said...you can read it again...when you write while(token){..}, it'll compile, reason i've already mentioned. But if you write while(false){..} it'll not compile successfully...reason already mentioned in my previous post.
Let me know if you still require help
Content minimized. Click to view
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think that is what i said...you can read it again...when you write while(token){..}, it'll compile, reason i've already mentioned. But if you write while(false){..} it'll not compile successfully...reason already mentioned in my previous post.
Let me know if you still require help


Thanks!!!

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!