...

/

if and case Statements

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 ...