How do you define “if” for computers?

400 views

I’m not a programmer, how do you get from binary 1s and 0s to “if x occurs complete y” How do you tell the computer to wait for a future input?

In: 2

11 Answers

Anonymous 0 Comments

There are generally two strategies:

1. It checks the condition periodically to see if it has occurred.
2. The condition interrupts whatever the computer is doing at the time, and the computer then triggers the follow-up action.

Anonymous 0 Comments

Imagine that you arrange y (in the computer’s memory) as a domino arrangement that will “topple to completion” with one instruction triggering the next and the next and so on.

And then you run all of the possible options for x right next to the first domino tile in y, so that the moment x “happens to be 1 = true”, the set of domino tiles in y gets triggered.

In terms of the computer electronics, 1 = 5 volts and 0 = 0 volts (for example), and all of the transistors are connected together to form the set of domino tiles that forms your y condition. So as soon as x is calculated to be 1 (5 volts on that last connecting transistor), the whole set of transistors for y gets triggered (gets powered up with 5 volts, going down the chain).

Anonymous 0 Comments

CPUs have a BNE (branch not-equal) instruction, or similar. Code can set a register, and based on the state of that register (0 or 1) code either continues or jumps to another location.

Anonymous 0 Comments

Your questions might be interpreted in a variety of ways, I’m not sure how to approach them the way you want but since you’re not a programmer, I’ll go with something basic.

The first thing I can think of is “loops”. A program (or parts of it) can be made to run in loops, as in repeating an action again and again until an exit condition is met. This can be utilized in a way, like checking “if x has occurred” every time the code loops.

The instructions would look like

– loop this segment of code until y is completed:
– check if x occurred:
– yes: then complete y
– no: then do nothing

Assume this loop executes every 100ms, this would ensure you will be completing y in around ~100ms after x occurs. You don’t know when x is going to occur, but your program would catch it during the first iteration once it does. This way your program is able to wait for a future input, or at least act like it’s waiting for it.

