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.
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
Joined: Mar 24, 2008
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.
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.
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.
Joined: Mar 24, 2008
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.