15.2 Ifdef

15.2.1 Synopsis

The simplest form of ifdef statement is:

ifdef (variable)
  statement;

A slightly more complex form is:

ifdef (variable)
  statement;
else
  alternate_statement;

while the more complete form is:

ifdef (variable)
  statement;
elifdef (variable_2)
  statement_2;

[ ... ]

elifdef (variable_n)
  statement_n;
else
  default_statement;

15.2.2 Description

With the former syntax, if a variable called variable has been defined, Cows will execute statement. With the second syntax, if a variable called variable has 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 defined the corresponding statement is executed and following clauses are skipped. If none of checked variables is 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: A variable containing an empty string (i.e. some_var = "") is considered defined by Cows.

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

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

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

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

ifdef (foo)
  print (x); print (y);
ifdef (foo)
  print (x);
  print (y);

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

ifdef (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:

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

The second form is much more readable.

15.2.3 Example

ifdef (some_variable) {
  print ("I have from an early age abjured the use of meat,");
  print ("and the time will come when men such as I will look upon");
  print ("the murder of animals as they now look upon the murder");
  print ("of men.");
  print ("-- Leonardo Da Vinci");
}

If some_variable has been defined Cows' output will be:

I have from an early age abjured the use of meat,
and the time will come when men such as I will look upon
the murder of animals as they now look upon the murder
of men.
-- Leonardo Da Vinci

Otherwise, there will be no output.

15.2.4 Example (2)

ifdef (some_variable) {
  print ("You have just dined, and however scrupulously the");
  print ("slaughterhouse is concealed to the graceful distance");
  print ("of miles, there is complicity.");
  print ("-- Ralph Waldo Emerson");
} else {
  print ("If slaughterhouses had glass walls, everyone");
  print ("would be vegetarian. We feel better about ourselves");
  print ("and better about the animals, knowing we're not");
  print ("contributing to their pain.");
  print ("-- Paul and Linda McCartney");
}

If some_variable has been defined Cows' output will be:

You have just dined, and however scrupulously the
slaughterhouse is concealed to the graceful distance
of miles, there is complicity.
-- Ralph Waldo Emerson

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

If slaughterhouses had glass walls, everyone
would be vegetarian. We feel better about ourselves
and better about the animals, knowing we're not
contributing to their pain.
-- Paul and Linda McCartney

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