Cloudbook: C

  1. Home
  2. Control
  3. If/Else
  4. § 4 exercise →
If/Else

In Perl elsif is a word. It means "else-if" and is conveninent for dividng conditions into mutually exclusive categories, each with an associated code block.

Instead of nesting an if/else statement inside an else clause, you can use an elsif clause. An elsif clause follow either a if statement, or another elsif clause, and has a controlling logical expression and a code block.

The logical expression is evaluated, and the code block is invoked if the logical expression evaluates true, but only if all previous logical expressions were false.

Typically, you start with an if, and add elsif clauses, as many as you like, and end optionally with an else clause. In this format, exactly one of the code blocks will run. It will be the code block of the first logical expression that evaluates true, trying them in order. If none are true, and there is a final else clause, the else block is run.