Page 1 of 1

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

Posted: Sat Apr 18, 2015 7:47 am
by rafaparche
Hello,

below code iterates only once so I thought that i would be 1 instead 0, why?

Code: Select all

for (i=0 ;       ; i++) break; 
thanks!

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

Posted: Sat Apr 18, 2015 8:55 am
by admin
Iteration doesn't complete because of the break so i++ is never executed.

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

Posted: Wed Sep 16, 2015 5:07 pm
by madhurisatish
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

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

Posted: Wed Sep 16, 2015 5:11 pm
by madhurisatish
does it mean it doesn't have body. Is it same like

Code: Select all

for(;i<5;) {
             }

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

Posted: Wed Sep 16, 2015 7:15 pm
by admin
madhurisatish wrote:does it mean it doesn't have body. Is it same like

Code: Select all

for(;i<5;) {
             }
Yes.

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

Posted: Mon Oct 09, 2017 9:35 am
by JuergGogo

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.

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

Posted: Tue Dec 12, 2017 11:51 pm
by tanushri
should it compile if the local variable i is being redefined (in the for- loops) within the same scope, ie main method?

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

Posted: Wed Dec 13, 2017 12:46 am
by admin
what happened when you tried it out?

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

Posted: Wed Dec 13, 2017 10:13 pm
by tanushri
i didnt run it. But was comparing with Question 8 of test 7.

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

Posted: Sun Nov 11, 2018 10:47 am
by flex567
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?

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

Posted: Sun Nov 11, 2018 8:58 pm
by admin
Yes, it can appear in all loops including enhanced for. Explanation has been updated to make it clear.
thanks!

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

Posted: Wed Jan 09, 2019 4:59 pm
by crazymind
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.

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

Posted: Wed Jan 09, 2019 10:08 pm
by admin
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.

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

Posted: Thu Apr 04, 2019 1:33 pm
by li_hanglin@bah.com
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?

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

Posted: Thu Apr 04, 2019 9:53 pm
by admin
No, //2 doesn't contain declaration i.e. int i=0; it contains only assignment i.e. i=0;