Why Does Java Use Braces on Conditional Statements?

Why Does Java Use Braces on Conditional Statements? thumbnail
Java programs use conditional tests to tailor program execution.

Learning Java syntax involves learning how to construct individual statements, but also how to group statements together. If you are just starting out as a Java developer, you will find yourself dealing with larger sections of code as your skills progress. Understanding the ways in which Java applications group code elements together is a key skill. Control structures such as conditionals are among those code blocks you are most likely to use.

  1. Java Code Blocks

    • Sections of Java code appear within programs in blocks. Blocks are indicated by surrounding braces, an opening brace before the block and a closing brace after it. Code blocks delineated by braces include methods, loops and conditionals. A code block in Java normally indicates a series of statements that will all be executed one after another. The following example demonstrates a code block for a Java method:

      public void doSomethingGood() {

      int sum = 5+6;

      System.out.println(sum);

      }

      If this method executes, both of the code statements within it will execute.

    Conditionals Without Braces

    • Some Java conditionals do not use braces. This is appropriate in cases where conditional statements only contain a single line of code. The following example demonstrates a series of chained conditionals without braces:

      //variable "aNumber" already exists

      if(aNumber>0)

      doSomething();

      else if(aNumber<0)

      doSomethingElse();

      else

      doAnotherThing();

      Each section in this series of conditional tests contains a single line of code, so no braces are necessary.

    Conditionals With Multiple Lines

    • If the content of a Java conditional section includes multiple lines of code, braces are essential to group the code together. The following example code demonstrates a conditional statement with two lines of code:

      if(aNumber<0){

      System.out.println(aNumber);

      aNumber=0;

      }

      This code indicates that both lines will be executed if the conditional test returns a true value. If the conditional test returns a false value, neither of the two contained lines will execute.

    Conditional Processing Errors

    • Using braces incorrectly can cause programming errors. For example, if a conditional test does not use braces to group its two statements together, everything after the first line will execute regardless of the result of the conditional test:

      if(aNumber<0)

      System.out.println(aNumber);

      aNumber=0;

      In this case the first line will only execute if the conditional test returns a true value, but the second line will execute even if the conditional test is false. This is a common error in cases where programmers have originally only had one line in a code block, then decided to add additional code to it, forgetting to group it inside a block using braces.

Related Searches:

References

Resources

  • Photo Credit Thinkstock Images/Comstock/Getty Images

Comments

Related Ads

Featured