No history yet

Introduction to Programming

Storing Information

At the heart of any program is data. Whether it's a player's score, a character's name, or the color of a button, your code needs a way to keep track of this information. To do this, we use variables.

Variable

noun

A named container in a program's memory that stores a piece of data. Its value can change as the program runs.

Think of a variable as a labeled box where you can put something. You give the box a name, like score, and then you put a value inside it, like 100. Later, you can look inside the box to see what's there, or you can replace the contents with something new.

But not all data is the same. Just as you wouldn't store soup in a paper bag, programming languages use different data types to store different kinds of information. Here are the most common ones:

Data TypeDescriptionExample
IntegerA whole number, without a fractional part.10, -5, 0
FloatA number with a decimal point.98.6, -0.5, 3.14
StringA sequence of characters, like text."Hello, world!", "Player1"
BooleanA value that can only be true or false.true, false

Choosing the right data type is important because it tells the computer how to handle the data. For example, you can do math with integers and floats, but you can't multiply two strings together in the same way.

// Let's create some variables

// An integer for a player's score
score = 1500

// A float for a character's speed
speed = 5.5

// A string for a user's name
username = "Alex"

// A boolean to check if the game is over
is_game_over = false

Making Decisions

Programs rarely do the exact same thing every time they run. They need to react to different situations, like a player pressing a key or a timer running out. This is where control structures come in. They control the flow of the program.

The most basic control structure is the if statement. It lets your program make a decision. You give it a condition (a question that is either true or false), and if the condition is true, a specific block of code will run.

You can think of it like this: If it's raining, then I will take an umbrella.

The condition in an if statement is usually a comparison. For example, is the player_health variable less than or equal to 0? If it is, the code to end the game runs. You can also add an else part, which runs only if the condition is false. If it's raining, take an umbrella, else wear sunglasses.

player_health = 10

// Check if player_health is 0 or less
if player_health <= 0:
  print("Game Over!")
else:
  print("Keep playing!")

// The output here would be "Keep playing!"

Repeating Actions

Sometimes you need to do the same task over and over again. Instead of writing the same code multiple times, you can use a loop. Loops are control structures that repeat a block of code until a certain condition is met.

There are two main types of loops. A for loop is great when you know exactly how many times you want to repeat something. For instance, you could use a for loop to count from 1 to 10.

// This loop will run 5 times
for i in range(5):
  // Each time it runs, it prints the current number
  print("This is loop number", i)

/* Output:
This is loop number 0
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
*/

The other main type is a while loop. A while loop keeps running as long as its condition is true. This is useful when you don't know ahead of time how many repetitions you'll need. For example, you might want a character to keep taking damage as long as they are standing in lava.

enemies_left = 3

// This loop continues as long as enemies_left is greater than 0
while enemies_left > 0:
  print("An enemy was defeated!")
  // Decrease the count of enemies by 1
  enemies_left = enemies_left - 1

print("All enemies defeated!")

Packaging Code

As your programs get bigger, you'll find yourself writing the same chunks of code in different places. To keep things organized and avoid repetition, you can package code into functions.

A function is a named, reusable block of code that performs a specific task. You define it once, and then you can "call" it whenever you need it.

Think of a function like a recipe. The recipe has a name (like BakeCake), it takes ingredients (inputs called parameters), and it produces a result (an output called a return value).

Functions make your code cleaner, easier to read, and simpler to debug. If there's a problem with how you calculate damage, you only have to fix it in one place: the calculate_damage function.

// Define a function that adds two numbers
// 'a' and 'b' are the parameters (inputs)
func add(a, b):
  // The function returns the sum of a and b
  return a + b

// Now, let's call the function and store its result
sum_result = add(5, 3)

// The 'sum_result' variable now holds the value 8
print(sum_result)

These concepts, variables, control structures, and functions, are the fundamental building blocks of almost every programming language. Mastering them is the first step toward building anything you can imagine.

Time to check your understanding of these core concepts.

Quiz Questions 1/6

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

Quiz Questions 2/6

You need to store the price of an item in an online store, such as $19.99. Which data type is most suitable for this value?

With these basics, you have a solid foundation for starting your programming journey.