Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jashburn

Thread: Code brackets and indentation

  1. #1
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Code brackets and indentation

    Hi guys,

    As I am trying to get into Java I have been following a calculator tutorial and have come across the following code:

    Java Code:
    for(int i = 4; i < 8; i++)
        row[2].add(button[i]);
    row[2].add(button[15]);
    add(row[2]);
    
    for(int i = 8; i < 12; i++)
        row[3].add(button[i]);
    row[3].add(button[16]);
    add(row[3]);
    
    row[4].add(button[18]);
    for(int i = 12; i < 14; i++)
        row[4].add(button[i]);
    row[4].add(button[17]);
    add(row[4]);
    Whilst I understand the code, I do have a few questions regarding it.

    When I wrote the code I added brackets around each of the for loops and my indentation was slightly different (in my opinion both these things make the code easier to read). My code is as follows.

    Java Code:
           for(int i = 4; i < 8; i++){
                row[2].add(button[i]);
                row[2].add(button[15]);
                add(row[2]);
            }
            for(int i = 8; i < 12; i++){
                row[3].add(button[i]);
                row[3].add(button[16]);
                add(row[3]);
                row[4].add(button[18]);
            }
            for(int i = 12; i < 14; i++){
                row[4].add(button[i]);
                row[4].add(button[17]);
                add(row[4]);
            }
    What I dont understand is why the braces are not required around each for loop? How does it distinguish between nested for loops and a new for loop without the braces?

    Finally, If, in netbeans I write the code without the braces with the following indentation I get a yellow underscore under the line which reads row[4].add(button[17]); saying confusing indentation, however if I add my braces around the for loop this underscore disappears.

    Below code gives gives yellow line stating confusing indentation for row[4].add(button[17]);
    Java Code:
    for(int i = 12; i < 14; i++)
         row[4].add(button[i]);
         row[4].add(button[17]);
         add(row[4]);
    Below does not give indentation warning
    Java Code:
    for(int i = 12; i < 14; i++)
         row[4].add(button[i]);
    row[4].add(button[17]);
    add(row[4]);
    Any explanation on this would be greatly appreciated. Obviously as im starting out I want to follow good practise from the beginning so any pointers are welcome.

    Thank you.
    Last edited by Dark Knight; 03-09-2014 at 01:14 PM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Code brackets and indentation

    A good practice would be to always use braces even when it is not necessary. Doing so avoids subtle mistakes that can be easily overlooked.

    For example,

    Java Code:
    for(int i = 4; i < 8; i++)
        row[2].add(button[i]);
    row[2].add(button[15]);
    add(row[2]);
    does not translate to

    Java Code:
    for(int i = 4; i < 8; i++){
        row[2].add(button[i]);
        row[2].add(button[15]);
        add(row[2]);
    }
    This is because braces for a for loop are optional if its body has only 1 line of code. In other words, the first code snippet above actually translates to

    Java Code:
    for(int i = 4; i < 8; i++) {
        row[2].add(button[i]);
    }
    row[2].add(button[15]);
    add(row[2]);
    See how more explicit it is with braces?

    Indentation is not structurally significant in Java, nor is it enforced like the Python programming language. Indentation in Java is mainly to help with code reading. It is up to the programmer to use it wisely, and IDEs like NetBeans and Eclipse provide formatters to help format and indent code consistently. Indentation, together with liberal use of braces, improves code readabilty and helps prevent subtle mistakes.
    Dark Knight likes this.

  3. #3
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Re: Code brackets and indentation

    Ahh, thankyou for that, so in fact, my translation of the code with the brackets is incorrect and unless there are brackets enclosing code in a for loop it is only the 1st line which is considered to be in the loop. That probably explains the indentation from the tutorial as the code is not actually part of the for loop and as such does not have the same indentation as the first line which is.

    Thanks again.

Similar Threads

  1. Indentation not possible in JTextPane?
    By Flo in forum AWT / Swing
    Replies: 1
    Last Post: 08-22-2011, 03:54 AM
  2. Indentation - plain text
    By barney75 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 05:54 PM
  3. Indentation Size Reset While Coding JavaFX
    By JDCAce in forum NetBeans
    Replies: 0
    Last Post: 10-06-2008, 09:28 PM
  4. String Code Indentation
    By andysh in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:45 AM
  5. how to set indentation to a text
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •