If...Else If Statements

An if…else if statement allows us to do differently things for several cases.

If…else if statement syntax

Here is the syntax for an if…else if statement:

if (condition1) 
{
    //first set of statements
}
else if (condition2) 
{
    //second set of statements
}
...
else 
{
    //last set of statements
}

In this structure, we first evaluate condition1. If it is true, then we execute the first set of statements and leave the if…else if statement. If condition1 is false, then we step down and evaluate condition2. If it is true, then we execute the second set of statements and move on in the program. If it is false, we move down to evaluate the next condition. This process continues until we run out of conditions to check. If none of the conditions were true, then the statements inside the else are executed.

We can have as many “else if” conditions as we want in this structure (which is denoted by the …). Furthermore, the else" portion is optional – we don’t have to have a special section that executes if none of the conditions were true.

If…else if example 1

For example, suppose we want to print out an age category for the user:

Scanner s = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = s.nextInt();

if (age <= 12) 
{
    System.out.println("You are a child");
}
else if (age < 19) 
{
    System.out.println("You are a teenager");
}
else 
{
    System.out.println("You are an adult");
}

In this example, exactly one of the statements will be printed, depending on the user’s age.

If…else if example 2

As another example, suppose you want to ask the user for 5 tests grades (out of 100) so that you can calculate their overall letter grade (90% and up is an A, 80-90% is a B, etc.) Here is a fragment of the program:

Scanner s = new Scanner(System.in);
System.out.print("Enter a test score: ");
int grade1 = s.nextInt();
System.out.print("Enter a test score: ");
int grade2 = s.nextInt();
System.out.print("Enter a test score: ");
int grade3 = s.nextInt();
System.out.print("Enter a test score: ");
int grade4 = s.nextInt();
System.out.print("Enter a test score: ");
int grade5 = s.nextInt();
double avg = (grade1+grade2+grade3+grade4+grade5)/5.0;

if (avg >= 90.0) 
{
    System.out.println("Letter grade: A");
}
else if (avg >= 80.0) 
{
    System.out.println("Letter grade: B");
}
else if (avg >= 70.0) 
{
    System.out.println("Letter grade: C");
}
else if (avg >= 60.0) 
{
    System.out.println("Letter grade: D");
}
else 
{
    System.out.println("Letter grade: F");
}

Error checking

Conditional statements are very useful in handling error conditions in programs. You can check to see if the user’s input is what you expected before performing any calculations. For example, in the grade calculator we just wrote, we expect the test scores to be between 0 and 100. As an error condition, we could add this check after getting all the user input:

if (grade1<0 || grade1>100 || grade2<0 || grade2>100 || grade3<0 ||
    grade3>100 || grade4<0 || grade4>100 || grade5<0 || grade5>100) 
{
    System.out.println("Invalid input");
}
else 
{
    //put the average calculation and the code to print the letter grade here
}

Now, a letter grade will only be printed if all test scores had appropriate values.