Java: for-loops: Different loop-styles

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.

Upvotes: 2

Views: 894

Answers (8)

takendarkk
takendarkk

Reputation: 3443

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.

Upvotes: 4

Siri
Siri

Reputation: 207

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

Upvotes: 0

user3120586
user3120586

Reputation: 51

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

Upvotes: 2

Fred
Fred

Reputation: 21

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

Upvotes: 1

Johan Kaving
Johan Kaving

Reputation: 4939

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.

Upvotes: 2

peter.petrov
peter.petrov

Reputation: 39457

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.

Upvotes: 1

Andrew_Dublin
Andrew_Dublin

Reputation: 745

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
}

Upvotes: 2

deviantfan
deviantfan

Reputation: 11434

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

Upvotes: 1

Related Questions