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
char* arr[] = {"even", "odd"}; printf("%s\n", arr[n&1]);
$\endgroup$ – Bartosz Przybylski Nov 14 '14 at 2:10