The if else Selection Structure in Java
 
The if else selection structure allows you select between two sets of instructions. One set is skipped and the other is executed.
 
The if else Selection Structure Template:
 
if ( test expression )
{
   // Line or lines of code selected if test expression is true
   ________________________;
   ________________________;
   ________________________;
}
else
{
   // Line or lines of code selected if test expression is false
   ________________________;
   ________________________;
   ________________________;
}
 
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 and the lines inside the else block are skipped.
 
If the test expression is false, the lines inside the if block curly braces are skipped and the lines inside the else block are performed.
 
Curly braces
The curly braces contain the lines of code that the if else 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 and the else statement without and with curly braces. Without curly braces is considered a shortcut when only the next line is controlled by the if statement or the else. Generally you should indent the single line the if or the else statement controls or place it on the same line as the if or else statement with the former preferred. Here is an example where both the if and the else only control one line of code using the preferred styling.
if ( test expression )
   ____________________;  // The single line of code
                          // controlled by the if statement
// DO NOT PUT CODE HERE - COMPILER ERROR!
else 
   ___________________;   // The single line of code
                          // controlled by the else statement
____________________;     // This line of code executes
                          // independently of the
                          // else statement and regardless
                          // if you indent it.
 
One danger is placing additional lines after the line controlled by the if statement and before the else statement. This fortunately will result in a compiler error and you need to either move the line or add curly braces.
 
Another danger is that if you add another line later during programming you wanted controlled by the else statement, you may overlook that the line is not part of the else statement. This leads to the more difficult to solve logic type of bug.
 
Beginners and most programmers should always include curly braces for both the if and the else statements.
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. It uses if else selections and nested if selections.
public class Tester
{
   // Fields (Always static in main method of starting class)
   static float oldAnnualSalary;       // Current annual salary
                                       // 0 not valid or unchanged
                                       // .01 - 1000000.00
  
static float newAnnualSalary;       // New annual salary
                                       // 0 not valid or unchanged
                                       // .01 - 2000000.00
  
static float percentIncrease;       // Percent increase as whole
                                       // percent
                                       // ex 10.5, 1, .03, 100
  
static int msgTypeQ =
      JOptionPane.QUESTION_MESSAGE;    // showInputDialog message type
                                       // in variable to shorten code
  
  
public static void main(String[]args)
   {
 
      // Get oldAnnualSalary from user
      inputOldAnnualSalary();
      // oldAnnualSalary not valid
      if (oldAnnualSalary != 0)
      {
         // Get percentIncrease from user
         inputPercentIncrease();
         // percentIncrease not valid
         if (percentIncrease != 0)
         {
            if (canCompute())
            {
               // 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);
            }
         }
      }
   }
 
   static void inputOldAnnualSalary()
   {
      String returnValue;              // showInputDialog return value
      String prompt;                   // showInputDialog prompt argument
      String title;                    // showInputDialog title argument
      oldAnnualSalary = 0;             // 0 not valid or unchanged
      // 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 != null && !returnValue.equals(""))
      {
        
// Convert returnValue to float
         oldAnnualSalary = 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.");
            oldAnnualSalary = 0;       // Set to failed value
         }
      }
      else
      {
         System.out.println("Salary was not entered");
      }
  
   }
 
   static void inputPercentIncrease()
   {
      String returnValue;              // showInputDialog return value
      String prompt;                   // showInputDialog prompt argument
      String title;                    // showInputDialog title argument
      percentIncrease = 0;             // 0 not valid or unchanged
      // Prompt and get percentage increase
     
prompt = "Enter percent increase (.01 - 100) ex 10.5 for 10.5% : ";
      title  = "Attention";
      returnValue = JOptionPane.showInputDialog(null, prompt, title, msgTypeQ);
      // Percentage increase was entered - convert to float
      if (returnValue != null && !returnValue.equals(""))
      {
         // Convert returnValue to float
         percentIncrease = Float.parseFloat(returnValue);
         // precentIncrease is not 0.0 - 100.0
         if (percentIncrease < 0.01 || percentIncrease > 100.00)
         {
            System.out.println("Percent increase is not .01 to 100.");
            percentIncrease = 0;
         }
      }
      else
      {
         System.out.println("Percent increase was not entered.");  
      }
  
   }
 
   static boolean canCompute()
   {
      boolean isValid = true;          // Can compute state
      // Cannot compute
     
if ( oldAnnualSalary == 0 || percentIncrease == 0)
      {
         isValid = false;              // Cannot compute state
     
}
      return isValid;
   }
}
The if else is used in the inputOldAnnualSalary() and inputPercentIncrease() methods. The same code is used in both places.
if (returnValue != null && !returnValue.equals(""))
      {
Tests for true that the returnValue from the showInputDialog is not null and it is not equal to an empty string.
}
else
{
Tests for false that the returnValue from the showInputDialog is not null and it is not equal to an empty string. Said another way either returnValue is null or returnValue is an empty string.
 
Nesting and Methods
The use of multiple methods in this example reduced the nesting levels of if statements had all the code been in the main method. As the number of nesting levels increases, think of creating methods that handle parts of processing logic.