Next: , Previous: Operators in Conditionals, Up: Conditionals and Loops


5.20.2 if-else

gtroff has if-then-else constructs like other languages, although the formatting can be painful.

— Request: .if expr anything

Evaluate the expression expr, and executes anything (the remainder of the line) if expr evaluates to a value greater than zero (true). anything is interpreted as though it was on a line by itself (except that leading spaces are swallowed). See Operators in Conditionals, for more info.

          
          .nr xxx 1
          .nr yyy 2
          .if ((\n[xxx] == 1) & (\n[yyy] == 2)) true
              ⇒ true
— Request: .nop anything

Executes anything. This is similar to .if 1.

— Request: .ie expr anything
— Request: .el anything

Use the ie and el requests to write an if-then-else. The first request is the `if' part and the latter is the `else' part.

          
          .ie n .ls 2 \" double-spacing in nroff
          .el   .ls 1 \" single-spacing in troff
— Escape: \{
— Escape: \}

In many cases, an if (or if-else) construct needs to execute more than one request. This can be done using the \{ and \} escapes. The following example shows the possible ways to use these escapes (note the position of the opening and closing braces).

          
          .ie t \{\
          .    ds lq ``
          .    ds rq ''
          .\}
          .el \
          .\{\
          .    ds lq "
          .    ds rq "\}

See Expressions.