No history yet

Introduction to Programming

The Building Blocks of Code

At its heart, programming is about giving a computer a set of instructions. Think of it like a recipe. A recipe lists ingredients and provides step-by-step directions to turn those ingredients into a meal. A program does the same thing, but its ingredients are data, and its instructions tell the computer what to do with that data.

We write these instructions using programming languages, which act as a bridge between human ideas and the binary world of computers. Let's look at the fundamental concepts you'll find in almost every programming language.

Storing Information

Before we can instruct a computer to do anything, we need a way to store the information it will work with. In programming, we use variables for this. A variable is like a labeled box where you can keep a piece of information. You give it a name, and you can change what's inside the box whenever you need to.

Variable

noun

A storage location with a specific name that holds a value. The value can be changed during program execution.

Of course, not all information is the same. You wouldn't store a word the same way you store a number. That's where data types come in. A data type tells the computer what kind of data a variable is holding. This is important because it determines what operations you can perform on it. You can add two numbers, for instance, but you can't add a number and a word in the same way.

Data TypeDescriptionExample
IntegerWhole numbers10, -5, 0
FloatNumbers with decimal points3.14, -0.5
StringText, enclosed in quotes"Hello, World!"
BooleanA true or false valuetrue, false

Here's what this looks like in practice. This snippet creates a variable named age to hold an integer, and another called name to hold a string of text.

// Storing a number (integer)
let age = 30;

// Storing text (string)
let name = "Alice";

// Storing a true/false value (boolean)
let isLoggedIn = true;

Making Decisions and Repeating Actions

Programs aren't just static lists of instructions. Their real power comes from their ability to react to different situations. This is handled by control structures, which direct the flow of the program.

The most basic decision-making tool is the if statement. It's simple: if a certain condition is true, the computer performs a specific action. You can also add an else clause to provide an alternative action if the condition is false.

let temperature = 15;

if (temperature < 20) {
  // This code runs because the condition is true
  print("It's cold, wear a jacket!");
} else {
  // This code is skipped
  print("It's warm enough.");
}

What if you need to do something over and over? That's what loops are for. Loops allow you to execute a block of code multiple times without rewriting it. A common type is the for loop, which runs a set number of times.

// This loop will run 5 times
for (let i = 1; i <= 5; i = i + 1) {
  print("This is loop number " + i);
}

// Output:
// This is loop number 1
// This is loop number 2
// This is loop number 3
// This is loop number 4
// This is loop number 5

Another type is the while loop, which keeps running as long as its condition remains true. You use this when you don't know exactly how many times you'll need to repeat the action.

Building with Reusable Blocks

As programs get larger, it's helpful to break them down into smaller, manageable pieces. We do this using functions. A function is a named, reusable block of code that performs a specific task. Think of it like a mini-program within your main program.

For example, you could write a function to greet a user. It might take a name as an input (called a parameter or argument) and produce a greeting.

// Define the function
function greet(userName) {
  print("Hello, " + userName + "!");
}

// Call the function to use it
greet("Bob");  // Output: Hello, Bob!
greet("Sue");  // Output: Hello, Sue!

Using functions makes code easier to read, debug, and update. This practice of breaking a program into smaller, self-contained modules is known as modular programming.

The Plan Before the Code

Before you write a single line of code, you need a plan. In programming, that plan is called an algorithm. An algorithm is a finite, step-by-step set of instructions for solving a problem or accomplishing a task. It's pure logic, completely separate from any specific programming language.

Algorithm

noun

A step-by-step procedure for calculations or other problem-solving operations.

For example, here's an algorithm for a simple task: finding the largest number in a list.

  1. Look at the first number in the list and call it the 'largest so far'.
  2. Go through the rest of the numbers in the list, one by one.
  3. For each number, compare it to the 'largest so far'.
  4. If the current number is bigger, it becomes the new 'largest so far'.
  5. After checking all the numbers, the 'largest so far' is your answer.

This clear, logical plan can now be translated into code in any programming language. Developing strong problem-solving skills and learning to think algorithmically is just as important as learning a language's syntax.

Quiz Questions 1/5

In programming, what is the primary purpose of a variable?

Quiz Questions 2/5

A step-by-step set of instructions for solving a problem, which is independent of any specific programming language, is known as a(n) ____.