10

The if keyword is so prevalent in programming that it seems to just be part of it. However, with an integer value and a goto statement, one cansimulate the functionality of an if.

Which means it's not necessary for a language to have dedicated conditional keywords in order to achieve that functionality. Which in turn makes me think that there must have been a time before somebody created the first if to be used in a language.

So, my question is: What was the first time a language offered a dedicated keyword for conditional execution?

The word itself doesn't really matter here, it could be AS_IT_OCCURS_THAT instead of if for all I care.

migrated from softwareengineering.stackexchange.com Apr 18 at 14:19

This question came from our site for professionals, academics, and students working within the systems development life cycle.

  • 4
    "However, with an integer value and a goto statement"? I'm not seeing the conditional here. Are you assuming a "Jump if Zero" (or equivalent) machine instruction for the goto? – Caleth Apr 18 at 12:18
  • 1
    @Caleth No, I mean jumping to different line numbers. – R. Schmitz Apr 18 at 12:21
  • 1
    @HighPerformanceMark I don't know LISP history, so this might be a later addition, but it seems to have a literal if - so yeah, that absolutely counts for this question! (But it's not the correct answer because it's already older than FORTRAN.) – R. Schmitz Apr 18 at 12:27
  • 4
    The original FORTRAN had IF (value) 10, 20, 30 which would branch to statement with label 10 if value was negative, 20 if zero, and 30 if positive. – BobDalgleish Apr 18 at 13:41
  • 3
    "Computed goto" is actually a later development than conditional branching. – Mark Apr 18 at 20:50
18

The Analytical Engine designed in 1837 was capable of conditional branches

From the wiki (emphasis mine):

The programming language to be employed by users was akin to modern day assembly languages. Loops and conditional branching were possible, and so the language as conceived would have been Turing-complete as later defined by Alan Turing.

The wiki later provides an example of the conditional being executed (with the code represented on cards):

For example, a factorial program would be written as:

N0 6 
N1 1 
N2 1 
× 
L1 
L0 
S1
- 
L0 
L2 
S0 
L2 
L0 
CB?11 

where the CB is the conditional branch instruction or "combination card" used to make the control flow jump, in this case backwards by 11 cards.

Ada Lovelace may very well be the first to have written a program for this machine.

Ada Lovelace's notes were labelled alphabetically from A to G. In note G, she describes an algorithm for the Analytical Engine to compute Bernoulli numbers. It is considered to be the first published algorithm ever specifically tailored for implementation on a computer, and Ada Lovelace has often been cited as the first computer programmer for this reason.

  • 6
    It should be noted however that the Analytical Engine was never actually built, so only exists as theory. – Darrel Hoffman Apr 18 at 17:17
  • It was made functional in an emulator though: retrocomputing.stackexchange.com/a/6287/11796. – bitsoflogic Apr 19 at 13:49
  • 1
    Either way, I figured the language was the most important thing to note for this conversation. – bitsoflogic Apr 19 at 13:50
  • And just to confirm: Ada Lovelace's code (large image) does indeed contain a conditional. She also makes a comment about that in her notes, writing “if we were calculating for n = 1 instead of n = 4, Operation 6 would have completed the computation of B1 itself, in which case the engine instead of continuing its processes, would have to put B1 on V21[...]” – leo Apr 19 at 18:02
10

Computers since ENIAC have had conditional branch instructions (like jump if zero), which means assembly languages have had such a statement since the beginning, and there is no logical reason for higher-level languages to ever have forgone using it.

While it's probably theoretically possible to make programs with just your indirect jump idea, you sort of have a chicken and egg problem where it's difficult to get the branch address into the register conditionally in the first place. Implementing a conditional branch using only indirect jumps would add several instructions every time you needed to make a choice, using precious time, memory, and registers,

4

The answer would, by definition, be the first programming language.

A little bit of free CS101 here...

All algorithms can be expressed using 3 elements: sequence, selection, and iteration. Those are the basic building-blocks of a computer program. In order to express an algorithm with a programming language, it has to support those 3 elements in some form. An "if" check is of course a kind of selection.

Sequence, Selection, and Iteration are the basic elements that we use to tell the computer what to do. The code will definitely look different depending on the programming language we use, but the algorithm will be the same.

So let’s describe these elements:

  • Sequence– the order we want the computer to execute the instructions we provide as programmers. For example, do this first, then do this, then do that, and so forth.
  • Selection– selecting which path of an algorithm to execute depending on some criteria. For example, if you passed a class in school, then we execute the operations that clap and cheer and play a song. But if you didn’t pass the class, then maybe we would say, “Better luck next time, hang in there!”
  • Iteration– looping or repeating. Many times, we want to be able to repeat a set of operations a specific number of times or until some condition occurs.

To get down deeper into computability theory, we call the ability of a model to express any algorithm Turing completeness, and selection is required for this. Most CS types will tell you than anything that isn't Turing complete isn't really a programming language.

So by definition every programming language has some means of selection, and always has. Without that, you don't really have a programming language.

  • 1
    I agree with your main these, but have a program with the 'sequence, selection, and iteration' point: declarative languages don't have sequence and yet are certainly programming language. – Quelklef Apr 18 at 17:39
  • 1
    "by definition every programming language has some means of selection" - but the question explicitly isn't asking for the first programming language with some means of selection; it's asking for the first language with "a dedicated keyword for conditional execution". So for example, the untyped lambda calculus is certainly a programming language, but it certainly does not have a dedicated keyword for conditional execution. – Tanner Swett Apr 18 at 18:04
  • @Quelklef - That's a bit of a gray area. "Pure" declarative languages are typically not Turing complete, and the ones that are Turing complete I believe all allow some sense of sequence. (Arguably, the sequencing and iteration are still there, but buried in their interpreter). Does that mean pure declarative languages aren't proper "programming languages"? They are languages (often called DSLs), but "programming languages"? – T.E.D. Apr 18 at 18:07
  • 1
    @T.E.D. sequence is just a metaphor. All of the "sequencing" that arises from monads is a metaphor; it's not central to the language. The monadic sequence metaphor is, however, usually built on the real notion of sequence. The real source of sequencing is that, if you want to evaluate case scrut of { D _vars -> _arm; _ -> _etc } (with data-con D), you must first evaluate scrut, match against D _vars, then evaluate _arm. The root of the sequence/evaluation/"strictness" is the runtime system, an entity "external" to Haskell that methodically tears down the data structure main. – HTNW Apr 19 at 0:33
  • 1
    @Quelklef Actually, when Haskell evaluates f (g x) it will almost always evaluate f first, not g. This is the essence of call by need evaluation order, aka "laziness". (There are probably some strictness annotations you can put on f to make it evaluate g first, but it's not the default.) As HTNW says, the real sequencing operation in a lazy language is case (specifically, scrut must be evaluated to weak head normal form before _arm can begin evaluation). – Mario Carneiro Apr 19 at 18:49
4

I'm going to go out on a limb here and assume that what is wanted is not a language that has conditional branches i.e. an equivalent to

if condition then goto someLabel

because Turing complete computers have had that since the beginning. I'm going to assume we are talking about a block structured conditional like

if condition
    some arbitrary sequence of statements including perhaps nested ifs
else 
    some other arbitrary sequence of statements including perhaps nested ifs

Some early contenders for that would be:

  • Lisp (1958) which has an if and cond function. I think condpredates if. When I learned Lisp in the 1980's I'm fairly sure that if wasn't there.
  • Algol 60 (1960) which has the structured if inherited by most modern imperative languages

Early versions of FORTRAN and COBOL did not have structured if statements as far as I know.

  • I want to say yes to your going-out-on-a-limb, but that changes the question and kinda invalidates the other answers. I don't want to do that to the others, even though what you wrote is what I really wanted to know. So I asked a new question and would be happy if you just copy-pasted your answer there. – R. Schmitz Apr 18 at 15:48
  • 1
    Early LISP had conditional support but not IF. Conditionals in M-expressions were handled via syntax. Conditionals in S-expressions were handled via the COND symbol/keyword. – Kelvin Sherlock Apr 18 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.