Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817
Moderator: admin
rafaparche
Posts: 5 Joined: Tue Apr 14, 2015 9:32 am
Contact:
Post
by rafaparche » Sat Apr 18, 2015 7:47 am
Hello,
below code iterates only once so I thought that i would be 1 instead 0, why?
thanks!
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sat Apr 18, 2015 8:55 am
Iteration doesn't complete because of the break so i++ is never executed.
madhurisatish
Posts: 15 Joined: Thu Sep 10, 2015 3:50 pm
Contact:
Post
by madhurisatish » Wed Sep 16, 2015 5:07 pm
I want to know what does for ( ; i<5?false:true ; ); mean??
Basically, I have a question like what does for (; i<5;); mean?
Please explain
madhurisatish
Posts: 15 Joined: Thu Sep 10, 2015 3:50 pm
Contact:
Post
by madhurisatish » Wed Sep 16, 2015 5:11 pm
does it mean it doesn't have body. Is it same like
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Sep 16, 2015 7:15 pm
madhurisatish wrote: does it mean it doesn't have body. Is it same like
Yes.
JuergGogo
Posts: 28 Joined: Mon Sep 25, 2017 8:16 am
Contact:
Post
by JuergGogo » Mon Oct 09, 2017 9:35 am
Code: Select all
class TestClass{
public static void main(String args[]){
int i = 0;
for (i=1 ; i<5 ; i++) continue; //(1)
for (i=0 ; ; i++) ; // break; //(2)
for ( ; i<5?false:true ; ); //(3) --> compiler error: unreachable statement
}
}
I have chosen the second Loop as invalid, but it works fine. When eliminating the break statement, you get a compiler error.
tanushri
Posts: 5 Joined: Tue Dec 12, 2017 11:45 pm
Contact:
Post
by tanushri » Tue Dec 12, 2017 11:51 pm
should it compile if the local variable i is being redefined (in the for- loops) within the same scope, ie main method?
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Dec 13, 2017 12:46 am
what happened when you tried it out?
tanushri
Posts: 5 Joined: Tue Dec 12, 2017 11:45 pm
Contact:
Post
by tanushri » Wed Dec 13, 2017 10:13 pm
i didnt run it. But was comparing with Question 8 of test 7.
flex567
Posts: 202 Joined: Mon Apr 02, 2018 8:40 am
Contact:
Post
by flex567 » Sun Nov 11, 2018 10:47 am
From the answer:
Code: Select all
A continue statement can occur in and only in a for, while or do-while loop
Is the for each loop included?
In which loop continue cant occur?
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sun Nov 11, 2018 8:58 pm
Yes, it can appear in all loops including enhanced for. Explanation has been updated to make it clear.
thanks!
crazymind
Posts: 85 Joined: Mon Dec 24, 2018 6:24 pm
Contact:
Post
by crazymind » Wed Jan 09, 2019 4:59 pm
Isn't it suppose be an unreachable code error? first loop keep iterates since continue then gets out. Other two inner loops never get executed.
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Jan 09, 2019 10:08 pm
No, because the statements use variables that are not compile time constants. That is why the compiler cannot know the value of the loop variables at compile time.
li_hanglin@bah.com
Posts: 6 Joined: Mon Jan 28, 2019 4:18 pm
Contact:
Post
by li_hanglin@bah.com » Thu Apr 04, 2019 1:33 pm
In the explanation, it says: for ( ; i<5?false:true ; ); never iterates because i is less than 5 (it is 0 because of //2).
isn't the i in //2 local to that for loop and go out of scope in //3 so for ( ; i<5?false:true ; ) would use the i from int i = 0?
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Thu Apr 04, 2019 9:53 pm
No, //2 doesn't contain declaration i.e. int i=0; it contains only assignment i.e. i=0;
Users browsing this forum: Bing [Bot] and 6 guests