You might also want to check out [event-driven programming.](https://en.wikipedia.org/wiki/Event-driven_programming)

Anonymous 0 Comments

There’s really two types (that I’m aware of) of IF conditions that happen in the actual circuitry.

IF zero/ IF not zero. (Commonly we see it written as if true/false) it will take a binary number and check to see if any of the digits are 1s. In such case, it will trigger an action that essentially skips to a particular line in the code, or continue on to the next line of code as normally if that trigger doesn’t get activated.

This true/false check works pretty much the same however way you do it. Just throwing in some NOT gates to get the opposite if that’s what’s desired. This can check to see if something is 0, or something isn’t 0. And I’ve never seen it done but I suppose you could have a circuit check for all 1s in the number as well. Although it’s really the exact same thing so there’s no reason for it.

If zero/not zero is also used to check if two things are equal. Just subtract one from the other, and if they’re the same you would have a zero, otherwise you’d have a non-zero value.

The other IF checks for a carryout or not after two things are added/subtracted. For example, if you add (in binary) 1010 and 1010 you get 10100. That’s really thought of as 0100 and the 1 upfront is carried out if the number has an extra digit now and doesn’t fit the standard size used in the computer.

This kind of check is generally going to be happening exclusively ‘behind the scenes’. As a computer needs to check the math it’s doing to see if it’s got the correct answer or not. Sometimes it will need to do special operations to handle a situation when there’s an unexpected result in the carryout bit.

This carry out bit can also trigger an IF statement for reasons relating to negative vs positive numbers. Good for doing things like ‘is x > y?’

Anonymous 0 Comments

You’ve got a range of answers here, but there’s a little bit that I don’t think is getting explained well.

Program code’s just a lot of data in memory. Even though in the languages programmers use you can see how things are connected, once it’s in memory all of the instructions are in a line, like a cookbook with no page breaks.

So the code for “if true” is in one place and the code for “if false” is in another place. Meanwhile the CPU has a special number that tells it where the next instruction is in memory.

So when a programmer does an “if x do y” block of code, really what’s happening is a line of code that’s more like, “If this condition is true, move to the line at this memory location, otherwise just move to the next instruction.”

So the way you tell a computer, “Get eggs if we are out, then do laundry” looks something like this in memory:

(1) Store the number of eggs in A.
(2) If A is bigger than 0, go to instruction 6.
(3) Go to the store.
(4) Get eggs.
(5) Return home.
(6) Do laundry.

The code on lines 3, 4, and 5 won’t be run by the CPU if (2) determines there are eggs, so that means the instructions to buy more eggs won’t run. But even if lines 3, 4, and 5 run, line 6 will run. If we want the more complicated, “If we are out of eggs, get more, but if we have eggs do laundry” you end up with:

(1) Store the number of eggs in A.
(2) If A is bigger than 0, go to instruction 7.
(3) Go to the store.
(4) Get eggs.
(5) Return home.
(6) Go to instruction 8.
(7) Do laundry.
(8) <You’re done!>

Note how lines 3 through 6 only run if there are no eggs, line 7 only runs if we have eggs, but line 8 will run no matter what.

Anonymous 0 Comments

Input bits A and B.

Very simply, send both through an AND gate, and it will produce a 1 only if both A and B are 1. You can then send the 1 through the circuitry to accomplish what you want in the case “A AND B” is true.

Anonymous 0 Comments

You understand that everything the computer does is arithmetic, right? Manipulation of numbers? In particular, the list of instructions is numbered (as illustrated by u/Slypenslyde’s answer), and the CPU has a number stored in its memory that tells it which instruction it is supposed to execute next.

If you’re trying to tell the computer to do something like `If c is 1, then go to instruction number n; if c is 0, then go to instruction number m instead`, then the way to do that without an “if” is to tell it `Go to instruction number (n*c+m*(1-c))`.

If this instruction is, itself, instruction number m, then the computer is just going to keep executing the instruction “Go to instruction number m” over and over until c becomes true, and then will go to instruction n. So effectively the computer will “wait” for c to become true before it acts.

Anonymous 0 Comments

Lets start by answering the last question you asked, because there is actually a fair bit of space between the answers to your various questions.

There are a couple of different ways to make computers wait for a signal. First, there is a “polling”, where you create a process to check the voltage on a wire at regular internals to get the data. The way is known as interrupts. With an interrupt, you have to define an interrupt handler, a small piece of software which does what you want it to do when the interrupt is triggered, and attached that to one of the interrupts provided by the underlying hardware. When the condition matching the interrupt happens, the CPU suspends the normal execution of the programs running on it to run your interrupt and returns to normal execution once the interrupt has been handled. In the interrupt, you generally write a flag in memory to let the programs that care know that the interrupt happened.

So, to get from binary to if structures, we need to talk about the structures of CPUs for a moment. One of the core components is known as the “program counter”. The program counter tells the CPU which instruction in memory is currently being executed. During normal operations, once an instruction has been executed, the program counter is updated to the next instruction in memory, but there are a few kind of instructions where this doesn’t happen: jumps, branches, enter & return from function. Enter & Return from Function are a bit more complex and don’t need to be explained to answer your questions, so I am going to skip it.

A jump instruction is fairly simply. Jump update the program counter to a new value, letting the programmer specify potentially any instruction in memory to be executed.

Branches are more complex, and can be implemented in a couple of different ways. Branches are conditional jumps. They generally check the condition of the “status registers”, which stores information about the state of the CPU. Depending on the state of the status register, a Branch will either jump to a specific address, or allow the CPU to load the next instruction as normal. A good chunk of the status registers are usually given over to the storing information about the last operation done by the ALU (Arithmetic Logic Unit, which does all the math). The “Zero” bit stores if the last math operation’s result was 0, and if two numbers are equal subtracting them will produce zero. This allows us to use a “branch if zero” to check if a location in memory has a specific value and jump to a specific location in memory if it does.

Anonymous 0 Comments

The CPU is hard-wired to read instructions and execute them. It does that because it’s designed to do that using physics. Programming consists of designing the instructions for the CPU to read. One kind of instruction might be “wait for X” which tells the CPU to wait until X happens.

In reality there are no “wait for X” instructions, but there are “wait for *something*” instructions and then the program can look at which kind of thing happened, and make the appropriate response.

There are multiple ways that can be achieved. A simple one is for the CPU to have a “something happened” ire that connects to all the peripheral devices (mouse, keyboard etc). When something happens that the CPU/program might need to react to, the peripheral device signals that wire, then the CPU somehow activates* the part of the program that deals with things happening, and checks each device in turn: “Ooh, something happened. Was a key pressed? No. Did the mouse move? No. Did the hard disk finally finish reading that data I asked for *ten milliseconds ago*? No. Is the screen refreshing? Ooh, the screen is refreshing. I’d better do that stuff I need to do when the screen refreshes.”

* The “somehow” is called an *interrupt*. One of the instructions in the program says “hey, whenever something happens, go to step 385744” or whatever. The CPU remembers that number, and when something happens it goes straight to step 385744 in the program, *interrupting* whatever it was doing before that.