No history yet

Introduction to Programming

The Building Blocks of Code

At its heart, a computer program is just a set of instructions that tells a computer what to do. To give these instructions, we need a way to store and label information. This is where variables come in.

Think of a variable as a labeled box where you can keep a piece of information. You give the box a name, and you can put something inside it. Later, you can look inside the box by using its name, or you can even swap out what's inside for something new.

variable

noun

A storage location in a computer's memory, paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

The type of information you store in a variable matters. You wouldn't put a sandwich in a box meant for shoes. In programming, this is called the variable's data type. Let's look at a few common ones.

Data TypeWhat it isExample
IntegerA whole number10, -5, 0
StringText"Hello!"
BooleanTrue or falsetrue, false

When you create a variable, you often declare its name and data type, then assign it a value. Here's what that looks like in a general sense, not tied to any specific programming language:

// Create a variable named 'age' to hold an integer.
let age: Integer = 30

// Create a variable named 'name' to hold a string.
let name: String = "Alex"

// Create a variable to check if the user is logged in.
let isLoggedIn: Boolean = true

Making Decisions and Repeating Actions

Programs would be pretty boring if they could only run instructions in a straight line. The real power comes from controlling the flow of the program. This is done with control structures.

The most basic control structure is the if statement. It lets your program make a choice. It works just like you do in real life: If it's raining, then you grab an umbrella. If not, you don't. The program checks if a certain condition is true, and if it is, it runs a specific block of code.

let temperature = 15

if temperature < 20 {
  // This code runs only if the condition is true
  print("It's a bit chilly. Wear a jacket!")
}

What if you need to do something over and over again? You wouldn't want to write the same line of code 100 times. For that, we use loops.

A for loop is great when you know exactly how many times you want to repeat an action. For example, if you want to print the numbers from 1 to 5.

for number from 1 to 5 {
  print(number)
}

// Output:
// 1
// 2
// 3
// 4
// 5

A while loop is useful when you want to keep doing something as long as a certain condition is true. For example, you might keep a game running while the player's health is greater than 0.

let health = 100

while health > 0 {
  // Code for one round of the game goes here...
  // At the end of the round, the player might take damage.
  health = health - 10
}

print("Game Over")

Control structures like if statements and loops give your program a brain. They allow it to react to situations and perform tasks efficiently.

Talking to Your Program

For a program to be useful, it needs to communicate with the user. This is done through input and output operations.

Output is what the program shows you. The most common way to do this is by printing text to the screen. We've already seen this with the print() command in our examples. It's the program's way of talking.

print("Hello! What is your name?")

Input is how you give information to the program. The program can pause and wait for you to type something. Once you do, it can store your response in a variable and use it.

// The program asks for the user's name and waits for input.
let userName = readInput()

// The program uses the input it received.
print("Nice to meet you, " + userName + "!")

This simple cycle of input, processing, and output is the basis for almost every computer program ever written.

These concepts, variables, data types, control structures, and input/output, are the absolute fundamentals of programming. Every complex application you've ever used is built on these simple ideas. Let's review them before we test your knowledge.

Ready to check your understanding?

Quiz Questions 1/5

What is the primary purpose of a variable in programming?

Quiz Questions 2/5

A developer needs to write code that repeats a specific action exactly 10 times. Which type of control structure is the most suitable for this task?