The logical OR operator of C, which has propagated itself to several other programming languages written in or inspired by C: C++, Java, awk, Perl. It combines two boolean expressions and returns true if either is true; false otherwise. A typical use of || is in an if statement:

if (expr1 || expr2)
   do_something();

The || operator is alawys a short-circuit logical operator: If the first expression returns true, the second expression is never evaluated. Since || is guaranteed to have a sequence point after the first expression is evaluated, this is one of the few places where you can rely on the order that sub-expressions of other expressions. There are a few caveats, however. In the following expression:

baz = (static_cast<int>(foo()||bar()) -0.5) * weight();

you can guarantee that foo() will be called before bar(), which will be called before the assignment to baz, but there is no telling when weight() will be called relative to foo and bar. Also,

if (1 || a = a = c)

is still undefined behavior.
In the various forms of the *n?x shell (and thus in Perl command substitution), || has a somewhat different meaning. || is a sort of cryptic alias for 'if not' or else, combining two shell pipelines. The opposite of shell &&, the second pipeline will be executed only if the result code of the first pipeline is non-zero. Thus,

false && echo 'yes' || echo 'no'

will result in

no

Since, by convention, a program returning a result code of 0 indicates that a program succeeded, || is a way of catching the failure of a program to take an action. Thus, || can also be pronounced "fails".

Log in or register to write something here or to contact authors.