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

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

Moderator: admin

Post Reply
skeptic783
Posts: 1
Joined: Wed Jan 29, 2014 3:18 pm
Contact:

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

Post by skeptic783 »

Hi, newbie here. :D I was looking at question #85 on practice test #1:

Code: Select all

class LoopTest{
    public static void main(String args[]) {
        int counter = 0;
        outer:
        for (int i = 0; i < 3; i++) {
            middle:
            for (int j = 0; j < 3; j++) {
                inner:
                for (int k = 0; k < 3; k++) {
                    if (k - j > 0) {
                        break middle;
                    }
                    counter++;
                }
            }
        }
        System.out.println(counter);
    }
}
Why is it that i increments every time

Code: Select all

break middle;
executes? The label

Code: Select all

middle:
is after the for loop that would increment i?

Thanks for any insight.

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

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

Post by admin »

break middle causes only the middle loop (i.e. the j loop) to break. The i loop will continue normally and so i will increment after each iteration of i loop.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi!

I don´t see why after break middle is executed counter increments by one.

Is it not exactly the opposite? just when the if statement is not true the flow is going down to the counter++?

Thank you very much!!

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Because I see that counter++ is inside of the inner loop.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

I mean inside of the scope:
Does it mean is inside of the inner loop? because now I am very confuse :(

inner:
for (int k = 0; k < 3; k++) {
if (k - j > 0) {
break middle;
}
counter++;
}

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

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

Post by admin »

counter is incremented because the inner loop gets to complete a few iterations. See the output given in the explanation with an extra print statement. Observe the values of i, j, and k.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

What I don´t understand is why counter++ is read only when is "break middle",
It shoudn´t be read aswell in the other cases?

for (int k = 0; k < 3; k++) {
if (k - j > 0) {
break middle;
}
counter++;// why the flow of the code is not going here when the condition of
//the if is false?
}
Thank you

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

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

Post by admin »

It IS going there when the condition is false! Try adding an extra print statement and run the code. That will help you understand the flow.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Sorry, I got it now, I was blocked and I wasn't doing good the inner loop

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests