2

Situation:
I have been downloading a .java-file from the internet. It's pretty messy, and I have cleaned a lot of the code with Java Docs in mind.

However:
The author of the code seems to practice three to four different ways of writing the for-loop, making it super-hard for me to debug.

Example 1 (This is the form I like):

for (int i = 0; i < x; i++) {
. . //Action inside the for-loop.
}

Example 2 (I can understand this one):

for (int i = 0; i < x; i++) //Action inside the for-loop.

Example 3 (It becomes harder...):

for (int i = 0; i < x; i++)
. . //Line 1
//Line 2
//Line 3

Example 4 (I'm totally lost):

for (int i = 0; i < x; i++)
. . //Line 1

//Line 2
for (int i = 0; i < x; i++) //Line 3

for (int i = 0; i < x; i++)
. . //Line 4

//Line 5
//Line 6

//Line 7

Question
The for-loop format i name "Example 1" is so clean; it got brackets and is tabbed. Why are they using the other formats? Are they cooler than the first example? How are the for-loops in example 4 working? Are they inside each other?

Thanks for your time and answers.

5
  • 2
    Code style, if you follow example 1 is the good choice Commented Dec 30, 2013 at 23:57
  • 1
    +1 on @nachokk - the only reason you'd actively seek to accomplish any of the later (3-4) alternatives would be... never. :( #1 is my way to go. Commented Dec 30, 2013 at 23:59
  • Agreed! Even if you only need to execute 1 line of code inside your loop, always add the brackets just to be clear, increase readability, and save yourself headaches if you add to that code later. Commented Dec 31, 2013 at 0:07
  • Thanks for answers. I am always using example 1, NEVER 2, 3 or 4, but appearently others do, and I am cleaning it. I'm not really asking which way I should go, but how I can make example 2, 3 and 4 have the same structure as example 1. Commented Dec 31, 2013 at 0:07
  • its a naturally tendency with a lot of programmers to skip the braces when there is only one line of logic (code wise) after the condition/loop(if,for loop, while loop etc). Commented Dec 31, 2013 at 0:27

8 Answers 8

4

Number 1 = Great!

Number 2 = Works, but can be confusing without brackets and can cause errors if you want to add more than one line of code later on.

Number 3 = Only going to execute Line 1 the amount of times your loop runs, the other lines will execute only once each because of the missing brackets.

Number 4 = Totally unreadable IMO, see correction below.

To fix number 2 just add brackets like you know how to.

To fix number 3 add brackets around Line 1

for (int i = 0; i < x; i++) {
    //Line 1
}
//Line 2
//Line 3

To fix number 4

for (int i = 0; i < x; i++) {
    //Line 1
}
//Line 2
for (int i = 0; i < x; i++) {
    //Line 3
    for (int i = 0; i < x; i++) {
        //Line 4
    }
}
//Line 5
//Line 6
//Line 7

As for why some coders use this style, it's just a bad and lazy habit.

Sign up to request clarification or add additional context in comments.

Comments

2

According to the Code Conventions for the Java Programming Language Example 1 is the correct format.

7.5 for Statements

A for statement should have the following form:

for (initialization; condition; update) {
  statements;
}

An empty for statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form:

for (initialization; condition; update);

When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause).

Skipping the curly brackets mean that only the statement immediately following the "for" parentheses will be included in the loop.

The only reason to use one of the other formats is because they are more compact - but, as you have discovered, it also means that they are harder to understand. They make it less clear which code will actually be executed in the for loop.

Comments

2

Author of the code should receive an email with a lot of f### words inside. There are 2 loops which are acceptable to me and easy to read

for (int i = 0; i < x; i++) {
//code
}

or

for (int i = 0; i < x; i++)
{
//code
}

Comments

2

example 3 would should look like this

for (int i = 0; i < x; i++) {
  . . //Line 1
}
//Line 2
//Line 3

and example 4 like this

for (int i = 0; i < x; i++) {
  . . //Line 1
}
//Line 2
for (int i = 0; i < x; i++) { //Line 3
  for (int i = 0; i < x; i++) {
    . . //Line 4
  }
}
//Line 5
//Line 6
//Line 7

Code like this isn't any better, some people just really don't like {} and will try to get rid of them whenever possible see python

1 Comment

So the third for-loop is nested under the second for-loop? Some other people who answered here were saying that all the for-loops in the fourth example were on the same level.
1

When there are no {}, only the first command after the loop is looped, ie. until the first ; after the loop construct. So, at example 3, only Line 1 is looped, and the loops at example 4 are not nested

Comments

1

From syntactic point of view, these forms are the same thing.

The general form of all these is:

for (int i=0; i<x; i++) some_statement

Now, some_statement is either a simple statement like

something;

or a compound statement like

{ something 1; something 2; ... ; something N; }.

So in each of the 4 cases you should just only look at the next statement
right after the closing bracket ) of the for loop. Only that
statement is inside the loop. The rest are some outside statements.

But as others pointed out, you should always use form 1).
There are several good reasons for this recommendation.

Comments

1

There is a pretty simple way to make that clearer for you. If you use eclipse, you can reformat the code automatically so it tabs out lines. But in your case, what would be simplier is just to add brackets. Here is what your example 4 should look like after reformatting. This will not change the way the compiler sees your code, so more brackets do not change anything.

for (int i = 0; i < x; i++) {
    . . //Line 1
}

//Line 2

for (int i = 0; i < x; i++) {
    //Line 3
}

for (int i = 0; i < x; i++) {
    . . //Line 4
}

//Line 5
//Line 6

//Line 7

To answer your question: they use the other ways to

  1. Make it somewhat easier to read the condition events
  2. Make it faster to code
  3. Make it easier to see what the condition affects

Comments

0

Only reason for not using brackets may be to reduce number of lines in complex method. In such cases proper indentation should be used.

1 Comment

You can use brackets without increasing number of lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.