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
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

enthuware.ocajp.i.v7.2.1083

Post by ashishrai.kv »

Code: Select all

What will the following program print?


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);
    }
}
Is there any trick or way to go through the problems given above which have multiple loops running through, to check and get the answers?

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

Re: enthuware.ocajp.i.v7.2.1083

Post by admin »

Not really. You need write the values of the loop variables on a paper at each step. It sounds too much but usually the loops in the questions end rather quickly due to strategic placement of break/continue.
If you like our products and services, please help us by posting your review here.

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.1083

Post by ashishrai.kv »

ok thanks.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: enthuware.ocajp.i.v7.2.1083

Post by flex567 »

I think the point here is that after

Code: Select all

break middle;

the execution doesn't enter the second loop

Code: Select all

for (int j = 0; j < 3; j++)
but continuous with the first one ?

Code: Select all

for (int i = 0; i < 3; i++)

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1083

Post by crazymind »

My question is: why does ''counter++;" get execute after "break middle;" ? The code immediately goes to outer loop after ''break middle".

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

Re: enthuware.ocajp.i.v7.2.1083

Post by admin »

No, break middle; will break the loop labelled "middle". The control will go to the next statement after middle loop, which means the next iteration of outer loop.

Also, break middle; is executed only if k - j > 0. You will need to write the values of the variables in each iteration to see why counter++ gets executed. Check the output shown in the explanation.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1083

Post by crazymind »

Thanks. Do you mean that counter++ gets executed after break middle?
So counter++ will not get executed if I change it to break inner.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1083

Post by crazymind »

sorry. I did not look this code carefully. I understand now.

Post Reply

Who is online

Users browsing this forum: gadsgadsx and 14 guests