Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "||" the symbol for or? [closed]

Tags:

I know that || represents the logical operation "or", but I'm curious if anyone knows the history of choosing that symbol. Was it just because it happened to be an unused symbol on the keyboard?

like image 981
Erty Seidohl Avatar asked Feb 24 '13 21:02

Erty Seidohl


People also ask

What does || mean in coding?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical OR has left-to-right associativity.

Which symbol is used for logical or operation?

The logical OR operation is represented by a plus sign (+). Certain facts involving negation, the OR operation, and the AND operation have been proven in Boolean algebra, and are known as DeMorgan's Laws.


2 Answers

Origins of the single vertical line "|" as indicating the disjunctive "or".

From ASCII character history:

It has been conjectured that the vertical line character was introduced to the area of computing with the Backus-Naur Form metalanguage for describing programming languages. It was also taken into use in APL in early 1960's and incorporated into PL/I at about the same time as the OR operator and, doubled, as the concatenation operator.

John Warner Backus (December 3, 1924 – March 17, 2007) was an American computer scientist. He directed the team that invented the first widely used high-level programming language (FORTRAN) and was the inventor of the Backus-Naur form (BNF), the almost universally used notation to define formal language syntax. He also did research in function-level programming and helped to popularize it.

A programming language designer at IBM, he proposed "metalinguistic formulas" to describe the syntax of the new programming language IAL, known today as ALGOL 58, using the BNF notation.

In Backus-Naur form, an expression consists of sequences of symbols and/or sequences separated by '|', indicating a choice, the whole being a possible substitution for the symbol on the left.

<personal-name> ::= <name> | <initial>

In the ALGOL 58 specification, Backus did not originally use the vertical line; He used the word "or" with a line over it (as well as the logic symbol OR symbol). Peter Naur, a Danish computer scientist who contributed to ALGOL 60, modified several symbols to those that could be typed at a standard keyboard. Among his changes was the addition of the vertical line. (Source: "History of Programming Languages", Richard L. Wexelblat)

Even after the ALGOL 60 specification, however, there are plenty of examples of the OR symbol symbol still being used with ALGOL. In fact, in 1961 the ASCII character set got its backslash added so ALGOL's logical operators could be typed with slashes, like this: \/ /\ The symbols can be found on the IBM 2741 keyboard, available in the mid sixties:

IBM Keyboard

But there's clear evidence that Naur added the vertical line to ALGOL 60. In an article published in 1964, (Knuth D., “Backus Normal Form vs. Backus Naur Form” Letters to the Editor, Communications of the ACM, Vol. 7 (1964). pp. 735-736, available here), Donald Knuth argued that "Backus Normal Form" should be referred to as "Backus Naur Form" due to Naur's contributions. Among the contributions he referred to was the addition of the vertical line as an or operator.

Here's an image of a portion of the 1964 article. Note on the right side, the bulleted item (iv).

enter image description here

The article goes on to describe Naur as having been responsible for that semantic change as part of his editing responsibilities for the 1960 Algol report:

enter image description here

Origins of the double vertical line "||"

In The Development of the C Language, Dennis M. Ritchie describes why the double vertical line operator was added:

Rapid changes continued after the language had been named, for example the introduction of the && and || operators. In BCPL and B, the evaluation of expressions depends on context: within if and other conditional statements that compare an expression's value with zero, these languages place a special interpretation on the and (&) and or (|) operators. In ordinary contexts, they operate bitwise, but in the B statement

if (e1 & e2) ...

the compiler must evaluate e1 and if it is non-zero, evaluate e2, and if it too is non-zero, elaborate the statement dependent on the if. The requirement descends recursively on & and | operators within e1 and e2. The short-circuit semantics of the Boolean operators in such `truth-value' context seemed desirable, but the overloading of the operators was difficult to explain and use. At the suggestion of Alan Snyder, I introduced the && and || operators to make the mechanism more explicit.

(Thanks to Richard Brown for this section's research).

like image 151
Cameron Fredman Avatar answered Sep 23 '22 19:09

Cameron Fredman


I believe it was BCPL (wp) which introduced it. Its predecessor CPL had the symbol | but used it as an alternate til-end-of-line comment.

I have found no evidence that the reason was ever written up anywhere, but we can think about whether it was the only choice (which would have made a good reason):

They did not choose the word "or" because the concept of context-free grammars (wp) was becoming very popular in language design, so they chose something that could not be a valid symbol (symbols being defined as [a-zA-Z_]+[a-zA-Z0-9_]* or additionally with '-' typically).

So they looked at all the characters that were defined in previously defined standards. (Keep in mind that the character had to already exist in a standard so they could not just decide and paint their own. ) They looked especially hard at the 3-year-old ASCII but also at EBCDIC. They found that there was not much too choose from:

  • aside from the alphabet, underscore, numbers and space there were:
  • NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US which were all used as terminal-control characters (and unprintable).
  • "" ( ) { } [ ] were semantically unfit, as was & (been used as 'and' for >100 years)
  • all of these were used ALGOL and/or CPL: ! # $ % ' * + , - . / : ; < = > ? @ \ ^ _ ~
  • so what remained were ` and | but the backtick could not be used because it was not available everywhere even where claimed ascii-compliant.
like image 37
Bernd Elkemann Avatar answered Sep 21 '22 19:09

Bernd Elkemann