A colleague said he heard of a language that did not have the concept of "if". Is that possible? If so, what language is it?
-
1What do you mean by `ifs`? A language might not have an explicit `if` operator, but every language must have *some* way of branching from one piece of code to two other pieces of code, or else every input to a program would always give the same output. – BlueRaja - Danny Pflughoeft Oct 19 '11 at 22:44
-
Did my answer provide what you were looking for? If so please mark it as the answer to your question – Paul C Jun 06 '12 at 10:46
-
Also see [can-you-write-any-algorithm-without-an-if-statement](http://stackoverflow.com/questions/1937362/can-you-write-any-algorithm-without-an-if-statement) – nawfal Jan 04 '14 at 11:52
5 Answers
Besides perhaps Prolog, I don't know of any specific languages, but I can think of a few ways a language without if statements may work. In fact, you don't need loop constructs either. You obviously needs some way of conditional branches and looping.
If, for example, you had the following features: functions, ML-style pattern matching on function arguments and tail-call optimization, you could program without if's or loops.
foo () {
for (i = 1 to 10) {
if even(i) {
print "even"
}
}
}
would become soemthing like
print_if_true (true) {
print "even"
}
print_if_true (false) {}
foo_loop (11) {
}
foo_loop (n) {
print_if_true(even(n))
foo_loop(n+1)
}
foo () {
foo_loop(1)
}
or with ML-like syntax:
foo =>
let loop 11 => 0
n => p_i_t(n), loop n + 1
and p_i_t true => print "even"
_ => unit
in
loop 1
end
Of course, you still need the usual comparison operators and then you can use simple true/false function argument pattern matching instead of conditionals. Or you could match on arbitrary values. Or the language could support guard expressions, which are basically if statements which determine if a function overload is valid or not.
The example above is obviously contrived and the code without ifs/loops is a lot uglier and harder to understand than the original, but it demonstrates how you can make do. More or different language features may make it possible to write clean programs without if/loops.
Another way would be something like this, if true == 1 and false == 0.
[function(){else-clause}, function(){then-clause}][condition]()
That is, store the true and false branch in a list or tuple or whatever you have that can be indexed by true and false and then use the result of the condition as the index, look up the branch and call the function. If your language supports macros, it may be possibly to translate traditional conditionals into this format.
Smalltalk, which is considered as a "truly" object oriented language, has no "if" statement, and it has no "for" statement, no "while" statement. There are other examples (like Haskell) but this is a good one. Source: Without ifs
-
7Smalltalk does however, define `ifTrue:` and `ifFalse:`, as well as `whileTrue:` and `whileFalse:` – jer Nov 28 '12 at 15:58
C++ template programming does not have an 'if' construct but is Turing-complete via template specialization:
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1

- 2,608
- 2
- 25
- 38
I believe a language has to have some means of doing selection, in order to be Turing-Complete. However, that means would not have to be your classic if-statement form.
Probably the most familiar example would be regular expression languages. (a | b*) makes a decision based on what's on the opposite sides of that |
. Not exactly an "if" statement.

- 44,016
- 10
- 73
- 134
There are logical languages that consist of statements. The results to a query is a logical assessment that checks if the result CAN be assumed by the group of rules that were "coded" .
Look at Prolog for example.

- 48,127
- 24
- 147
- 185