Case Statement

The case statement can be used to compare a single value to a set of other values.  It can not handle the concept of greater than or less then.

When pattern matching, an asterisk (*) means zero or more characters, brackets [  ] can be used to denote character sets, and a single pipe ( | )can be used as an “or”, and a question mark (?) denotes a single character.

case Statement Syntax

case Execution Steps

  1. The value of the variable is compared to the first test condition.
  2. If the variable matches the test condition,
    1. the code is executed
    2. and you are done.
  3. If the variable does not match the test condition, the value of the variable is compared to the next test condition.
  4. Repeat steps 2 and 3 until either a match is found or all the conditions are tested.
  5. If no test condition is matched, the code with the *) condition is executed.

Special Contructs && and ||

The constructs && and || are a short hand form of the if command, but should be used sparingly.

Example 1:

Example 2:

Example 3: