Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Mon Nov 24, 2014 2:22 am
by gparLondon
How come the compiler knows about the value of the counter, before the program has been run? or how will it evaluates if condition before the program being executed?

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Mon Nov 24, 2014 3:18 am
by admin
It doesn't know the value of counter but it knows that there is an if part and there is an else part. The if part contains break and the else part contains continue, so no matter what is the value of counter, nothing after the if/else statement can be executed.

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Wed Jul 15, 2015 4:28 pm
by Vermeulen
Is it really one of the OCA exam objectives to know when the compiler can identify unreachable code?

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Wed Jul 15, 2015 9:17 pm
by admin
Yes, you may get questions that require you know when the code will not compile because of this reason.

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Sun Oct 18, 2015 12:57 pm
by AnneLy
in the syntax (value<4) in the question does it mean that the program is trying to access the whole value of the array & check if its less than 4 ?

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Sun Oct 18, 2015 7:42 pm
by admin
AnneLy wrote:in the syntax (value<4) in the question does it mean that the program is trying to access the whole value of the array & check if its less than 4 ?
No, the code is inside the forEach loop i.e. forEach(int value : arr){ ... }. So value > 4 compares a value in the array with 4.

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Mon Jan 29, 2018 1:51 pm
by erickdeoliveiraleal
and remember break and continue only works for loops.

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Sat Jun 05, 2021 12:56 am
by baichen7788
that makes no sense.
the following run well , but the code below continue is also unreachable,which is never executed.


int[] arr = {1,2,3,4,5};
for (int value :arr
) {
if (value ==0){
System.out.println("0000");
}else {
continue;
}
if (true){
System.out.println(true);
}
System.out.println("last line");
}

Re: About Question enthuware.ocajp.i.v7.2.1413 :

Posted: Sat Jun 05, 2021 1:12 am
by admin
if(true) is an exception to the rule. See this (read the part towards the end about conditional compilation): https://docs.oracle.com/javase/specs/jl ... #jls-14.21
This point is covered in other questions.