TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Why Are Conditionals Important?

22 pointsby q_eng_anonalmost 6 years ago
Quantum Computer Engineer here: Studied Computer Engineering in school (undergrad), now I&#x27;m working on quantum computers for an unnamed company. I thought I understood computers (having worked -albeit shallowly- on literally every level from silicon manufacturing to web-development) until I started working on quantum computers...<p>I&#x27;d like to get some varied perspectives (theoretical or otherwise) on why conditionals (i.e. the ability to write &#x27;if -something- do -this- else do -that-&#x27;) are important to classical computer architectures&#x2F;languages. Clearly they&#x27;re important - something to do with the fact that without them your machine isn&#x27;t actually Turing-complete. Clearly there&#x27;s something about them that differentiates them from other operations - see the limits imposed by branch operations in pipelining. Perhaps I just haven&#x27;t found the correct description of why they are important yet - or perhaps I am just stupid (very likely) and am incapable of understanding.<p>Input welcome, references appreciated.

12 comments

tonygalmost 6 years ago
Conditionals allow a program to <i>react</i> to the data it is presented with. This can be data from the outside world, or data from some other module internal to a program composed out of modules (i.e. subroutines, for example). Without conditionals - or their moral equivalent - programs would not be able to react to their inputs or to changes in their internal state.<p>--<p>There are Turing-complete languages without conditionals, though - even without data, as such! An example is the lambda calculus. If you want to learn more about this, read up on &quot;Church encoding&quot; and &quot;Scott encoding&quot;.<p>The basic idea is to use functions and the visitor pattern to encode a kind of conditional behaviour that varies with the input supplied to it.<p><pre><code> True = (lambda (t f) (t)) False = (lambda (t f) (f)) If = (lambda (b tr fa) (b tr fa)) (lambda (x) (If x (lambda () (display &quot;It was true!&quot;)) (lambda () (display &quot;It was false!&quot;)))) </code></pre> Desk check the reductions of applying that function to True and to False. The &quot;t&quot; and &quot;f&quot; arguments <i>to the boolean values</i> act as a kind of &quot;menu&quot; (the visitor pattern), from which the datum selects in order to communicate what variant of data it is. The pattern extends readily to natural numbers, lists, and so on, giving a straightforward encoding of not only conditionals, but also (one-layer-deep) pattern matching.
评论 #20748786 未加载
评论 #20742470 未加载
评论 #20742432 未加载
tlbalmost 6 years ago
There are styles of programming with no conditionals, especially in purely functional languages.<p>A common technique inside compilers is to separate out which code gets run, and which result gets used. If everything is side-effect-free, then there&#x27;s no harm in running the code whose result won&#x27;t get used. In highly parallel systems, you can just run both versions and select at the end. So you can rewrite:<p><pre><code> if foo &lt; 2: a = foo else a = 2 </code></pre> to:<p><pre><code> a = phi(foo&lt;2, 2, foo) </code></pre> (the phi operator is taken from SSA notation. See <a href="https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#phi-instruction" rel="nofollow">https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#phi-instruction</a> if you&#x27;re unfamiliar)
评论 #20743113 未加载
lackeralmost 6 years ago
Some operations are basic enough that theoretically you can build any other operation out of them. “NAND” is commonly used as an example but in practice “nand” doesn’t really map to how people think. Conditionals are another one. They are a bit more complicated in a sense (three inputs rather than two) but they map better to how people think so people actually use them in practice.<p>So Turing completeness is basically two things. 1: the ability to compose an arbitrary number of conditional statements. 2: the ability to keep computing recursively, until some output is true.
SamReidHughesalmost 6 years ago
Without conditionals, every program either terminates after a pre-specified amount of computation or never terminates. That leaves you extremely limited in what you can accomplish (less powerful than a DFA).<p>A DFA or maybe a 2DFA (which are equally powerful) is basically what you get when <i>all</i> you have is conditionals.<p>Without that, what you have is a box that can read finite input and compute a value. Equivalent to a finite lookup table.
评论 #20740909 未加载
ktpsnsalmost 6 years ago
Conditionals actually destroy information. The way they are written does not even matter; Consider the evaluation of this functional pseudocode:<p><pre><code> min(a,b) := if(a &lt; b, a, b) </code></pre> Obviously, the evaluation of this function looses information about either a or b. This is of course by intention. But this is incompatible to a closed (quantum) system where information must be preserved.<p>Interestingly (but I think there is no deeper connection), vectorization uses masks to avoid branching. Programmatically, one implements then both branches at the same time, and on a per-data level different operations are performed. A simple pseudo-numpy example would be<p><pre><code> B[where(A &lt; B)] = A which should actually read component by component, for instance, having A and B being matrices, B_{ij} = A_{ij} if A_{ij} &lt; B_{ij} B_{ij} else (&quot;no-op&quot;) </code></pre> One can write long programs following this paradigm which frequently excel at performance at GPUs and modern day CPUs (which are actually vector machines).
评论 #20742444 未加载
badrabbitalmost 6 years ago
Are you not approaching it a bit too much from a high level perspective?<p>From assembly(x86), most conditionals and loops break down to cmp&#x2F;test and jmp,jx,jnx type instructions.<p>Computers need to make decisions based on comparisons of data or program state. You can abstract decision making however you want (e.g.: switch statements in place of if&#x2F;else or usage of goto&#x27;s) but the computer needs to test or compare and decide on what instruction to execute based on the result.<p>Let me invent a new condtional &#x27;alot&#x27;:<p>for i in range(0,100): alot i: exit(1)<p>instead of testing for boolean true&#x2F;false. My conditional &quot;alot&quot; tests the result of the evaluation against the value range of the type,if the result is over half of the value range it executes the conditional statement (exit())<p>Of course I am being a bit silly but my point is my silly conditional much like if&#x2F;else breaks down to comparing and jumping based on evaluation (i.e: instead of a jz&#x2F;jnz it will break down to jge)
psv1almost 6 years ago
To simplify, software (generally) does this:<p><pre><code> input &gt;&gt;&gt; output </code></pre> By definition the output <i>is conditional</i> on the input, at least if the program works correctly. That&#x27;s why conditional statements are important - they express the relationship between input and output.<p>Isn&#x27;t this really basic stuff? Or am I missing something?
评论 #20743122 未加载
评论 #20742916 未加载
raincomalmost 6 years ago
In a trivial way, logical gates (AND, OR, etc) are conditionals. So, we need these gates to build any logical expressions. And many problems can be reduced to satisfiability, etc.
mcphagealmost 6 years ago
Taking in input, and making decisions based on that input, are what computers are <i>for</i>. And conditionals are the simplest version of &quot;making a decision&quot;.
AnimalMuppetalmost 6 years ago
Most computer programs are, in practice, <i>difference amplifiers</i>. Conditionals are how they achieve that.<p>(&quot;Most&quot; because you have programs like image classifiers that are trying to be <i>similarity amplifiers</i>. That turns out to be hard to do with the standard building blocks, which want to be difference amplifiers. Perhaps different building blocks would be more useful.)
评论 #20749141 未加载
jensgkalmost 6 years ago
It is possible to approximate logical expressions without conditionals. This is an old problem in neural networks. See more here: <a href="https:&#x2F;&#x2F;towardsdatascience.com&#x2F;emulating-logical-gates-with-a-neural-network-75c229ec4cc9" rel="nofollow">https:&#x2F;&#x2F;towardsdatascience.com&#x2F;emulating-logical-gates-with-...</a>
Jtsummersalmost 6 years ago
I apologize, but I really am having a hard time understanding your question at all. Every non-trivial computation requires some kind of conditional.<p>The first computers were literally people, they computed. There wasn&#x27;t really a lot of &quot;conditional&quot; work to what they did, they did <i>math</i> (principally numerical work) like generating tables of data and compiling the results. They still needed some conditionals, like if a value was over a certain threshold, use a particular formula or change some parameter.<p>The early mechanical computers were largely tabulators of the same sort, but machines and not people. But as you want to express more complicated computations, you need the ability to branch. Conditionals are just a means for executing such a branch. Even the simplest of computational machines (in the theory sense) like regular expressions and deterministic finite automata include a branch or conditional element: Given input C while in state S go to state S&#x27;. A simple way to visualize this decision matrix is with a table:<p><pre><code> Input | S_0 | S_1 ----------------- 0 | S_0 | S_0 1 | S_1 | S_1 Accepting states = {S_0} </code></pre> Is a simple machine that will recognize any binary string ending in &#x27;0&#x27;. In each state, a decision is made on what the next state should be based on the input. This isn&#x27;t even a Turing complete system, DFAs can only recognize regular languages. This cannot be done without some sort of conditional branching capability. You may be able to encode this in a hardware description language such that the conditional is obscured or obfuscated, but it&#x27;s still there.<p>Even trying to compute taxes or payroll depended on conditionals (income level determines which tax brackets you pay into, over a certain amount of income you&#x27;ve maxed out Social Security). And that was one of the earliest (commercial) uses for computational devices.<p>Now, the way we express conditionals may change from one language or system to another. In Erlang, you can remove a lot of `if` expressions using pattern matching, consider the simple computation of the Fibonacci numbers as an example:<p><pre><code> fib(0) -&gt; 1; fib(1) -&gt; 1; fib(N) -&gt; fib(N-1) + fib(N-2). </code></pre> A conditional is right there: If N = 0, return 1; if N = 1, return 1; else return fib(N-1)+fib(N-2). Haskell and other functional languages use similar constructs, and you can include other guards:<p><pre><code> fib(0) -&gt; 1; fib(1) -&gt; 1; fib(N) when N &gt; 1, is_integer(N) -&gt; fib(N-1) + fib(N-2). </code></pre> This removes the potential for infinite recursion by adding an additional guard, now this function is only valid for non-negative, integral inputs. It has been transformed now to: If N = 0, return 1; if N = 1, return 1; if N &gt; 1 and N is an integer, return fib(N-1)+fib(N-2); else signal an error.