About Question enthuware.ocajp.i.v7.2.1413 :
Moderator: admin
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1413 :
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?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
Is it really one of the OCA exam objectives to know when the compiler can identify unreachable code?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
Yes, you may get questions that require you know when the code will not compile because of this reason.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Oct 18, 2015 12:53 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
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 ?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
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.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 ?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Jan 29, 2018 1:47 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
and remember break and continue only works for loops.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
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");
}
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");
}
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1413 :
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.
This point is covered in other questions.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 31 guests