Search code examples
if-statementlogiclow-level

What is an "if" statement at the level of transistors?


I recently took a Digital Logic course and learned all about AND, OR, and various other kinds of logic. One thing we did not cover, which is entirely essential to programming, is if statements and it left me rather curious as to how they work.

My best guess is that it would simply be a 2:1 multiplexer, and as you add more else statements it gets to be 4:1 and 8:1 but that seems a little too complex for such a simple concept.

Anyone know what an if statement actually translates to?


Solution

  • You're forgetting that programs are executed as individual instructions, and (at least in the simplest case) instructions execute sequentially.

    So for if (a + 4 > 5) an instruction will load a into a register, another instruction will add 4, another instruction will compare the sum to 5. Then an instruction will test the "condition code" from the compare and either execute the immediately following instruction (executing the "if body") or "jump" to a location in memory several (or several dozen) instructions away (skipping over the "if body").

    There is digital logic in there, but it's at a lower level, deciding how to execute each individual instruction.