The other conditional, ifelse, is much more powerful. It can be
used as a way to introduce a long comment, as an if-else construct, or
as a multibranch, depending on the number of arguments supplied:
ifelse(comment) ifelse(string-1, string-2, equal, opt not-equal) ifelse(string-1, string-2, equal, ...)
Used with only one argument, the ifelse simply discards it and
produces no output. This is a common m4 idiom for introducing a
block comment, as an alternative to repeatedly using dnl. This
special usage is recognized by GNU m4, so that in this case, the
warning about missing arguments is never triggered.
If called with three or four arguments, ifelse expands into
equal, if string-1 and string-2 are equal (character
for character), otherwise it expands to not-equal.
ifelse(foo, bar, `true') => ifelse(foo, foo, `true') =>true ifelse(foo, bar, `true', `false') =>false ifelse(foo, foo, `true', `false') =>true
However, ifelse can take more than four arguments. If given more
than four arguments, ifelse works like a case or switch
statement in traditional programming languages. If string-1 and
string-2 are equal, ifelse expands into equal, otherwise
the procedure is repeated with the first three arguments discarded. This
calls for an example:
ifelse(foo, bar, `third', gnu, gnats, `sixth', `seventh') =>seventh
Naturally, the normal case will be slightly more advanced than these
examples. A common use of ifelse is in macros implementing loops
of various kinds.
The macro ifelse is recognized only with parameters.
Go to the first, previous, next, last section, table of contents.