Sequence and order.

In the previous videos, we learned that a program transforms state through instructions. We learned that execution is a sequence of state transitions.

Today, we focus on something that seems obvious but is deeply important: order.

Instructions execute in a specific order. That order determines the outcome. Change the order, and you change the result.

This is fundamental. Let us understand it deeply.

When a program runs, instructions execute one after another.

Not at the same time. Not in random order. One after another, in sequence.

The first instruction executes. It completes. Then the second instruction executes. It completes. Then the third. And so on.

This is sequential execution. It is the most basic form of program execution.

Think of it like reading a book. You read the first word, then the second, then the third. You do not read all words simultaneously. You do not skip around randomly. You follow a sequence.

Programs work the same way. The computer reads the first instruction, executes it, then moves to the next instruction.

This seems simple. But it has profound consequences.

Because instructions execute in order, the order determines the outcome.

Let me say that again. The order of instructions determines what the program does.

Consider two instructions: "put on socks" and "put on shoes."

If you execute them in order — socks first, then shoes — you get a sensible result. Socks under shoes.

If you reverse the order — shoes first, then socks — you get a different result. Shoes under socks. That is not what you wanted.

The instructions are the same. The order is different. The outcome is different.

This is true for all programs. The order of instructions is not arbitrary. It is essential to correctness.

When you write a program, you are not just choosing what instructions to include. You are choosing the order in which they execute. Both choices matter.

Why does order matter so much?

Because each instruction operates on the current state. And the current state depends on all the instructions that came before.

Remember: state is a snapshot of system data. Each instruction reads the current state, does something, and produces a new state.

The second instruction does not see the initial state. It sees the state after the first instruction executed.

The third instruction does not see the initial state or the state after the first instruction. It sees the state after the second instruction executed.

Each instruction inherits the effects of all previous instructions.

This is why order matters. Each instruction depends on the state left behind by previous instructions. Change the order, and you change what state each instruction sees.

Let us see this with a concrete example.

Consider these three instructions:

x equals ten. y equals x plus five. z equals y times two.

Let us trace the state.

After instruction one: x is ten.

After instruction two: x is ten, y is fifteen. Because y equals x plus five, and x is ten.

After instruction three: x is ten, y is fifteen, z is thirty. Because z equals y times two, and y is fifteen.

Final result: z is thirty.

Now let us reorder. Put instruction three before instruction two:

x equals ten. z equals y times two. y equals x plus five.

After instruction one: x is ten.

After instruction two: we try to compute z equals y times two. But what is y? We have not set y yet. y has no value, or has some undefined value. The result is wrong or an error.

Same instructions. Different order. Completely different outcome.

This example shows something important: dependencies.

Some instructions depend on others. Instruction two depends on instruction one because it uses the value of x, which instruction one sets.

Instruction three depends on instruction two because it uses the value of y, which instruction two sets.

When instruction A sets a value that instruction B uses, B depends on A. B must come after A.

Understanding dependencies is crucial for understanding order.

If there is no dependency between two instructions, their order might not matter. If there is a dependency, their order is constrained.

When you write a program, you must respect dependencies. An instruction that uses a value must come after the instruction that sets that value.

Let us think about order in everyday programs.

When you make a web request, you must establish a connection before sending data. Connection first, then data.

When you process user input, you must read the input before validating it. Read first, then validate.

When you save a file, you must have content before writing. Create content first, then save.

In every case, order is dictated by dependencies. You cannot send data without a connection. You cannot validate input you have not read. You cannot save content that does not exist.

Programs are full of these dependencies. And dependencies constrain order.

Understanding order means understanding what depends on what. Once you see the dependencies, the correct order becomes clear.

When you reorder instructions, you change the meaning of the program.

Sometimes the change is dramatic. The program crashes or produces garbage.

Sometimes the change is subtle. The program runs but produces a slightly different result.

Both are dangerous. Obvious failures are easier to find. Subtle differences can go unnoticed for a long time.

This is why order is so important to understand. When you read code, you must read it in order. When you write code, you must think about order. When you modify code, you must preserve the necessary order.

Moving an instruction is not a simple change. It changes what state that instruction sees. It changes what state subsequent instructions see. It can change everything.

To understand a program, you must think sequentially.

Start at the beginning. What is the initial state?

Execute the first instruction in your mind. What is the state now?

Execute the second instruction. What is the state now?

Continue, step by step, until the end.

This is sequential reasoning. It is how you understand what a program does.

You cannot understand a program by looking at instructions in isolation. Each instruction only makes sense in the context of the state it receives from previous instructions.

You cannot understand a program by looking at the end first. The final state only makes sense as the result of all previous transitions.

Sequential reasoning means following the order. It is the only way to truly understand a program.

When you write a program, order is a choice you make.

Some orders are wrong. They violate dependencies. They produce incorrect results.

Some orders are correct. They respect dependencies. They produce the intended result.

Sometimes multiple orders are correct. If two instructions have no dependency, you can put either one first.

But you must always choose consciously. Do not write instructions in random order. Do not assume order does not matter. Think about dependencies. Think about what state each instruction needs. Then choose the right order.

Order is not an accident. Order is a design decision.

Let us summarize.

Instructions execute one after another, in sequence. This is sequential execution.

The order of instructions determines the outcome. Same instructions in different order can produce different results.

Each instruction operates on the current state, which depends on all previous instructions. This creates dependencies.

When instruction B uses a value that instruction A sets, B depends on A. B must come after A.

To understand a program, you must reason sequentially. Follow the order, track the state, understand each transition.

Order is a choice. When you write a program, you choose the order of instructions. Choose consciously. Respect dependencies. Produce the correct result.

Sequence and order. This is how programs execute.