About Question enthuware.ocajp.i.v7.2.1265 :
Moderator: admin
-
- Posts: 1
- Joined: Mon Sep 30, 2013 9:13 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1265 :
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.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
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.
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.
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
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
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
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
This is given in the explanation:
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.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
-
- Posts: 12
- Joined: Sat Feb 22, 2014 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
Do I understand correctly that is never run because continue or break is always triggered in the inner loop?
Code: Select all
j--
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
That is correct.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1265 :
So i++ or j-- are only triggered when the control is passed back to them, otherwise not.
Who is online
Users browsing this forum: No registered users and 16 guests