Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Problem with self test question

jay sugrue
Greenhorn
Posts: 20

Hi,

In the Sierra-Bates self tests I'm having difficulty with this question, its q10 from chapter 5

My understanding of this code is as follows:

Inner loop first iteration
j = 0
x = 1 //1 is printed
break not executed

Inner loop second iteration
j = 1
x = 1 //1 again is printed

break condition resolves to true, break is executed so in the outer loop x is moved to next value = 3
So, at this point, does the break statement mean, as the next inner loop iteration is entered, the value of j is
still at 1 or is it now 2 ?

If j's value is indeed 2 and the inner loop iterates, x is printed as 3, and on the next iteration of the inner loop the
condition fails (j becomes 3) does the print statement still run ? Printing x as 3 again. If so giving us 1133 ?

I know it can't be 1 - would that not result in the next element of the outer loop ie. 5 when the answer is:

113399

Hope formatting is better than my last post ! There's a couple more questions on this but for now, thanks !

Kemal Sokolovic
Bartender
Posts: 825
5
So, at this point, does the break statement mean, as the next inner loop iteration is entered, the value of j is
still at 1 or is it now 2 ?

No. That break statements breaks from inner for loop to the end of the outer for loop (line 15 of your code). So after it's executed, new value to is assigned to x (value after 1 is 3) and then the execution enters inner for loop from the beginning, hence j is initialized to 0 again.

You should note that each time the next value of x is used form ia array, inner for loop is entered afterwards; and each time you enter it, j has the initial value of 0.

Edit: To make it more clear to you, here is how the execution of your program looks like:

jay sugrue
Greenhorn
Posts: 20
Yeah, this makes much more sense. Thanks Kemal for all your time and effort on this, I really appreciate it.

...and to hopefully wrap this up the continue below is why 5 and 7 are skipped ?

Kemal Sokolovic
Bartender
Posts: 825
5
That's right. That continue says - proceed with next iteration (inner for), so the execution in that case doesn't come to the statement that prints out the value.

jay sugrue
Greenhorn
Posts: 20
Kemal,

Just looked again at your code solution - apologies for this but according to the answer given the last two continue statements are redundant. So, in this case is it the break statement that moves the outer loop onto next element ?

Kemal Sokolovic
Bartender
Posts: 825
5
jay sugrue wrote:Kemal,

Just looked again at your code solution - apologies for this but according to the answer given the last two continue statements are redundant. So, in this case is it the break statement that moves the outer loop onto next element ?

Didn't quite understand what you meant.

Edit: Looked again through all the posts. The post I wrote on how the execution looks like says that whenever j == 1 evaluates to true "code breaks to second continue which gets you to the next iteration of outer loop". So basically, the second continue statement (line 15 of your code) is executed after a break statement, and the outer loop proceeds with the next value from array.

However, those continue statements are redundant; you would get the same result if you've not had them (try to comment them out and see what happens).

Jeff Verdegan
Bartender
Posts: 6109
6
jay sugrue wrote:Kemal,

Just looked again at your code solution - apologies for this but according to the answer given the last two continue statements are redundant. So, in this case is it the break statement that moves the outer loop onto next element ?

Not totally sure what you mean, and the messed up indentation makes it hard to see what's going on. However, it looks like the continues are the last statements in their respective loops. If that is in fact the case, then they are redundant an do nothing.

jay sugrue
Greenhorn
Posts: 20
Kemal, Jeff,

Yes, the last two continue statements have no active role in the code. so, given this, the outer loops are iterated through via the break statement ie.

At this point the inner loop is broken and the outer loop iterates to the next element. 1 changes over to 3. Yeah ?

Kemal Sokolovic
Bartender
Posts: 825
5
Exactly. Though the last continue is executed after break, it doesn't make any difference as the code would be executed the same way without it.

jay sugrue
Greenhorn
Posts: 20
Cool. Thanks again Kemal.

Kemal Sokolovic
Bartender
Posts: 825
5
Any time, glad we could help.