If/Then/Else If Versus Switches

Let’s examine the difference between using If/Then/Else If statements and Switch statements on a basic menu control object.

We are working with the structure of a previously built 4-function calculator that was constructed with If/Then/Else If statements similar to the following:

//Program Name: 4-Function Calculator
//Programmer Name: Your Name
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class calculator{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Basic Calculator");
        System.out.println("1 - Addition");
        System.out.println("2 - Subtraction");
        System.out.println("3 - Multiplication");
        System.out.println("4 - Division");
        System.out.println("");
        System.out.print("Enter Your Selection_");
        int menu = menuChoice.nextInt();
        
        if (menu == 1){
            //Code for Menu Option 1
        }else if (menu == 2){
            //Code for Menu Option 2
        }else if (menu == 3){
            //Code for Menu Option 3
        }else if (menu == 4){
            //Code for Menu Option 4
        }else{
           //Code if Other Integer Option is Selected
        }
    }
}

Let’s compare the structure of the above with that of a Switch statement similar to the following:

//Program Name: 4-Function Calculator (Switches)
//Programmer Name: Your Name
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class calculator_switches{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Basic Calculator");
        System.out.println("1 - Addition");
        System.out.println("2 - Subtraction");
        System.out.println("3 - Multiplication");
        System.out.println("4 - Division");
        System.out.println("");
        System.out.print("Enter Your Selection_");
        int menu = menuChoice.nextInt();
        
        switch (menu) {
            case 1:
                {
                    //Code for Menu Option 1
                    break;
                }
            case 2:
                {
                    //Code for Menu Option 2
                    break;
                }
            case 3:
                {
                    //Code for Menu Option 3
                    break;
                }
            case 4:
                {
                    //Code for Menu Option 4
                    break;
                }
            default:
                //Code if Something Different is Entered
                break;
        }
    }
}

Structurally, they both look about the same. Some of the first observations made are similar to “The Switch code is longer.” or “The Switch code has ‘break’ in it a lot.” or finally “I’m going to be thrown off that I don’t need curly braces in the default case of a Switch.”. All of these are good valid observations and concerns. Now, let’s look at why we even consider using the Switch construct.

Why Do We Care?

The primary reason we care is in the general processing speed of the application. When using an If/Then/Else If statement, the application must process each case until it finds the condition that is met. Once that condition is met, the conditional processing stops.

In contrast, a Switch statement creates a jump table that automatically jumps to the correct segment of code to be executed.

When Should I Use What?

If you have a construct that has two different pathways plus a default escape pathway, then an If/Then/Else If is typically considered the best choice.

Example – If/Then/Else If

if (menu == 1){
            //Code for Menu Option 1
        }else if (menu == 2){
            //Code for Menu Option 2
        }else{
            //Code for Items Other Than Menu Options 1 or 2
        }

However, anything beyond two different pathways plus a default escape pathway is better suited by a Switch statement.

Example – Switch

switch (menu) {
            case 1: {
                    //Code for Menu Option 1
                    break;
                }
            case 2: {
                    //Code for Menu Option 2
                    break;
                }
            case 3: {
                    //Code for Menu Option 3
                    break;
                }
            case 4: {
                    //Code for Menu Option 4
                    break;
                }
            default:
                //Code if Something Different is Entered
                break;
        }

 Which is More Efficient?

This is a bit of a loaded question. Some would say that efficiency is defined by how much typing and therefore “coding” is done on the front-end. In this case, the If/Then/Else If code at the top of this post has 755 typed characters while the Switch code has 787 typed characters. By this definition, the Switch code is 95% as efficient as the If/Then/Else If code.

If we define efficiency as speed at which the application runs, it depends on what data is input into the application at runtime. For example, if in the If/Then/Else If the user enters menu choice 1, the application processing time will likely be equal to or slightly faster than that of a Switch since menu choice 1 was the first condition to be checked.

However, if menu choice 4 is selected in the If/Then/Else If code, the application processing time will be longer than that of the Switch code since the conditional statement of menu choices 1, 2, and 3 had to be checked first since they were above the 4th menu choice in the code.