HACKER Q&A
📣 revskill

A programming language without if then else?


I'm not sure if there is any high level and practical programming language which forbid usage of if then else ?


  👤 armchairhacker Accepted Answer ✓
In theory you could create a language without if/else as long as it has pattern matching, because if/else can be emulated like so:

    match condition {
        true => { ... }
        false => { ... }
    }
In fact you can create a language without if/else and pattern-matching, if you have something like first-class functions to handle branching. One notable example of such a language is lambda-calculus:

    true = λtf.t
    false = λtf.f
    if = λctf.ctf
Which in more familiar syntax is

    true = (t, f) => t()
    false = (t, f) => f()
    if = (c, t, f) => c(t, f)
And would evaluate like so

    if(true, workCorrectly, crashAndBurn)
        ==> true(workCorrectly)
        ==> workCorrectly()
However in practice, most high-level languages have if/else. Even lesser-known functional languages like Coq and Haskell which rely more on their pattern matching, still include it simply because it's convenient and easy to implement. Worst case, the compiler can (and sometimes does) just desugar the conditional into one of the above.

👤 hayley-patton
Smalltalk uses method dispatch and closures for control flow, which resembles the "Church encoding" in lambda calculus:

    3 < 4 ifTrue: [ 'true' ] ifFalse: [ 'false' ]
But very arguably that is still if-then-else, it's just not a special construct in the language. Avoiding Boolean values is also sometimes better to convey intent more precisely ("Boolean blindness"), but that's just a stylistic thing and not forbidding Boolean values entirely.

👤 sinuhe69
If you mean there is no if/then keywords in the language then yes, PROLOG is one. But if you mean there is no branching, no comparison (conditional branching) then NO. Even the most basic Turing machine has to decide, and to be able to decide you have to compare and do a form of branching (halt,left or right). Therefore, all “proper” programming languages must have conditional branching.

👤 yellowapple
Even low-level languages would struggle to get rid of "if"; any conditional branch is pretty much an "if". The various attempts in these comments to define "if" in terms of lambda calculus or pattern matching or whatever are noble, but at the end of the day, they'll be running on actual computers and will boil down to conditional branches - a.k.a. "if" - and even when they ain't, they're still just fancier ways of saying "if" anyway.

You can, however, do away with else using unconditional branches - a.k.a. "goto". Pseudocode:

    goto :positive if input > 0
    goto :negative if input < 0
    print "Got zero"
    goto :end
    :positive
    print "Got a positive number"
    goto :end
    :negative
    print "Got a negative number"
    :end
Whether this is an improvement is questionable, but I've seen some industrial and enterprise systems with their own "graphical" languages that do this. I've also seen (in similar environments) the inverse where everything is an if/else (or pass/fail), like so:

       start: compare(input > 0)  pass: positive fail: next
            : compare(input < 0)  pass: negative fail: next
            : out("Got zero")     pass: end      fail: end
    positive: out("Got positive") pass: end      fail: end
    negative: out("Got negative") pass: end      fail: end
         end: exit()              pass: next     fail: next

👤 GianFabien
Assembler? Uses conditional and unconditional branches / jumps / calls instead.

👤 Leftium
This video explains how all branching was removed by refactoring an imperative-style function into functional-style. The original code is on the top, the refactored code is on the bottom: https://youtu.be/GyYME8btMHE?t=1473

Generally, this type of refactoring converts code to a more declarative style: tell the computer what to do instead of how to do it (if-then-else): https://www.wikiwand.com/en/Declarative_programming

So you're probably looking for a declarative programming paradigm. (A single programming language can support multiple paradigms.)


👤 john_the_writer
You could do this with elixir?

def print(true), do: IO.puts("true")

def print(_), do: IO.puts("not true")

In fact it's typically best practice to do this. I almost never write if statements in elixir. It makes chaining much easier.

value |> change_1() |> change_2() |> print()


👤 brudgers
It is hard to wrap my head around the idea of "a high level programming language without conditional branching."

I mean you can sugar up if as "when" or "cond" or "unless," but unless you have conditional branching you won't really have logic within the program.

More philosophically, a language that doesn't have conditional branching will still be running on hardware that has it, so long as it is running on a computer.

Good luck.


👤 db48x
Early languages such as Fortran had only goto, with no structured loops or conditionals at all (Fortran did have an IF keyword, but it was just a form of conditional goto). The first language to have them was Lisp, invented in 1960. Although Lisp has mostly remained a niche language, structured conditionals and loops became ubiquitous pretty quickly. Structured conditionals and loops were retrofitted to Fortran in 1977.

👤 JonChesterfield
No branching might show up in some GPU kernel related things. Sometimes slow constructs are forbidden by construction.

(if predicate consequent alternate) might be missing in favour of only (if predicate consequent), or you might only have the variadic form (cond p0 c0 p1 c1 p2 c2 alternate).


👤 mdp2021
It would be a rigid process (e.g. take value, increase, sum, shift, store - that is what you could do)... How can a program be "«practical»" without conditional branching?

👤 rurban
One of the very first, lisp, only had cond and then the minor variants if and when. cond is more general than if then else, and also easier to read.

👤 comprev
Terraform's HCL doesn't have "if" statements as such. It does have a "for each" logic though.

👤 gompertz
Snobol4 is pretty much exactly this. Everything branches based on success or failure of predicates.

👤 bradknowles
Slack Workflows have no concept of conditionals.

👤 the_linux_lich
Gleam doesn't have if-else