Decisions and branches.
So far, we have seen programs that execute instructions in a fixed sequence. Every instruction executes, in order, every time.
But real programs do more than that. Real programs make decisions. They choose between different paths based on conditions.
Today, we will understand how programs make choices. How they branch. How different conditions lead to different outcomes.
Until now, every instruction in our examples has executed. The program starts at the beginning, executes each instruction in order, and finishes at the end.
But this is not always the case.
Sometimes, a program skips instructions. Sometimes, it executes one group of instructions but not another. Sometimes, the path through the program depends on the data.
This is branching. The program reaches a point where it must choose: go this way, or go that way.
The choice is not random. The choice depends on a condition. The program evaluates the condition, and based on the result, it takes one path or the other.
This is how programs make decisions.
What is a condition?
A condition is a question that has two possible answers: true or false.
Is x greater than ten? That is a condition. The answer is either true or false, depending on the value of x.
Is the user logged in? That is a condition. Either they are, or they are not.
Is the list empty? That is a condition. Either it is, or it is not.
Conditions are questions about the current state. You look at the state, you ask the question, and you get an answer: true or false.
There are only two possible answers. Not "maybe." Not "sometimes." True or false. Yes or no. One or zero.
This binary nature is fundamental. It is what allows programs to make clear decisions.
When a program encounters a condition, it branches.
If the condition is true, the program takes one path.
If the condition is false, the program takes a different path.
Think of it like a fork in the road. You reach the fork. You look at a sign. The sign tells you: if your destination is north, go left; if your destination is south, go right.
You evaluate the condition — where is my destination? — and based on the answer, you choose a path.
Programs work the same way. They reach a decision point. They evaluate a condition. Based on the result, they take one path or another.
The key insight: only one path executes. If the condition is true, the "true" path executes and the "false" path is skipped. If the condition is false, the "false" path executes and the "true" path is skipped.
Both paths exist in the program. But only one executes on any given run.
Why do we branch? Because we want different outcomes in different situations.
Each branch contains different instructions. Different instructions cause different state changes. Different state changes lead to different final states.
Consider a simple example. We have a number, and we want to ensure it is positive.
If the number is negative, we multiply it by negative one to make it positive.
If the number is already positive or zero, we do nothing.
Two branches. One branch changes the state. The other branch leaves the state unchanged. The final state depends on which branch executed.
This is the power of branching. The same program can produce different results based on different inputs. The program adapts to the situation.
Let us trace a branching program.
We have a variable x. We want to compute the absolute value.
The condition: is x less than zero?
If true: set x to negative x. This flips the sign, making it positive.
If false: do nothing. x is already non-negative.
Let us trace with x equals negative five.
Initial state: x is negative five.
We evaluate the condition: is x less than zero? Negative five is less than zero. The condition is true.
We take the "true" branch: set x to negative x. Negative of negative five is five.
Final state: x is five.
Now let us trace with x equals three.
Initial state: x is three.
We evaluate the condition: is x less than zero? Three is not less than zero. The condition is false.
We take the "false" branch: do nothing.
Final state: x is three.
Same program, different inputs, different paths, different outcomes.
Notice what determines which path executes: the condition.
The condition is evaluated at the moment of the decision. It looks at the current state. Based on that state, it produces true or false.
The state at the moment of the decision is what matters. If x is negative at that moment, we take one path. If x is non-negative at that moment, we take another.
This means previous instructions affect which branch executes. If earlier instructions modified x, they changed the state, which changes the condition, which changes the path.
Everything is connected. The sequence of instructions before the branch determines the state. The state determines the condition. The condition determines the path. The path determines the final state.
Understanding a branching program means understanding this chain.
Sometimes, there are more than two possibilities.
Imagine classifying a number as negative, zero, or positive. That is three categories, not two.
We can still use conditions, but we need multiple checks.
First condition: is the number less than zero? If true, it is negative.
If false, we ask another condition: is the number equal to zero? If true, it is zero.
If false, it must be positive.
Three outcomes. Each outcome is reached by a different path through the conditions.
The structure is: check a condition. If true, take that branch. If false, check another condition. And so on.
Only one branch executes. The first condition that is true determines the path. Once a path is taken, the other possibilities are skipped.
How do branches fit with our model of state transitions?
Before the branch, we have some state. The condition is evaluated against this state.
Based on the condition, we enter one branch or another. Each branch may contain instructions. Those instructions cause state transitions.
After the branch completes, we have a new state. This state depends on which branch executed.
The branch is not a state transition itself. The branch is a choice of which state transitions to perform.
Think of it this way: the branch selects a subset of instructions. Only those instructions execute. Only those state transitions occur.
This is how branching interacts with our model. State determines condition. Condition selects instructions. Instructions cause transitions. Transitions produce new state.
Why is branching so important?
Because without branching, programs would be rigid. They would do the same thing every time, regardless of input.
With branching, programs can respond to different situations differently. They can handle different inputs. They can adapt to different conditions. They can make decisions.
Every interesting program uses branching. Login systems check if credentials are valid. Games check if the player collided with an obstacle. Calculators check what operation the user selected.
Branching is what makes programs flexible and useful. It is what allows one program to handle many different situations.
Without decisions, programs would be trivial. With decisions, programs can be powerful.
Let us summarize.
Programs can make decisions. They do not have to execute every instruction. They can choose between different paths.
A condition is a question with two answers: true or false. Conditions are evaluated against the current state.
Branching means choosing a path based on a condition. If the condition is true, take one path. If false, take another path.
Only one branch executes. The other branches are skipped.
Different branches contain different instructions. Different instructions cause different state changes. Different state changes lead to different outcomes.
Branching allows programs to respond to different situations differently. It is what makes programs flexible and powerful.
Decisions and branches. This is how programs choose.