The if Selection Structure in Java
 
The if selection structure allows you select or skip one or more instruction steps in a program. 
 
The if Selection Structure Template:
 
if ( test expression )
{
   // Line or lines of code selected if test expression is true
   ________________________;
   ________________________;
   ________________________;
}
 
 
You need to provide the red values. The black items are always included.
test expression - This is any logic expression.
Examples: 
menuChoice != 'n';              
income <= 0.0 || income >= 10000.0; 
 
If the test expression is true, the lines inside the if block curly braces are performed. If the test expression is false, the lines inside the if block curly braces are skipped.
 
Curly braces
The curly braces contain the lines of code that the if selection statement controls. you should indent inside the curly braces. You should also indent the curly braces at the same position.
No curly brace alternative and a warning.
You can have an if statement without curly braces. This is considered a shortcut when only the next line is controlled by the if statement. Generally you should indent the single line the if statement controls or place it on the same line as the if statement with the former preferred. Here is the sample template with two formatting styles, the first is preferred:
if ( test expression )
   ____________________; // The single line of code
                         // controlled by the if statement
____________________;    // This line of code executes
                         // independently of the
                         // if statement and regardless
                         // if you indent it.
 
if ( test expression ) ____________________; // The single line
                                             // of code can go
                                             // here too;
____________________;    // This line of code executes
                         // independently of the
                         // if statement and regardless
                         // if you indent it.
 
The danger is that if you add another line later during programming you wanted controlled by the if statement, you may overlook that the line is not part of the if statement. This leads to the more difficult to solve logic type of bug. Beginnners and most programmers should always include curly braces.
So if more than one line of code, use curly braces and as a rule of thumb always use curly braces.
 
Here is an example the takes user input and uses if selections to determine action to take.
import javax.swing.*;
public class Tester
{
   public static void main(String[]args)
   {
      String returnValue = "";  
      String prompt;
      String title;
      int msgTypeQ = JOptionPane.QUESTION_MESSAGE;
      float oldAnnualSalary = 0.0f; // Current annual salary
                                    // .01 - 1000000.00
      float newAnnualSalary = 0.0f; // New annual salary
                                    // .01 - 2000000.00
      float percentIncrease = 0.0f; // Percent increase as whole
                                    // percent
                                    // ex 10.5, 1, .03, 100
      // Prompt and get annual salary
      prompt = "Enter your annual salary";
      title  = "Attention";
      returnValue = JOptionPane.showInputDialog(null, prompt, title, msgTypeQ);
      // Salary was entered - convert to float
      if ( !returnValue.equals("")  )
      {
         oldAnnualSalary = Float.parseFloat(returnValue);
      }
   
      // Prompt and get percentage increase
      prompt = "Enter percent increase (0 - 100) ex 10.5 for 10.5% : ";
      returnValue = JOptionPane.showInputDialog(null, prompt, title, msgTypeQ);
      // Percentage increase was entered - convert to float
      if ( !returnValue.equals("") )
      {
         percentIncrease = Float.parseFloat(returnValue);
      }
    
      // oldAnnualSalary is not from .01 to 1000000.00
      if ( oldAnnualSalary < 0.01 || oldAnnualSalary > 1000000.00 )
      {
         System.out.println("Salary entered was not from 0.01 to 1000000.00.");
      }
 
      // precentIncrease is not 0.0 - 100.0
      if ( percentIncrease < 0.01 || percentIncrease > 100.00 )
      {
         System.out.println("Percent increase is not .01 to 100.");
      }
      // oldAnnualSalary is from .01 to 1000000.00
      // and percentIncrease is not 0
      if ( oldAnnualSalary >= 0.01 && oldAnnualSalary <= 1000000.00
           && percentIncrease != 0.0)
      {
         // Compute new salary
         newAnnualSalary = oldAnnualSalary *
                           (1 + percentIncrease / 100); // Convert whole
                                                        // percent to
                                                        // decimal
                                                        // and add one.
                                                        // Multiply by
                                                        // old salary

 
         // Display salary change report
         System.out.println("Your old salary is "  + oldAnnualSalary);
         System.out.println("Percent increase is " + percentIncrease);
         System.out.println("Your new salary is "  + newAnnualSalary);
      }
      // No change in salary
      if ( newAnnualSalary == 0.00 )
      {
         // Display unchanged salary report
         System.out.println("Your salary was unchanged");
      }
   }
}
 
if (returnValue != null && !returnValue.equals(""))
There are two of these if statements. They both test that the returnValue String variable is not null and is not empty. For the first of these if true the returnValue String is converted to a float and stored in oldAnnualSalary. For the second if true, the returnValue String is converted to a float and stored in percentIncreasd
 
if (oldAnnualSalary < 0.01 || oldAnnualSalary > 1000000.00)
Tests the oldAnnualSalary amount entered by the user. If it falls outside the range, the test expression is true and the message concerning this error is displayed. If false, the lines are skipped.
 
if (percentIncrease < 0.01 || percentIncrease > 100.00)
Tests the percentIncrease entered by the user. If it falls outside the range, the test expression is true and the percentIncrease variable is set to 0 and an error message is presented. If false, the lines are skipped.
 
if (oldAnnualSalary >= 0.01 && oldAnnualSalary <= 1000000.00 && percentIncrease != 0.0)
Tests the oldAnnualSalary amount entered by the user and the percentIncrease. If the oldAnnualSalary falls inside the range and the percentIncrease is not 0, the test expression is true and the computations and salary change report is generated. If false, the lines are skipped.
 
if (newAnnualSalary == 0.00)
Checks the newAnnualSalary variable to be 0. It was set to 0 at the beginning of the program. If it was not changed, then the message is displayed that is was unchanged.