ThoughtStorms Wiki

Quora Answer : If the programming world has to choose between the IFs or the Loop and be allowed to use only one, what will be the best choice?

Nov 9, 2017

Neither by itself is sufficient.

A computer without some kind of if / branch operation is not a computer that can respond to input or context. At best it can churn out the same output every time the program is run.

A computer without looping / iteration means you have to write enough code to do everything as many times as you want to do it. Want a program that prints "hello world" ten times? You'll need ten print lines. Want it 1000? Your program needs a 1000 lines. The main benefits of automation are lost.

However, every real computer that was ever made, has some functionality which is the basis of both iteration and selection. Typically its a machine-code instruction (ie. hardwired into the processor) which says "jump to this new address in the program if this condition is met". Often it's as basic as "jump if this register is 0" (or not 0)

This is the fundamental "atom" of computer programming. Out of this statement you can build loops. You can build if statements and case statements. You can build polymorphic dispatch and map-reduce and Prolog-style inference engines. But you need this basis to do that.

In C-like languages, the standard "for" loop describes both the iteration and the test (which is why some people are answering this question by saying they'll take the "loop", but really they're taking both, because that's what a "for" is)

Really you can't do without either. And at the fundamental level, the two are (usually) implemented using the same underlying machine-code "atom".

No Backlinks