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

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

Moderator: admin

Post Reply
sergiogeorgini
Posts: 1
Joined: Mon Sep 30, 2013 9:13 am
Contact:

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

Post by sergiogeorgini »

Hi, I do not understand why when the loop for(i = 0; i < 3; i++) ends, the value of i is 3 and not 2.

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

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

Post by admin »

i starts with 0 and its value is incremented each time at the end of an iteration.

At the end of first iteration, i becomes 1 and the control goes back to the for statement, where i<3 is checked. This is true, so the second iteration happens, at the end of which i is incremented to 1. and so on...

Fast forwarding to when i is 2, the condition i<3 is satisfied, so the loop runs for that value of i. Once the loop is done, i is incremented again and becomes 3. Now, i<3 fails and the loop is exited.

That is why after the loop the value of i is 3 and not 2.

HTH,
Paul.

Zoryanat
Posts: 20
Joined: Tue Aug 27, 2013 3:16 am
Contact:

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

Post by Zoryanat »

Could you please explain what is happening to j?
Shouldn't it be decremented it once it had reached "continue" or "break" statement? How does it manages to stay "3" by the end of loop?

Many thanks!
Regards
Zoryana

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

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

Post by admin »

This is given in the explanation:
The statement: if(i < j) continue X1; else break X2; only makes sure that the inner loop does not iterate more than once. i.e. for each iteration of i, j only takes the value of 3 and then the j loop terminates
The value of j at the end of the inner loop is 3 and so it will remain as it is after the loop. So I am not sure why do you think it should change back to something else.

crux terminatus
Posts: 12
Joined: Sat Feb 22, 2014 3:55 pm
Contact:

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

Post by crux terminatus »

Do I understand correctly that

Code: Select all

j--
is never run because continue or break is always triggered in the inner loop?

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

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

Post by admin »

That is correct.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

So i++ or j-- are only triggered when the control is passed back to them, otherwise not.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests