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

6

23

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?

Bathyal answered 24/2, 2013 at 21:33 Comment(10)
| is bitwise or not logical in most languagePlainsman
Yeah, why don't all languages have a keyword for it? (Like or)Scarecrow
I feels || as logical OR was came from | bitwise ORPlainsman
In SQL it's the string concatenation operator.Offering
@Chiel92 or came latter in new programming languages. I can only hypothesize that or introduced because people confuse between | and || and do errors in writing code As Erty did in his Question now he has corrected.Plainsman
Yes, when you think about it, in && it is much more obvious why & was chosen.Crockett
@Chiel92 the IBM note also says: The logical OR (||) should not be confused with the bitwise OR (|) operatorPlainsman
Why not? There were only so many characters on the 026 keypunch keyboard and the ASR33 TTY.Giffie
(It should be noted that the code position occupied by the | character in both ASCII and EBCDIC was often presented differently. Quite common was a split vertical bar, and I'm recalling a few other "squiggle"-like characters. But there really weren't any other characters left, unless you went the APL route.)Giffie
If only there were more "on topic" questions as worthy and interesting as this supposedly "off topic" one!Tavie
C
43

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).

Chuckwalla answered 24/2, 2013 at 22:3 Comment(11)
The original notation for BNF used the mathematical or-symbol "v": home.comcast.net/~jhorning4/backus-proposed-ial.pdfNimesh
@eznme updated the answer. Apparently the vertical line came with ALGOL 60, not ALGOL 58.Chuckwalla
Where did you get that A60 had | operator? I cannot find that even 68 had them.Nimesh
@eznme added a cite, but it's unfortunately for purchase only.Chuckwalla
if there were a | or-operator in algol 58, 60, 68 there should be some public information about it.Nimesh
in fact i am certain that all algol's just use the word "or" instead of an or-operator.Nimesh
@eznme added a scan of the 1964 article that uses the vertical line and refers to it as coming from the 1960 modifications.Chuckwalla
@eznme "grammar alternative" is a strange phrase here, but yes, BNF used the vertical line to represent a choice, as my entry stated. By the way, you're right about the original or symbol being an "or" with a line over it. Updated the answer to reflect that.Chuckwalla
+1 you are putting a lot of effort into this.Nimesh
On a closed question, no less. :) Even paid the $15 to see that article.Chuckwalla
So it appears that Naur revised Backus' notation sometime between 1958 and 1960 while drafting the ALGOL 60 syntax description, substituting | for the word "or" with the horizantal bar over it. Here's a link to the Algol 60 syntax. csci.csusb.edu/dick/samples/… The logical OR operator in the ALGOL 60 language is the word "or", but the vertical bar was used in the metalanguage that was used to specify ALGOL 60, and it appears to have taken hold in other languages from there.Terrorstricken
N
3

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.
Nimesh answered 24/2, 2013 at 22:4 Comment(2)
I think the BNF answers (1950s) beat the BCPL answers (1966) chronologically.Chuckwalla
I was writing in BCPL from 1972-75, and I'm pretty sure (but relying on memory) that the compiler I used represented the logical or operator as \/.Blanks
M
2

From: http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

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.

Monday answered 24/2, 2013 at 21:46 Comment(2)
Implicit follow up question: why | for the "first" or?Vigue
That's a question for BCPL historians most likely. Good luck in finding them. :)Monday
H
1

The oldest usage of an | as or, I could find after googling a while was in the Backus Naur Form.

Which seems to have first appeared in the paper The syntax and semantics of the proposed international algebraic language of the Zuerich ACM-GRAMM conference by J.W. Backus in 1959, however he did not use the | notation there. Instead he used an overlined "or".

Then in 1960 he wrote the Report on the algorithmic language ALGOL 60 , which already used the |-notation.

So he invented it somewhere between 1959 and 1960.

Haymaker answered 24/2, 2013 at 22:10 Comment(1)
The original notation for BNF used the mathematical or-symbol "v": home.comcast.net/~jhorning4/backus-proposed-ial.pdfNimesh
T
0

The use of the vertical bar to express an "or" operation (|| for logical, or | for bit-wise) may have sprung from BNF. BNF is not a programming language, but it was developed in the late 1950s as a way to create a formal specification for programming language syntax. The vertical bar character in BNF indicates a choice between valid expansions for nonterminal symbol. It's highly likely that programming language designers in the '60s and later years were familiar with this notation and were influenced by it.

Terrorstricken answered 24/2, 2013 at 21:53 Comment(2)
The original notation for BNF used the mathematical or-symbol "v": home.comcast.net/~jhorning4/backus-proposed-ial.pdfNimesh
Interesting. I didn't know that. Thanks.Terrorstricken
S
-3

The logical OR || symbol actually came about from a splitting of the + plus symbol. The horizontal bar was rotated 90 degrees and thus was the birth of the logical OR || symbol. You may or may not find this documented within the internet because it happened so long ago. For the bitwise OR | , (you guessed it) the crossbar was removed from the + plus symbol.

Sicanian answered 24/2, 2013 at 21:47 Comment(5)
And what's the rational for or == +?Vigue
This sounds a bit apocryphal. Let me guess the next one: "For the letter A, two vertical posts were tilted toward each other. A lateral bar was added for structural support."Chuckwalla
To the downvoter(s) - lighten up - this was not a serious update. As you can see from the admim closure and CameronFredman comment, this kind of question should not be posted in SE.Sicanian
@CameronFredman +1 for being alert...Sicanian
@deceze: That's an easy one. If you use 0 to represent false and any number except 0 for true, you get the truth tables "0+0 = 0", "0+1=1", "1+0 = 1", "1+1=2", ..., so + is not only an notation for "or", but actually describes what the operation does. Similarly, "0*0 = 0", "0*1=0", "1*0 = 0", "1*1=1", so * is the natural symbol for "and"Haymaker

© 2022 - 2024 — McMap. All rights reserved.