11.4 Logical Operators

Logical operators are often used to check for multiple conditions.

11.4.1 And (&&)

Applied to boolean operands, the && operator returns true if both operands are true; it returns false if one or both operands are false.

You can also evaluate the logical And of number or strings; there are two ways to think about this kind of operations.

The easy way: an empty string, the number 0 and an undefined variable are considered false. Non empty strings and and numbers other than 0 are considered true.

5 && "foo"

1 (true)

5 && ""

0 (false)

Cows' real behavior is slightly more complicated: if the first operand is false, the && operator returns its value. Otherwise, it evaluates the second operand and returns its value.

5 && "foo"

"foo"

0 && "foo"

0

However, if you use the && operator in a context where a boolean is required, foo and 0 are automatically converted in true and false respectively so you can safely assume that expressions like 5 && "foo" returns true.

In other words: when applied to data types other than booleans the && operator returns more informations than a simple true or false. Usually you won't need this extra informations, and you'll safely forget about them relying on the automatic type conversion performed by Cows.

An example of how to use this extra information:

var_status = var && "Var is defined and not null";

Here, if var is undefined or null, var_status is left empty; otherwise, it will store the string Var is defined and not null (regardless of var's value).

11.4.2 Or (||)

Applied to boolean operands, the Or operator returns true if one or both operands are true; it returns false if both operands are false.

You can also evaluate the logical Or of number or strings; there are two ways to think about this kind of operations.

The easy way: an empty string, the number 0 and an undefined variable are considered false. Non empty strings and and numbers other than 0 are considered true.

5 || "foo"

1 (true)

5 || ""

1 (true)

0 || ""

0 (false)

Cows' real behavior is slightly more complicated: if the first operand is true, the || operator returns its value. Otherwise, it evaluates the second operand and returns its value.

0 || ""

""

"" || 0

0

"foo" || 5

foo

0 || "foo"

foo

However, if you use the || operator in a context where a boolean is required, empty string and 0 are automatically converted to false and foo is automatically converted in true so you can safely assume that expressions like "foo" || 5 returns true.

In other words: when applied to data types other than booleans the || operator returns more informations than a simple true or false. Usually you won't need this extra informations, and you'll safely forget about them relying on the automatic type conversion performed by Cows.

An example of how to use this extra information:

user = name || "Guest";

If a variable called name exists and is not empty, its value is stored in variable user; otherwise default string Guest is used.

11.4.3 Not (!)

The ! operator inverts a boolean expression; when applied to integers or strings Cows performs the automatic type conversion and then inverts the result.

! 5;

0 (false)

! "foo";

0 (false)

! 0;

1 (true)

! ""

1 (true)

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