Following on from my posts on Arithmetic and Assignment operators, Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Using ColdFusion, the Logical operators can be expressed as follows:

Logical AND

In the below example, both operands, x and y, return true, therefore myVariable is set to true within the logical statement.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (x < 10 && y > 1)
{
	myVariable = true;
}
</cfscript>

Logical AND Operator Example Results

Logical OR

In the below example, neither x equals 10 nor y equals 1, therefore the first part of the logical statement is not resolved. Instead, the else statement is processed, assigning the boolean value false to the variable myVariable.

<cfscript>
x = 9;
y = 2;
 
if (x == 10 || y == 1)
{
	myVariable = true;
}
else
{
	myVariable = false;
}
</cfscript>

Logical OR Operator Example Results

Logical NOT

In the below example, x does not equal y. Ordinarily this would result in the logical statement being evaluated, however, a logical negation has been applied to the beginning of the expression to be evaluated, therefore the variable myVariable is assigned the boolean value true.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (!x==y)
{
	myVariable = true;
}
</cfscript>

Logical NOT Operator Example Results

Unintended consequences are situations where an action results in an outcome that is not (or not only) that which was intended. The unintended results may be foreseen or unforeseen, but they should are the logical or likely results of the action.

Possible causes of unintended consequences include the world’s inherent complexity (parts of a system responding to changes in the environment), perverse incentives, human stupidity, self-deception or other cognitive or emotional biases.

In the twentieth century, sociologist Robert K. Merton popularised the concept, sometimes referred to as the Law of Unforeseen Consequences. Merton listed five causes of unanticipated consequences:

  1. Ignorance. It is impossible to anticipate everything, thereby leading to incomplete analysis.
  2. Error. Incorrect analysis of the problem, or following habits that worked in the past but may not apply to the current situation.
  3. Immediate interest, which may override long-term interests.
  4. Basic values may require or prohibit certain actions, even if the long-term result might be unfavorable. These long-term consequences may eventually cause changes in basic values.
  5. Self-defeating prophecy. Fear of some consequence drives people to find solutions before the problem occurs, thus the non-occurrence of the problem is unanticipated.

Of course, unintended consequences are not only common in everyday life, but also features, amongst a multitude of others, the World Wide Web (’the Web’).

Many users know that Sir Tim Berners-Lee developed the web at the CERN physics laboratory near Geneva. In August 1991, Berners-Lee created the first website as a means by which physicists trawl through the 200,000 abstracts more easily than ever before. But, though physicists were being won over by the web’s promise, in the early years few others grasped its potential.

Not even Berners-Lee could have forseen how the Web was to take off and come to dominate the world. The original concept was for a medium that people both read and contributed to, a rewritable web. New tools such as photo-sharing sites, social networks, blogs, wikis and others are making good on that early promise.

The journey has not been with out its problems. The browser wars threatened the very existence of, and access to, the Web. The establishment of the World Wide Web Consortium (W3C) has become a strong ‘directional’ force, but government regulation and the threat over net neutrality are a significant stumbling block. The free (relatively speaking) ubiquitous platform of the Web, where anyone can publish content, create a business opportunity and make money has also promoted the unintended consequence of being a great distribution platform for viruses, spam and porn; the bane of every ‘connected’ user’s existence.

The web may be worldwide but it is only just getting started.