Variables and Constants
Introduction
Hello students 👋 In programming, one of the first things you learn is how to store information so a program can use it later. That is where variables and constants come in. These ideas are small, but they are essential to almost every program, from a game score tracker to a school library system.
By the end of this lesson, you should be able to:
- explain what variables and constants are,
- describe how they help with problem-solving and computational thinking,
- identify when a value should change and when it should stay the same,
- use clear examples from programming to support your ideas,
- connect these concepts to abstraction, decomposition, and solution design.
Think of a real-life example 🏀: during a basketball game, the score changes after every basket. The score is like a variable because it changes. But the number of players on each team at the start of the game is usually fixed by the rules, so that can be treated like a constant. Programming works in a similar way.
What Is a Variable?
A variable is a named storage location in memory whose value can change while a program is running. In simple terms, it is a label for information that the computer may need to update.
For example, if a program tracks a student’s quiz score, the score might start at $0$, then change to $7$, and later become $9$. The name of the variable stays the same, but the value inside it changes.
A variable usually has:
- a name, such as
scoreorage, - a data type, such as integer, real number, string, or Boolean,
- a value, which is the actual data currently stored.
Example in pseudocode:
$$\texttt{score} \leftarrow 0$$
Later, after the player earns points:
$$\texttt{score} \leftarrow 5$$
This means the variable named $\texttt{score}$ now stores the value $5$.
Variables are useful because they let programs respond to changing situations. A weather app may store today’s temperature. A calculator may store input numbers. A game may store the player’s health, level, or timer. Each of these values can change during the program, so variables are the right choice.
What Is a Constant?
A constant is a named value that is intended to stay the same throughout a program. In many programming languages, a constant cannot be changed once it has been set, or it is at least treated as fixed by convention.
Constants are used for values that should not change because they represent a rule, a limit, or a value that remains fixed in the problem being solved.
Examples of constants include:
- the number of days in a week, $7$,
- the value of pi, $\pi \approx 3.14159$,
- a tax rate that is fixed for a calculation,
- the maximum number of players allowed in a team.
For example, if a program calculates the circumference of a circle, it may use a constant for pi:
$$\pi \approx 3.14159$$
If the program needs the radius $r$, it can calculate the circumference with:
$$C = 2\pi r$$
Here, $\pi$ is treated as constant because the formula depends on a stable value.
Constants improve clarity. When a number appears in a program as a meaningful constant, it becomes easier to understand what that value represents. Instead of writing a mysterious number like $3.14159$ many times, the program can use a name such as PI. That makes the code easier to read and maintain.
Variables and Constants in Computational Thinking
Variables and constants are important in computational thinking because they help you break down real-world problems into parts that a computer can handle.
Abstraction
Abstraction means focusing on the important details and ignoring unnecessary ones. When designing a program, you do not need every detail of the real world. You choose the data that matters.
For example, in a school attendance system, you may store:
- student name,
- date,
- attendance status.
You probably do not need to store the color of the student’s shoes 👟. That would be unnecessary for the problem. Choosing the right variables is part of abstraction.
Decomposition
Decomposition means splitting a big problem into smaller parts. Variables and constants help by giving each part its own piece of data.
For instance, a simple shop billing system can be broken into:
- item price,
- quantity,
- discount,
- total cost.
Each of these can be stored in a variable or constant. This makes the program easier to design and test.
Algorithmic Thinking
An algorithm is a step-by-step method for solving a problem. Variables are often used to keep track of values as the steps happen.
Example: a program may count how many correct answers a student gets in a quiz.
- Start with $\texttt{correctAnswers} \leftarrow 0$.
- For each right answer, increase the variable by $1$.
- At the end, display the total.
Without variables, the program could not remember the running total.
Programming Examples and Common Uses
In programming, variables and constants appear in many places. Here are some practical examples.
Example 1: Temperature Converter
Suppose a program converts Celsius to Fahrenheit. The input temperature changes every time a user types a new value, so it is stored in a variable, such as $\texttt{celsius}$.
The formula is:
$$F = \frac{9}{5}C + 32$$
Here, $C$ is the input variable, and $9$, $5$, and $32$ may be treated as fixed values. A programmer might store $\frac{9}{5}$ or $32$ as constants to make the code easier to understand.
Example 2: Game Score
A game may keep track of a player’s points with a variable:
$$\texttt{points} \leftarrow 0$$
Each time the player earns a point:
$$\texttt{points} \leftarrow \texttt{points} + 1$$
This is a common programming pattern called updating a variable.
Example 3: School Library System
A library program may use:
- $\texttt{maxBooks}$ as a constant for the borrowing limit,
- $\texttt{booksBorrowed}$ as a variable that changes for each student.
If the borrowing limit is $3$, then the rule could be written as:
$$\texttt{booksBorrowed} \leq \texttt{maxBooks}$$
This helps the program decide whether the student can borrow another book.
Naming, Data Types, and Good Practice
Good variable and constant names matter because they help programmers understand the program quickly.
A good name should be:
- clear,
- meaningful,
- related to the purpose of the value.
For example, $\texttt{age}$ is better than $\texttt{x}$ when storing a person’s age. The name tells you what the value means.
Data type also matters. A variable that stores a count should often be an integer, such as $12$. A variable that stores a student’s name should be a string, such as Maya. A variable that stores true/false information may be Boolean, such as isLoggedIn.
Choosing the correct type helps prevent errors. For example, it would be strange to store a telephone number as a number in some systems because leading zeros may matter. In that case, a string may be more suitable.
Constants should also be named clearly. A constant such as MAX_SPEED or VAT_RATE shows its purpose immediately. This supports code readability and reduces confusion.
Variables, Constants, and Evaluation
In IB Computer Science SL, you should not only know what variables and constants are, but also why they are useful in a solution.
When evaluating a program, ask:
- Are the values that change stored in variables?
- Are the fixed values stored or treated as constants?
- Are the names clear and meaningful?
- Does the program use the right data types?
- Would changing a value be easy if requirements changed?
For example, imagine a cinema ticket calculator. If the adult ticket price is stored as a constant, then changing the price later is easy because the value appears in one place rather than many. This is a strong example of good software design.
If a value should not change but is stored in a variable and accidentally updated, the program may give wrong results. So the choice between variable and constant affects correctness as well as readability.
Conclusion
Variables and constants are basic building blocks of programming. A variable stores information that can change, while a constant represents a value that should stay fixed. students, these ideas support computational thinking by helping you abstract important information, break problems into parts, and design clear algorithms.
In real programs, variables track changing values like scores, temperatures, and quantities, while constants store fixed values like limits, rates, and formulas. Understanding the difference helps you write better code, solve problems more effectively, and explain your reasoning clearly in IB Computer Science SL ✅
Study Notes
- A variable is a named storage location whose value can change during a program.
- A constant is a named value that stays the same or is intended to stay fixed.
- Variables are used for changing data such as scores, temperatures, and counters.
- Constants are used for fixed values such as $\pi$, limits, rates, and rules.
- Good names make code easier to read and understand.
- Variables and constants support abstraction by helping programmers store only the important information.
- They support decomposition by giving each part of a problem its own value.
- They support algorithmic thinking because programs often update and compare values step by step.
- Data type matters: choose integer, real number, string, or Boolean appropriately.
- In evaluation, check whether values that change are stored in variables and fixed values are treated as constants.
