,which is in fact true, why it has been marked as correct answer?for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
and if it is not correct, it means it is only 2 correct answers there, right?
thanks
Moderator: admin
,which is in fact true, why it has been marked as correct answer?for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
The code that you quoted from explanation is not the same and is presented as another example of unreachable statement.for( int i = 0; i< 0; i++) x = 3;
Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.Michailangelo wrote:But i<0 is also false.
This is not a test of good coding practices As long as it is valid, it is fair gameGuest wrote:for( int i = 0; i< 0; i++) x = 3;
Netbeans with 7.3 Beta has the warning, "For loops must use braces" for this statement:
Assumption that x was not defined will lead the user to the conclusion that none of the given options will compile, which is not true because there are three correct options. So the only logical assumption is that x is declared appropriately. This statement has now been added to avoid the confusion.for (int i = 0; i< 0; i++) x = 3;
Also, x should be defined in the question or individual answers, otherwise the user may assume that the statements won't compile as x was not defined.
-- Robert
Code: Select all
for( int i = 0; false; i++) x = 3
Code: Select all
for( int i = 0; i< 0; i++) x = 3;
Hello,ksnortum wrote:In the explanation it says,
but the option readsCode: Select all
for( int i = 0; false; i++) x = 3
This is confusing.Code: Select all
for( int i = 0; i< 0; i++) x = 3;
I'm not clear why the compiler doesn't know the value of i when it's given explicitly in the same statement:admin wrote:Yes, but you know that at run time and not at compile time. Compiler doesn't know the value of i.Michailangelo wrote:But i<0 is also false.
HTH,
Paul.
so many exception to rules which is hard to keep track of...admin wrote:This is how compilers work. The determination of the actual value of a variable happens only at runtime. That is why it is called a "variable". The compiler takes into account only those values which are constants. So if you use a final variable, then the compiler will consider its value otherwise, the compiler does not assume anything about the value and leaves it to the runtime.
Your doubt is valid though and it is possible to construct a compiler that checks this kind of situation as well. However, java language designers decided against that. The Java Language Specification describes something called "compile time constants" and that determines what the compiler considers and what it ignores. You may want to check it out.
HTH,
Paul.
=> very true! I hate this!!!baptize wrote: so many exception to rules which is hard to keep track of...
If Oracle focus on those exception to rules no one will pass the exam
Code: Select all
for(int i = 0; false; i++){ // unreachable code }
Code: Select all
for(int i = 0; i < 0; i++){ ... }
Code: Select all
for(final int i = 0; i < 0; i++){ ... }
I think this code is not valid?Kevin_C wrote:
But, what would happen if i was final, like so:Code: Select all
for(final int i = 0; i < 0; i++){ ... }
Code: Select all
for (final String s : stringArr) ....
Users browsing this forum: No registered users and 50 guests