This question is about how to create an if
statement (one of the control-flow statements) from scratch.
An if-statement is typically a built-in construct in languages. However, I am wondering if there is a way to construct an if-statement from some other non-control-flow primitives. I am also wondering how to do this with Boolean algebra, and there is a link there to how to represent if/else
statements with circuits.
I started off trying like this:
var x = true
var run = {
true: [],
false: []
}
var state = []
while (true) {
x = !x
var arr = run[x == true]
for (var i = 0, n = arr.length; i < n; i++) {
}
}
That x
toggle seemed like it could be used to somehow shift the evaluation path. Then in the state
array there would be states of the application listed, and so the application would go into that state if the x
was 0 vs. 1. But there pops up the if
right there, so it's hard to make it work without resorting to if
somewhere. The for
loop is also a form of control-flow, so would hope to avoid that.
Was going to try to test it out to simulate a program such as this:
if (a == 1) {
if (b == 1) {
console.log('1.1')
}
if (b == 2) {
console.log('1.2')
}
}
if (a == 2) {
if (b == 1) {
console.log('2.1')
}
if (b == 2) {
console.log('2.2')
}
}
if (a == 3) {
if (b == 1) {
console.log('3.1')
}
if (b == 2) {
console.log('3.2')
}
}
The program would then run:
myiftest(1, 1) // a == 1, b == 1
myiftest(1, 2)
...
There are ways to sort of cheat, such as using &&
to hack the conditional. For example:
a == 1 && b == 1 && console.log('1.1')
You could also do it with a while
loop:
while (a == 1 && b == 1) {
console.log('1.1')
break
}
I would like to see if there's any way to do this using only non-control-flow statements in JavaScript (or in C, Ruby, or Python, any modern iterative language, or even Assembly which might make it easier), i.e. not cheating like the above. I don't even know if it's possible, so any form that shows it is possible (or not possible!) would be great to know.
It's okay to use the while(true)
loop to start it out, so the program actually runs (if necessary).
Even in assembly the "if" statement is built into the jmp
or jump-/branch-related statements.