0
$\begingroup$

I know that this question is a bit out-of-the-box, yet i would be glad if someone could help with a good answers for my question because it is something that is troubling my curious mind.

When we program we have no doubt that we need conditionals, like the if statement. I will use the "if" statement as a reference throughout the topic.

I mean, if we have a program like

int main(int argc, char** argv)
{
int n = atoi(*argv);
if(n%2==0)
  printf("even");
else
  printf("odd");
}

Given these conditions, where we can only use what comes after the input is passed to the program to perform our computation, i cannot deny that i need an if statement to distinguish between an even and an odd number. This is just an exemple.

But then i wonder, what if we could mess with what comes before the argument passing occurs?

Let's assume that this program is called oddEven.exe. When the user executes the program by writing oddEven(3) for instance, on the command line, the keyboard sends a signal to the computer with the number 3, which is simply written to memory. No analysis is done on the input, it is simply dumped into memory.

Given this situation, once we run the program, we know only what a number (actually a sequence of characters) was written, but not which one was written. Now let's assume that the hardware itself would be responsible for calling the appropriate branch of the program once the number is written, as if two diferente programs were written.

int mainEven()
{
//code for even numbers;
printf("even");
}

int mainOdd()
{
//code for odd numbers;
printf("odd");
}

The hardware would then be responsible for decoding the signal sent by the keyboard and, treating it as a number, check whether it is odd or even. We could at this point argue that such a machine would make it possible to program without ifs. Yet, haven't we just implemented the if statements at the hardware level? Hence we couldn't get rid of conditionals.

Now, what if the computer was actually made out of several subcomputers, each implementing a diferent branch of the program. We can also see this as if we had 2 computers. Consider that once a button is pushed, computer C1 prints "even" whereas computer C2 prints "odd".

The user would then be responsible for choosing which computer to use based on the output he knows is right for the number he has in mind. At this point, neither the hardware nor the software contain conditionals. Is the problem solved? No! The conditionals are now "running" on the user's brain, who has to decide which computer to use.

My point is, where do conditionals come from? Where do they stop? Can we avoid them somehow? Can you prove me we need them? If we do, why do we put them at the hardware level? Why does the input of a program have to be compared against values to determine the branch to execute?

I am a computer scientist myself, but i feel like i have lost my path once i thought about this subject. I'm having trouble proving that i undeniably need conditionals to perform computation.

I'd be glad for some insightful answers! Thanks in advance

$\endgroup$
  • 1
    $\begingroup$ As a sidenote you can avoid if statement by small trick: char* arr[] = {"even", "odd"}; printf("%s\n", arr[n&1]); $\endgroup$ – Bartosz Przybylski Nov 14 '14 at 2:10
  • 3
    $\begingroup$ STOP RE-ASKING THIS! $\endgroup$ – Nicholas Mancuso Nov 14 '14 at 4:11
  • 1
    $\begingroup$ This question is very long and looks very similar to all of your earlier questions. If you are going to ask multiple, similar questions, you need to make it very clear how they are different and how the answers to your earlier questions didn't cover this one. $\endgroup$ – David Richerby Nov 14 '14 at 9:31
  • $\begingroup$ Since this question has been closed as duplicate, my answer to this question is posted with the "duplicated question". I hope it will help you end your quest. Some questions may require a different universe of discourse. $\endgroup$ – babou Nov 16 '14 at 13:34
1
$\begingroup$

It is certainly possible to avoid all conditionals, but most programmers get majorly annoyed if they try to use functional style on a very large program.

But then, no one ever programs anything serious directly in a Turing machine either.

$\endgroup$
1
$\begingroup$

You have asked basically the same question a few times already, so I suppose this is something you care deeply about. But as long as you don't give a definition of what a conditional is, exactly, you will not really get any definite yes or no answer. For example, in your odd-even example, some code is run or not run based on some condition; I would call this a "conditional" regardless of whether the choice is made in software, or in hardware, or by looking at figures in your morning coffee.

You might find theoretical programming languages like the lambda calculus interesting. In the lambda calculus you have three constructs: a lambda term is either a variable, a function definition, or a function application. There is nothing else (no if-then-else or switch statement), or rather, everything else has to be defined in terms of these three constructs. Still, the lamdba calculus is Turing-complete. You can give a lambda term which behaves like an if-then-else.

This basically works by getting rid of values: every value is a program and thus has certain behavior. The value true is a function which takes two arguments, evaluates the first and ignores the second argument. The value false is a function which takes two arguments, ignores the first and evaluates the second argument. So applying a boolean value to two terms results in the first term being evaluated if the boolean is true, and the second if the boolean is false. Something like this might come close to what you mean by a language without conditionals.

$\endgroup$
  • $\begingroup$ When you answer a question, why vote to close and deny others the right to answer too? - - - - - - - - - - - - - - I had to post my answer with the alleged duplicate, where it does not really belong. $\endgroup$ – babou Nov 17 '14 at 9:17
  • $\begingroup$ @babou. Sorry. Only after I wrote the answer, I saw that some people had already voted to close the question and I thought that they were right. $\endgroup$ – Hoopje Nov 17 '14 at 11:06
  • $\begingroup$ No problem ... it just seems very inconsistent. He actually has a very good and deep question, the problem was to dig enough to understand the proper way to state it. Deep questions require digging :) And that was not easy. $\endgroup$ – babou Nov 17 '14 at 11:39
0
$\begingroup$

Let's remove the user from the equation. Simple use case. A computer needs to process information arrived from another computer. Based on what information arrived or from who it arrived the computer needs to do specific operations.

Let's say we have a server that manages a home network and depending on what information comes from each computer he needs to do specific stuff.

How would he choose what to do without a conditional?

Also explaining on your code. Let's say now that you indeed have a separate piece of software for exactly each part of a conditional.

int mainEven()
{
//code for even numbers;
printf("even");
}

int mainOdd()
{
//code for odd numbers;
printf("odd");
}

but let's say that in your initial program after this check you moved on with the same code. So basically you have code before the conditional and code after the conditional that is the same in both cases. (odd or even) would you copy that code in all different software? Let's say now that you have 30 if statements in your code. Each being an if-else. You would have 60 different pieces of software that each does a specific part of code. Would you copy all the surrounding code in all 60? Or would you run the softwares in a specific row depending on what the answers are to each if-else statement? That would create 60^2 possible combinations (or is it 2^60? I keep getting confused about this).

Now let's go to a video game.

if (HP==0){
//player dead
player.FallDown();
gameOverScreen.show();
}

How would you code this without a conditional?

$\endgroup$

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