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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

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

Post 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....

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

zombie.face
Posts: 1
Joined: Sun Feb 02, 2014 10:58 am
Contact:

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

Post by zombie.face »

Hi,

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Try printing the value of i in the loop :)
If you like our products and services, please help us by posting your review here.

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post 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.

rafaparche
Posts: 5
Joined: Tue Apr 14, 2015 9:32 am
Contact:

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

Post by rafaparche »

Hello!

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

Thanks!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Can't really comment on what IDEs do. For the exam, we recommend using the command line.
If you like our products and services, please help us by posting your review here.

Sovember
Posts: 1
Joined: Wed Nov 25, 2015 12:04 am
Contact:

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

Post 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?

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

dserge5etd
Posts: 1
Joined: Tue May 04, 2021 5:13 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests