15.4 Ifndef

15.4.1 Synopsis

The simplest form of ifndef statement is:

ifndef (variable)
  statement;

A slightly more complex form is:

ifndef (variable)
  statement;
else
  alternate_statement;

while the more complete form is:

ifndef (variable)
  statement;
elifndef (variable_2)
  statement_2;

[ ... ]

elifndef (variable_n)
  statement_n;
else
  default_statement;

15.4.2 Description

With the former syntax, if a variable called variable has not been defined, Cows will execute statement. With the latter syntax, if a variable called variable has not been defined, Cows will execute the statement; otherwise, it will execute the alternate statement.

Finally, the last syntax allows to choose among an arbitrary number of cases. Variables are checked top to bottom; when a variable is found to be undefined the corresponding statement is executed and following clauses are skipped. If all checked variables are defined, the else statement (if present) is executed.

Variable can also be supplied with the $ function (see Section 9.6); this gives high flexibility.

Important: Cows considers an empty variable (i.e. some_var = "") defined.

Note: you can also test the existence of an array variable (ifndef ( array [ ] ) ... ) but you can't test the existence of an array element (ifndef ( array [3] ) ... ).

You can also execute more than one statement under a certain condition: simply enclose them between braces:

infdef (variable) {
  statement;
  statement;
} else {
  alternate_statement;
  alternate_statement;
}

Remember that whitespaces don't affect Cows' behavior so the following forms are misleading:

ifndef (foo)
  print (x); print (y);
ifndef (foo)
  print (x);
  print (y);

These forms suggest that both x and y are printed only if foo is not defined but Cows doesn't care about formatting so they are equivalent to:

ifndef (foo)
  print (x);
print (y);

y is printed anyway, regardless to foo but that's not what you probably wanted; correct ways to write the code above are:

ifndef (foo) {
  print (x); print (y);
}
ifndef (foo) {
  print (x);
  print (y);
}

The second form is much more readable.

15.4.3 Example

ifndef (some_variable) {
  print ("The human commitment to harmony, justice, peace, and");
  print ("love is ironic as long as we continue to support the");
  print ("suffering and shame of the slaughterhouse and its satellite");
  print ("operations.");
  print ("-- Karen Davis, PhD");
}

If some_variable has not been defined Cows' output will be:

The human commitment to harmony, justice, peace, and
love is ironic as long as we continue to support the
suffering and shame of the slaughterhouse and its satellite
operations.
-- Karen Davis, PhD

Otherwise, there will be no output.

15.4.4 Example (2)

ifndef (some_variable) {
  print ("There will be no justice as long as man will stand with");
  print ("a knife or with a gun and destroy those who are weaker than");
  print ("he is.");
  print ("-- Isaac Singer");
} else {
  print ("For as long as men massacre animals, they will kill");
  print ("each other. Indeed, he who sows the seed of murder and");
  print ("pain cannot reap joy and love.");
  print ("-- Pythagoras (6th century BC)");
}

If some_variable has not been defined Cows' output will be:

There will be no justice as long as man will stand with
a knife or with a gun and destroy those who are weaker than
he is.
-- Isaac Singer

Otherwise, if some_variable has been defined, Cows' output will be:

For as long as men massacre animals, they will kill
each other. Indeed, he who sows the seed of murder and
pain cannot reap joy and love.
-- Pythagoras (6th century BC)

This manual can be downloaded from http://www.g-cows.org/.