File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem with self test question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with self test question" Watch "Problem with self test question" New topic
Author

Problem with self test question

jay sugrue
Greenhorn

Joined: May 29, 2012
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

Joined: Jun 19, 2010
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:


The quieter you are, the more you are able to hear.
jay sugrue
Greenhorn

Joined: May 29, 2012
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

Joined: Jun 19, 2010
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

Joined: May 29, 2012
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

Joined: Jun 19, 2010
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

Joined: Jan 03, 2004
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

Joined: May 29, 2012
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

Joined: Jun 19, 2010
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

Joined: May 29, 2012
Posts: 20
Cool. Thanks again Kemal.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Any time, glad we could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with self test question