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: 9174
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: 9174
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: 31
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post by alkour »

in the explanation that is written that values of i an j changes as follows:
i = 0 j = 5
i = 0 j = 4
i = 0 j = 3
.....

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

thought that first iteratin would start with
i

alkour
Posts: 31
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: 9174
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: 31
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: 9174
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: 9174
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: 9174
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest