else

The else command starts the branch of an if or a when that is taken when every test before it was false.

Syntax

else

else takes no arguments and stands alone on its line.

What it does

The same word serves both families. Inside an if construct the command parser reads it. Inside a when construct the preprocessor reads it. James picks the family by whichever construct is open. The branch after else is read only when no earlier branch of the construct was taken.

When James applies it

By whichever family opened it: the command parser for an if, the preprocessor for a when. It is legal in any phase: Control, Part or Merge.

Rules James adds

  • A branch that cannot be taken is skipped without checking that else stands alone, so extra text on a skipped else line is dropped with its branch.
  • In a for body, an else with no if or when of its own is an error on every pass.
  • The rule for an if is the one in if, and the rule for a when is the one in when.

Diagnostics

  • E0315: there is no open if or when, or a second else follows one on the same construct.
  • E0309: the keyword shares its line with other input.

Example

The script tests/corpus/113-elsewhen-selects-branch/input.tg, which make test runs:

para x1 2;
para x2 5;
block
when(%x1.gt.%x2) then
1 3;1 3;1 3;
elsewhen(%x1.lt.%x2) then
1 2;1 2;1 2;
else
1 4;1 4;1 4;
endwhen
1 2 1 2 1 2
endpart

This else belongs to a when. x1 is 2 and x2 is 5, so the elsewhen test is true and the else branch is skipped. The prep dump shows 1 2;1 2;1 2; as the block’s index lists, with the two other branches gone.

See also

  • if: the command parser’s family.
  • when: the preprocessor’s family.
  • elseif, elsewhen: the branches with a test.

James 0.3.1.

This site uses Just the Docs, a documentation theme for Jekyll.