GNU Info

Info Node: (m4.info)Ifelse

(m4.info)Ifelse


Next: Loops Prev: Ifdef Up: Conditionals
Enter node , (file) or (file)node

Comparing strings
=================

   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.


automatically generated by info2www version 1.2.2.9