Are 'if' Statements Essential in Computational Logic?

  • Thread starter Thread starter DarkFalz
  • Start date Start date
  • Tags Tags
    Computation
AI Thread Summary
The discussion explores the necessity of 'if' statements in computational logic, using a bank account program as an example. It suggests that while 'if' statements are commonly used for decision-making, it is theoretically possible to eliminate them by modeling state transitions through matrices of function pointers. However, this approach significantly increases memory usage and complicates the program's design. Participants argue that 'if' statements provide a straightforward way to make decisions, which is crucial for clarity and debugging. Ultimately, the consensus leans towards the idea that 'if' statements are fundamental in programming, as they simplify the decision-making process.
DarkFalz
Messages
71
Reaction score
0
This question occurred to me some time ago when I was thinking about whether or not `if` statements are fundamental in computation.
Consider a program that manages a single bank account (for the sake of simplicity). The bank account could be defined as something like
Account
{
int balance; // current amount of Money

boolean withdraw(int n)
{
if (balance >= n)
{
balance = balance -n;
return true;
}
else
return false;
}

void deposit(int n)
{
amount = amount + n;
}
}
Since the program has no way to known in which state it currently is unless it performs validations using `if` statements, as in the withdraw operation, `if` statements are unavoidable.
However, over the course of time, the program will pass through a finite set of states that can be known beforehand. In this particular case, a state is defined solely by the value of the `balance` variable, hence we would have states: `{balance = 0 , balance = 1, balance = 2...}`.
If we assign each state a number, say state {0,1,2,...} with a 1-1 correspondence to the above set of states, and assign to each operation a number identifier as well (say `deposit = 0 and withdraw = 1`), we could model the program as an explicit transition between states and therefore remove every `if` statement from the code.
Consider that `state = 0` is the state where `balance = 0` and we want to perform a deposit of 50 dollars, if we coded every single possible execution of the deposit function, we could just define the deposit function as
void deposit (int n)
{
deposit[state][n]; // index deposit instance for state = 0, amount = n;
}
`deposit[][]` would be a matrix of pointers for a set of functions that represent each possible execution of deposit, like
deposit[0][0] -> balance = balance + 0; state = 0;
deposit[0][1] -> balance = balance + 1; state = 1;
...
In the case of withdrawal, it would be like:
boolean withdraw (int n)
{
// index withdraw instance for the current state and amount=n
return withdraw[state][n];
}
`withdraw[][]` would be a matrix of pointers for a set of functions that represent each possible execution of withdraw, like:
deposit[0][100] -> return false; state = state;
...
deposit[200][100] -> balance = balance - 100; state = 100; return true;
In this situation, the program that manages the single bank account can be written without using a single `if` statement!
As a consequence however, we have to use A LOT more memory, which may make the solution unreasonable. Also one may put the question of how did we fill the `deposit[][]` and `withdraw[][]` matrices? By hand? It somehow implies that a previous computation that used `if`s as necessary to determine and possible states and transitions.
All in all, are `if`s fundamental or does my example prove that they aren't? If they are, can you provide me an example where this does not work? If they are not, why don't we use solutions like these more often?
Is there some law of computation which states that `if` statements are unavoidable?
 
Technology news on Phys.org
A look at Turing machines may help you.
http://en.wikipedia.org/wiki/Turing_machine

It's basically the most simple computer possible that is still able to run any arbitrary algorithm. And it works only by explicit state transitions.
 
An "if" statement is just making a comparison.
You have seen the truth tables of boolean logic for AND, NAND, OR,.. gates.
The truth table, and the corresponding logic gates, are just basic "if" statements.
For an AND gate, when both inputs are "1", the output will be a "1". All other times the output will be a "0.
That is the same as IF A=1 an B=1 THEN C=1 ELSE C=0

In your program without the if statements, say for example in the deposit routine , the program still has to determine which and only which deposit[][] to execute. Does it?
 
256bits said:
An "if" statement is just making a comparison.
The if statement makes a comparison, and then chooses one of two options based on that comparison.
 
The point is that DECISIONS have to be made in programming and if statements are an extremely simple, and easy to understand, way to do that. If you get rid of them, you have to replace them with something that is likely to take as much computational time and without necessarily being as easy to understand when debugging.
 
You would find that restricting all decisions to be done using array pointers with state indices to be impractical in many cases. I personally would prefer a statement like "if( x < y){" to "execute_procedure[183]->" as you describe it. Enumerated values might help, but what is the point?
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
Replies
11
Views
1K
Replies
9
Views
2K
Replies
23
Views
2K
Replies
36
Views
3K
Replies
4
Views
1K
Replies
35
Views
4K
Back
Top