Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Understanding Programming and Computer Execution

This lesson is built with resources from youtube.com, pearsonhighered.com and 1 more site.

Welcome to your very first lesson! We're starting an exciting journey into the world of programming, with the ultimate goal of getting you proficient in TypeScript and Next.js. But before we can build complex web applications, we need to start with the absolute fundamentals.

This lesson addresses the first learning outcome of our course: to explain what programming is and how computers execute instructions. Think of this as looking under the hood of a car before you learn to drive. Understanding these core concepts will give you a much deeper appreciation for why your code works the way it does, which aligns perfectly with your preference for deep theory.

We'll cover:

  • The basic definition of programming as a form of communication.
  • The language barrier between humans and computers (machine code).
  • The essential hardware components that work together to run a program.
  • The step-by-step process a computer uses to execute your instructions.

Let's begin!

What is Programming? The Art of Giving Instructions

At its heart, programming is the process of giving a computer a set of very specific, step-by-step instructions to accomplish a task. Imagine you're writing a recipe. You need to list every ingredient and every single action in the correct order. If you write "add flour" but don't say how much or when, the person following the recipe will be lost.

Computers are even more literal than a novice baker. They are powerful but fundamentally "dumb" machines that do exactly what they are told, no more and no less. They can't guess your intentions.

To see a great analogy for this, let's watch a short segment from a video by freeCodeCamp.org.

Introduction to Programming and Computer Science - Full Course

This video introduces programming using a Lego analogy, which perfectly illustrates why computers need extremely precise instructions.

Watch the section from 01:32 to 03:03. Pay close attention to the idea that computers are not inherently 'smart' and how that impacts the way we must write instructions for them.

As the video explains, a program (also called software) is simply that list of instructions. The person who writes these instructions is a programmer.


Communicating with Computers: From Human Ideas to Machine Actions

So, if programming is about giving instructions, how do we "talk" to a computer? We can't just speak English to it. This is because computers operate on a fundamentally different level.

The Language of Computers: Binary

A computer's central processing unit (CPU), its "brain," only understands one language: machine code. This is a set of instructions represented as sequences of binary digits—1s and 0s. A 1 typically represents an "on" state (presence of an electrical signal), and a 0 represents an "off" state (absence of a signal).

Every piece of data stored on a computer—numbers, text, images, and programs—is ultimately converted into these long strings of 1s and 0s.

Introduction to Computers and Programming

To understand this better, let's read a short excerpt from the 'Introduction to Computers and Programming' textbook. It explains how data is represented in binary.

Read the section titled 'How Computers Store Data'. Focus on understanding the concepts of bits, bytes, and how different types of data (like numbers and characters) are translated into binary format. You don't need to memorize how to do binary conversion; just grasp the core idea.

Programming Languages: The Translator

Writing programs directly in machine code is incredibly difficult and tedious. This is where programming languages come in. They act as a translator or a "middleman" between the programmer and the computer.

  • Low-Level Languages (e.g., Assembly): These are very close to machine code. They give the programmer a lot of control over the computer's hardware but are complex and difficult to write.
  • High-Level Languages (e.g., JavaScript, Python, Java): These languages use words and syntax that are much closer to human language. They are easier to learn and use, allowing programmers to build complex applications more quickly. Our focus, JavaScript, is a high-level language.

We write our instructions (called source code) in a high-level language. Then, a special program translates our source code into the machine code the CPU can understand.


The Engine Room: How a Computer Executes Code

Now for the part you're interested in—what happens inside the machine? When you run a program, several physical components, known as hardware, work together.

Let's watch a fantastic animated overview from TED-Ed that visualizes this process.

Inside your computer - Bettina Bair

This video, 'Inside your computer', provides an engaging, high-level look at how a simple action like a mouse click triggers a complex sequence of events involving the CPU, memory, and software.

Watch from 01:21 to 03:56. Notice how the CPU is described as the 'brains' of the computer, and its main job is to fetch and execute instructions from memory.

The video introduced the key players. Let's formalize their roles:

  1. Central Processing Unit (CPU): This is the core of the computer that actually runs programs. It fetches instructions from memory and executes them one by one.
  2. Main Memory (RAM - Random-Access Memory): This is the computer's "workbench" or short-term memory. When you run a program, it's copied from long-term storage into RAM to be accessed by the CPU. RAM is volatile, meaning its contents are lost when the computer is turned off.
  3. Secondary Storage (Hard Drive, SSD): This is the computer's long-term memory. It's where your files and programs are stored permanently. It is non-volatile.

