Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Mon Sep 30, 2013 11:13 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Mon Sep 30, 2013 11:20 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Mon Nov 25, 2013 8:00 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Tue Nov 26, 2013 7:32 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Tue Mar 04, 2014 8:22 am
by crux terminatus
Do I understand correctly that
is never run because continue or break is always triggered in the inner loop?
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Tue Mar 04, 2014 10:52 am
by admin
That is correct.
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Posted: Wed May 20, 2015 2:44 am
by subhamsdalmia
So i++ or j-- are only triggered when the control is passed back to them, otherwise not.