else
{
// Line or lines of code selected if all previous
// test expressions are false.
// Skipped if any previous test expressions are true
________________________;
________________________;
________________________;
}
You need to provide the red values. The black items are always included.
This works like the if else if structure. The difference is that if all test expressions are false, the else block is processed.
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 if and else statement without and with curly braces. Without curly braces is considered a shortcut when only the next line is controlled. Generally you should indent the single line or place it on the same line as the if, else if or else statement with the former preferred. Here is an example where the if, the else and else if 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 if ( test expression )
____________________; // The single line of code
// controlled by the else if statement
// skipped if previous test
// expressions are true
// DO NOT PUT CODE HERE - COMPILER ERROR!
else if ( test expression )
____________________; // The single line of code
// controlled by the else if statement
// skipped if previous test
// expressions are true
// 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 an before the else if 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 last else if 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 if 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.
import javax.swing.*;
public class Tester
{
// Fields (Always static in main method of starting class)
static float annualSalary; // Current annual salary
// .01 - 1000000.00
// 0 not valid or unchanged
public static void main(String[]args)
{
float percentIncrease; // Set based on salary ranges
// -0 not valid or unchangedd
// .05 0.01 - 49999.00
// .045 50000.00 - 74999.00
// .0425 75000.00 - 99999.00
// .04 100000.00 - 499999.00
// .06 500000.00 - 1000000.00
// Prompt and get annualSalary
inputAnnualSalary();
// annualSalary is from .01 to 49999.0
if (annualSalary >= 0.01 && annualSalary <= 49999.0)
{
percentIncrease = 0.05f;
}
// annualSalary is from 50000.0 to 74999.0
else if (annualSalary >= 50000.0 && annualSalary < 75000.0)
{
percentIncrease = 0.045f;
}
// annualSalary is from 75000.0 to 99999.0
else if (annualSalary >= 75000.0 && annualSalary < 100000.0)
{
percentIncrease = 0.0425f;
}
// annualSalary is from 100000.0 to 499999.0
else if (annualSalary >= 100000.0 && annualSalary < 500000.0)
{
percentIncrease = 0.04f;
}
// annualSalary is from 500000.0 to 1000000
else if (annualSalary >= 500000.0 && annualSalary <= 1000000.0)
{
percentIncrease = 0.06f;
}
else
{
percentIncrease = 0;
}
// A salary .01 - 1000000 was entered
if (percentIncrease != 0)
{
// Display old salary
System.out.println("Your old salary is " + annualSalary );
// Compute new salary
annualSalary *= 1.0 + percentIncrease ;
// Display new percentage change a whole percent and new salary
System.out.println("Percent increase is " + ( percentIncrease * 100 )
+ "%");
System.out.println("Your new salary is " + annualSalary );
}
}
static void inputAnnualSalary()
{
String returnValue; // showInputDialog return value
String prompt; // showInputDialog prompt argument
String title; // showInputDialog title argument
int msgTypeQ =
JOptionPane.QUESTION_MESSAGE; // showInputDialog message type
// in variable to shorten code
annualSalary = 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
annualSalary = Float.parseFloat(returnValue);
// annualSalary is not from .01 to 1000000.00
if ( annualSalary < 0.01 || annualSalary > 1000000.00 )
{
System.out.println("Salary entered was not from 0.01 to 1000000.00.");
annualSalary = 0; // Set to failed value
}
}
// Nothing entered
else
{
System.out.println("Salary was not entered");
}
}
}
The first if test checks for the first annual salary range of .01 to 49999.0. Each successive else if checks for another range. The first one that becomes true, the lines inside that if or else if block are processed and all the others are skipped.
If all the test expressions are false, then the else block takes over. In this case it sets the percentIncrease variable to 0 to flag the remainder of the program that the salary value was incorrect. This is used at the end of the program for an if selection to produce the salary report if precentIncrease is not 0.