Question

Say I have the following code below that only uses 2 curly brackets:

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught)
    if (f != null)
    System.out.println(f.toString());}

Will it hurt my code or change the way it runs if I rewrite it this way? What is generally the correct way of using curly brackets? Thanks

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught){
    if (f != null){
    System.out.println(f.toString());
    }
}     } 
Was it helpful?

Solution

For a single statement it will remain same, but if you want to group more than one statement in the if block then you have to use curely braces.

if("pie"== "pie"){
    System.out.println("Hurrah!");
    System.out.println("Hurrah!2");
}

if("pie"== "pie")
    System.out.println("Hurrah!"); //without braces only this statement will fall under if
    System.out.println("Hurrah!2"); //not this one

You should see:Blocks

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo, illustrates the use of blocks:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}

OTHER TIPS

Generally, when you create any type of loop and there is only one line of code(that is, only one statement that ends with a semicolon), you do not need the {curly-braces}. However, when you have more than one line that will be executed if the loop is entered, then use the {curly-braces} like so:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}

Code is all about whether or not it can run... I can rewrite the code as follows an it still will run perfectly:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }

The whole point of lining up the braces and other things is for readability... If you can read it, you're generally good to go!

No, it will not "hurt" your code. Actually, the good practice is to always use curly brackets. To explain - find the difference between those four:

if (2 == 2)
    System.out.println("First line");
    System.out.println("Second line");


if (2 == 2)
    System.out.println("First line");
System.out.println("Second line");


if (2 == 2) {
    System.out.println("First line");
    System.out.println("Second line");
}


if (2 == 2){
    System.out.println("First line");
}
System.out.println("Second line");

When using curly braces everything is clear on the first sight.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top