if and case Statements

Learn the difference between if and case statements in detail. Also, learn about the code duplication problem in the case statement blocks.

Comparing if and case statements

Let’s compare the if and case statements in the if scriptifsmt and case script.casesmt First, we write them in a general form. Here is the result for the if statement:

if CONDITION_1
then
  ACTION_1
elif CONDITION_2
then
  ACTION_2
elif CONDITION_3
then
  ACTION_3
else
  ACTION_4
fi

The case statement looks this way:

case STRING in
  PATTERN_1)
    ACTION_1
     ;;

  PATTERN_2)
    ACTION_2
    ;;

  PATTERN_3)
    ACTION_3
    ;;

  PATTERN_4)
    ACTION_4
    ;;
esac

The differences between the constructs are evident now. First, the if condition checks the results of a boolean expression. The case statement compares a string with several patterns. Therefore, it makes no sense to pass a boolean expression to the case condition. Doing that, we handle two cases only: when the expression is true or false. The if statement is more convenient for such checking.

The second difference between if and case is the number of conditions. Each branch of the if statement evaluates an individual boolean expression. They are independent of each other, in general. The expressions check the same variable in our example, but this is a particular case. The case statement checks one string that we pass to it.

The if and case statements are fundamentally different. We cannot exchange one for the other in our code. We use an appropriate statement depending on the nature of our checking. The following questions will help us to make the right choice:

  • How many conditions should we check? Use if for checking several conditions.
  • Would it be enough to check one string only? Use case when the answer is yes.
  • Do we need compound boolean expressions? Use if when the answer is yes.

Delimiters between the code blocks

When we use the case statement, we can apply one of two possible delimiters between the code blocks:

  1. Two semicolons ;;.
  2. Semicolons and ampersand ;&.

The ampersand delimiter is allowed in Bash, but is not part of the POSIX standard. When Bash meets this delimiter, it executes the following code block without checking its pattern. It can be useful when we want to execute an algorithm from a specific step. Also, the ampersand delimiter allows us to avoid code duplication in some cases.

Code duplication problem

Let’s illustrate the code duplication problem. Let’s suppose that we have the script that archives PDF documents and copies the resulting file. An input parameter of the script chooses an action to do. For example, the -a option means archiving, and -c means copying. The script should always copy the archiving result. We get code duplication in this case.

The script1.sh shows the archiving script. The case statement there has two cp calls that are the same.

We can avoid code duplication by adding the ;& separator between the -a and -c code blocks. script2.sh shows the changed script.

Click the “Run” button and then execute the files.

Get hands-on with 1400+ tech skills courses.