Page 1 of 1

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

Posted: Thu Jan 17, 2013 6:49 pm
by deepa.patre
Hi,

i didn't understand one point. Why does the value of i remain as 0 not changing to 1?
i know it is post increment, but according to post increment the value of i changes when accessed next.... n also if i replace i++ with ++i there is no change in the output... please help me in understanding....

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

Posted: Thu Jan 17, 2013 7:53 pm
by admin
deepa.patre wrote:Hi,

i didn't understand one point. Why does the value of i remain as 0 not changing to 1?
i know it is post increment, but according to post increment the value of i changes when accessed next.... n also if i replace i++ with ++i there is no change in the output... please help me in understanding....
Your understanding is correct. But in this case the outer loop executes only once and it breaks before starting the next iteration. so i++ is never executed.

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

Posted: Sun Feb 02, 2014 11:11 am
by zombie.face
Hi,

What is the exit condition of the first loop? I am confused. Why the first loop exits?

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

Posted: Sun Feb 02, 2014 9:00 pm
by admin
zombie.face wrote:Hi,

What is the exit condition of the first loop? I am confused. Why the first loop exits?
It exits because of the statement break lab1;
lab1 points to the first i.e. the outer loop. So that causes the outer loop to break irrespective of its condition.

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

Posted: Mon Apr 06, 2015 5:14 am
by alkour
In the explanation, is written that values of i an j changes as follows:
i = 0 j = 5
i = 0 j = 4
i = 0 j = 3
.....

As the inner loop with changing j has prefix --j
for( ; ; --j) if (i > j) break lab1;

I thought that first iteratin would start with:
i = 0 j = 4.

Not with j=5.

Am I correct?

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

Posted: Mon Apr 06, 2015 10:53 am
by admin
Try printing the value of i in the loop :)

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

Posted: Wed Apr 08, 2015 2:58 am
by alkour
Yes. It is correct.

Code: Select all

    public static void main(String[] args) {
        int i = 0, j = 5;
        lab1: for ( ; ; i++){
            System.out.println("Outer loop: i = " + i +", j = "+ j); 
            for (; ; --j) { 
                System.out.println("Inner loop: i = " + i +", j = "+ j);
                if (i > j ) break lab1;
            }
        } 
        System.out.println(" i = " + i +", j = "+ j); 
    }   
}
Output

Code: Select all

Outer loop: i = 0, j = 5
Inner loop: i = 0, j = 5
Inner loop: i = 0, j = 4
Inner loop: i = 0, j = 3
Inner loop: i = 0, j = 2
Inner loop: i = 0, j = 1
Inner loop: i = 0, j = 0
Inner loop: i = 0, j = -1
 i = 0, j = -1
And it does not matter whether it will be prefix --j and postfix j++ in for loop.
It works the same.

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

Posted: Thu Apr 16, 2015 12:02 pm
by rafaparche
Hello!

Why Eclipse mark i++ as dead code and --z as normal code?

Thanks!

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

Posted: Thu Apr 16, 2015 7:44 pm
by admin
Can't really comment on what IDEs do. For the exam, we recommend using the command line.

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

Posted: Wed Nov 25, 2015 12:56 am
by Sovember
Hi, sorry probably a very basic misunderstanding for me, just beginning studying java.

Can I ask why it only goes through the first iteration of the loop? I thought the break statement only ran if the if statement conditions were met (i > j) ? Isn't i less than j?

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

Posted: Tue Nov 08, 2016 8:16 am
by vlezz94
Hi Paul!

Right now I'm struggling to understand why in this code the value of "i" is never incremented:

Code: Select all

public class BreakTest{
  public static void main(String[] args){
    int i = 0, j = 5;
    lab1 : for( ; ; i++){
      for( ; ; --j)  if( i >j ) break lab1;
    }
    System.out.println(" i = "+i+", j = "+j);
  }
}
Also, in one of the previous tests I came across another question that had loop labels. In that question the "break -label-;" was nested too but it didn't compile. Why in this code compiles?

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

Posted: Tue Nov 08, 2016 8:25 am
by admin
i never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++) never gets to execute.
Observe the inner loop. It keeps decrementing j until i becomes equal to j and then it breaks the outer loop using a labeled break.

You will need to post the code from the other question for me to help you with it because I do not remember the question.

HTH,
Paul.

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

Posted: Tue Nov 08, 2016 9:09 am
by vlezz94
This is the code (The correct answer is that it doesn't compile):

Code: Select all

void crazyLoop(){
   int c = 0;
   JACK: while (c < 8){
       JILL: System.out.println(c);
       if (c > 3) break JILL; else c++;
   }
}

So, when I run the program the i variable value is never incremented because there is a "break" statement. And if it wasn't in there the loop would be infinite and also it would never be incremented as well. I think I got it, thanks Paul!

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

Posted: Tue Nov 08, 2016 10:03 am
by admin
Ok, in this code, the label JILL does not identify a loop, so when you call break JILL, there is nothing to break and so the compiler complains.

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

Posted: Tue May 04, 2021 10:47 am
by dserge5etd
admin wrote:
Tue Nov 08, 2016 8:25 am
i never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++) never gets to execute.
Observe the inner loop. It keeps decrementing j until i becomes equal to j and then it breaks the outer loop using a labeled break.

You will need to post the code from the other question for me to help you with it because I do not remember the question.

HTH,
Paul.
Hi, please help with another task 11.2.3545 :

Code: Select all

        int i=0, j=0;
        X1: for(i = 0; i < 3; i++){
            X2: for(j = 3; j > 0; j--){
                if(i < j) continue X1;
                else break X2;
            }
        }
        System.out.println(i+" "+j);
    }
I thought that
i never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++) never gets to execute.
also true for the following code X1: for(i = 0; i < 3; i++){ , but the right answer of 11.2.3545 : 3 3

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

Posted: Tue May 04, 2021 11:31 am
by admin
In 2.3545, the statement break X2; breaks the inner loop because the label X2 is applied on the inner loop. But in 2.1032, break lab1; breaks the outer loop because the label lab1 is applied on the outer loop.