The Fetch-Decode-Execute Cycle

The CPU's fundamental job is to continuously repeat a process called the fetch-decode-execute cycle.

Computer Instruction Cycle and Program Execution Flow
This diagram shows the journey of a program from storage into RAM, and then how the CPU processes its instructions via the Fetch, Decode, and Execute cycle.

Here's what that means:

  1. Fetch: The CPU fetches the next instruction from its location in RAM.
  2. Decode: The CPU's control unit decodes the instruction, which is in machine code, to understand what operation needs to be performed (e.g., add, subtract, move data).
  3. Execute: The instruction is performed by the appropriate part of the CPU (for example, the Arithmetic Logic Unit does the math). The result might be stored back in memory or displayed to the user.

This cycle happens billions of times per second on a modern computer.

Compilers and Interpreters

The final piece of the puzzle is how our high-level source code becomes the machine code that participates in this cycle. There are two main types of translators:

  • Compiler: A compiler reads your entire program at once and translates it into a complete, separate machine code file (an executable). You then run that file. This is like translating a whole book from English to French and then giving the French version to a reader.
  • Interpreter: An interpreter reads your program one line at a time, translates that single line into machine code, and executes it immediately before moving to the next line. This is like having a live translator who translates a conversation sentence by sentence.

JavaScript has traditionally been known as an interpreted language, which makes it flexible and easy to get started with. (Modern JavaScript engines actually use a sophisticated mix of both techniques called Just-In-Time (JIT) compilation, but we can explore that advanced topic later).

Test your understanding!

Based on what you've learned, why is it necessary to have both Main Memory (RAM) and Secondary Storage (like a hard drive)? Why can't a computer just use one or the other?

Show answer

Secondary Storage is needed for permanent, long-term storage because it is non-volatile (it keeps data even when the power is off). However, it is relatively slow.

Main Memory (RAM) is needed because it is extremely fast, allowing the CPU to fetch instructions and data very quickly. This speed is essential for the computer to run smoothly. However, RAM is volatile (it loses all data when the power is off) and is more expensive, so we have less of it.

Therefore, we use them together: programs are stored permanently on the slow secondary storage, and when we want to run one, it's copied to the fast RAM for the CPU to work with efficiently.


Visualizing the Process: The Little Man Computer

To bring all these abstract ideas together, let's use a famous analogy called the Little Man Computer (LMC). It's a simplified model that perfectly visualizes how a CPU, memory, and a program interact.

The Little Man Computer Model
This diagram shows the Little Man Computer. The 'Little Man' is the CPU, the 'Mailboxes' are memory (RAM), the 'In/Out Baskets' are for input/output, and the 'Calculator' is the part of the CPU that does math.

Imagine the Little Man's job is to run a simple program:

  1. Fetch: The Little Man looks at his instruction counter (which tells him which mailbox to go to next), walks to that mailbox, and reads the instruction slip inside. He then increments the counter to point to the next mailbox for the next cycle.
  2. Decode: He reads the instruction. Let's say it's "Get input from the In-Basket and put it on the Calculator." He now knows what to do.
  3. Execute: He walks over to the In-Basket, takes the number inside, and puts it onto the Calculator.

He would then repeat the cycle for the next instruction, perhaps one that tells him to get another number and add it to the one on the calculator, and a final instruction to put the result in the Out-Basket. This simple story models exactly what your computer does, just billions of times faster.


Key Takeaways

  • Programming is the act of writing precise, sequential instructions for a computer to execute.
  • Computers only understand machine code (binary 1s and 0s). We use high-level programming languages as a human-friendly way to write instructions.
  • A translator (a compiler or interpreter) converts our high-level code into machine code.
  • The main hardware components are the CPU (the brain), RAM (the fast, temporary workbench), and Secondary Storage (the slow, permanent file cabinet).
  • The CPU runs programs using the Fetch-Decode-Execute cycle, processing one machine-code instruction at a time.

You've just completed the foundational layer of your programming knowledge. You now have a mental model of what's happening inside the machine when code is run.

In our next lesson, we will zoom in on our language of choice: JavaScript. We'll explore what it is, where it came from, and its crucial role in building the modern web applications you see every day.