File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes nested for loop confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nested for loop confusion" Watch "nested for loop confusion" New topic

nested for loop confusion

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107

Every time I think I understand nested loops another example comes along to mess me up .

This is the result of the previous code:

86 43 32
98 54 16

My problem is that I would have thought that there would be a line printed after each number because of the printlin() statement which follows the inner for loop. I was under the impression that everything after a for loop up until the curly brace would be executed after each loop around, and there are two print statements within the inner loop before the ending curly brace. It seems that only the first print statement gets executed.

Can anyone clarify where I am going wrong? I looked all through the previous nested for loop posts but couldn't find anything that was quite like my question.

Many thanks to anyone who can help.


alex delaguardia

Joined: Sep 07, 2004
Posts: 5
Since you are missing the curly brackets only the firt line after the for loop is executed (try putting a line of code after the second loop and before the print statement and you will see a compiler error because the variable col is not define.). You can do something similar with the if statements

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thanks so much. That was easier to understand than I thought it would be. The curly braces get me all the time. I like the If/End If of Visual Basic. It is so much easier to understand, but I like everything else about Java.

Well, thanks a lot!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
Alex has it right. I would like to add something.

I suppose what you had intended was the following

what you had originally posted was equivalent to the following...

In the first case there is a line after each number. Notice how I am positioning my curly brackets. Anyways, Try lining up up the closing brackets with the corresponding for statement. Hopefully you will will agree that this makes your code easier to read and eliminates those curly bracket gotchas that you complained about.

Edit: when there is only one statement within a for loop, curly brackets are not required. But if you get into the habit of using them anyways, that also might help eliminate problems.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I agree with Fred's closing comment:
But if you get into the habit of using them anyways, that also might help eliminate problems.

This is an example of why some coding standards say to always use braces for loops and conditionals. Even if they are only one line. It makes the code easier to read and understand. Plus it can prevent bugs when someone (even yourself) comes along later and wants to add an extra line of code to a loop or conditional, but doesn't notice there are no braces. Indenting can lie, but braces always tell the truth ;)

I personally subscribe to that idea and always use braces. In the interest of fairness, I will mention that some would disagree with this, instead preferring not to see all the white space added by the use of braces for single line conditionals and loops. Minimally, while you are learning, I would highly recommend you always use braces.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
I got that code from a beginner Java programming book and I think the curly brace after the second for loop was left off by design to show that the outer loop has three commands, and the inner loop only has one, which was the first print statement. In all the other nested for loops that I have learned about, I had never seen curly braces left off, and this threw me. I agree that the curly braces should always be used for the sake of clarity.

Thank you,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
It is worthwhile checking the website which goes with that book; it might simply be a misprint and there might be a correction on the website.
I agree. Here's the link:
subject: nested for loop confusion
It's not a secret anymore!