Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>how can a computer understand it is executing an "if" or a "while" statement?</p> </blockquote> <p>The short answer is that the computer does not understand if or while.</p> <p>The computer executes one simple instruction after another. Each instruction is located at a particular address. There are special "branch" (or "jump") instructions to jump forwards and backwards in the instruction list. Some of these jump instructions are conditional on the value of certain bits in the CPU. For example there is usually a bit that indicates whether the previous calculation was zero. There are also instructions to set these bits, and special jump instructions that only jump <em>conditionally</em> based on the value of these bits.</p> <p>Consider this code:</p> <pre><code>if( a == 0 ){ b = 1; }else{ b = 2; } </code></pre> <p>This might get compiled to machine code something like the following made-up assembly code (and I'm simplifying this to explain how an if statement might be compiled):</p> <pre><code>;;(address instruction params ;; comments) 10 TEST a ;; set the "ZERO" bit based on whether a is zero 20 JNZ 50 ;; if ZERO bit is *not* set, jump to 50, otherwise fall through to 30 30 STORE b, 1 ;; store 1 in b 40 JUMP 60 ;; jump over else branch 50 STORE b, 2 ;; store 2 in b 60 </code></pre> <p>So the machine knows how to test values and set bits in the <a href="http://en.wikipedia.org/wiki/Status_register" rel="nofollow">status register</a> (that's where the zero bit is stored). And it knows how to conditionally jump forwards and backwards in the instruction stream (you can probably guess how a while loop is implemented: you make a test and jump <em>backwards</em> in the instruction list to make a loop). I guess you could say that the machine has a kind of "if" built in, but it is very simple: it can only jump forwards or backwards.</p> <p>You might find it informative to study the <a href="http://en.wikipedia.org/wiki/Instruction_set" rel="nofollow">instruction set</a> of a simple CPU such as the <a href="http://en.wikipedia.org/wiki/Atmel_AVR_instruction_set" rel="nofollow">Atmel AVR</a> to get an idea about what a CPU can natively do.</p> <blockquote> <p>And how did the early programmer's know what to type, using only binary?</p> </blockquote> <p>I guess they had a big list of all the instructions that the processor knew how to interpret. Or maybe an <a href="http://www.atmel.com/images/doc0856.pdf" rel="nofollow">instruction set manual</a>.</p> <p>By the way, early programmers didn't type, they entered code on <a href="http://en.wikipedia.org/wiki/Punched_card" rel="nofollow">punch cards</a>, or by flipping banks of switches. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload