Skip to content

Variable and Constant

While fixed literal values are useful, to make programs dynamic you need to be able to work with values that change.

A variable is a building block you can use to store a value that you can change as the program runs. You can picture a variable as a container into which you can store a value and retrieve it later.

Variables store a value that can be read and changed

Variables — when, why, and how

Variables are an invaluable tool to help you build digital realities. They allow you to capture simple values, associate them with a name — the variable’s identifier — and change the value within your code. You can also use the variable to retrieve (i.e., read) the value it contains.

Whenever you have information in your program that you want to be able to change, you create a variable to store it. Every variable has a data type, which needs to match the kind of data you want to store. After you define a variable with an identifier and data type, you need to initialise it with a starting value. Then, you can then read the current value and store new values in the variable as the program runs and the information the variable represents changes.

Constants

A constant is similar to a variable, in that they have a name and a data type. Constants are declared within a program and must be given a value when they are created. However, they differ from a variable because once they are created the value within a constant cannot be changed. This is useful for data that should not change during the program’s execution.

Constants have a value that cannot be changed

Constants — Why, When, and How

Literals are values that are fixed within the code, so why do we need constants?

With programming, the biggest challenge is making sure your code is understandable. Scattering your program with literal values becomes confusing very quickly. When you see a value like 10 in code it doesn’t mean much, and potentially could represent anything. There will often be lots of these values that are easy to lose track of. Using a constant lets you name the value, which gives it meaning. When you see MAX_ITEMS in the code instead of 10, you are reminded of its meaning.

Another advantage of using a constant is that you can change the value in one place. If you want to change the maximum number of items in your pack from 10 to 42, you change it in one place. Updating the value of the constant means that everywhere that reads it will get the new value.

In C#

Variable and constant declarations use almost identical syntax in C#. The main difference between a variable and a constant is the const keyword at the start of the declaration. If you include const, you are creating constants. Without it, you are creating variables.

Both variables and constant declarations need to include the type of data you want to store.Then, you provide a name (identifier) for the variable or constant. A variable can optionally be given a value when it is declared, but a constant must be given a value. You can also declare multiple variables or constants in the one line of code, as long as they are of the same data type.

By convention, all constants should have an UPPERCASE name. Where the name has multiple words, separate them with underscores (_). For example: MAX_ITEMS. Variables should have a camelCase name, where the first letter is lowercase but each new word in the identifier starts with an uppercase character. For example, bottleVolume.

Examples

Basic Example

In this program, we want to capture the user’s name and greet them using it. We need a variable for the name, as it may change each time the program runs. Within the program, the variable will represent the name of the user.

using static SplashKitSDK.SplashKit;
// Create a variable called name
// it can store a string
string name;
// Create a constant called message
const string MESSAGE = "Welcome to The Greeting";
// Output the message
WriteLine(MESSAGE);
WriteLine();
WriteLine("What is your name?");
Write("name: ");
// Read user input and store it in the name variable
name = ReadLine();
WriteLine();
WriteLine("Hello " + name);

Example using multiple variables

The next example will help us calculate the volume of our water bottles, assuming they are cylinders. To achieve this, we need the following variables:

  • A PI constant for the calculation.
  • Numbers for radius and height which the user will enter.
  • A string line to store the text the user enters before we convert it to a number.
  • Numbers for bottleVolume and litres to store calculated values.
using static SplashKitSDK.SplashKit;
using static System.Convert;
// Create a const called PI
const double PI = 3.1415;
// Create variables radius, height, line, bottleVolume, and litres
double radius, height;
string line;
double bottleVolume, litres;
WriteLine("Water Bottle Volume");
WriteLine();
WriteLine("Enter the radius and height of the bottle in centimeters");
Write("radius: ");
line = ReadLine();
radius = ToDouble(line);
Write("height: ");
line = ReadLine();
height = ToDouble(line);
bottleVolume = PI * radius * radius * height;
litres = bottleVolume / 1000;
WriteLine();
WriteLine($"Volume { bottleVolume } cm^3");
WriteLine($" { litres } litres");

Circle Drawing Example

Let’s use SplashKit to look how variables can be used in a graphical program. The following program captures a radius for a circle and uses that to draw to the screen. To achieve this we need two variables, line and radius. We use ReadLine to read a string from the user, and ToDouble to convert the text read into a double value. This value is stored in the radius variable, which can then be used in the call to FillRadius.

using static System.Convert;
using static SplashKitSDK.SplashKit;
const double SATURATION = 0.8;
const double BRIGHTNESS = 0.8;
string line;
int radius;
double hue;
WriteLine("Welcome to Circle Drawer!");
WriteLine();
WriteLine("Enter the radius of the circle to draw. (a whole number)");
// Read value from the user and conver to double for radius
Write("Circle radius: ");
line = ReadLine();
radius = ToInt32(line);
// Read value from the user and conver to double for hue
WriteLine("What color? Enter hue between 0 to 1");
Write("Hue: ");
line = ReadLine();
hue = ToDouble(line);
// Draw scene and delay
OpenWindow("Circle Drawing", 800, 600);
ClearScreen(ColorWhite());
FillCircle(HSBColor(hue, SATURATION, BRIGHTNESS), 400, 300, radius);
RefreshScreen();
Delay(5000);

Activities

For each piece of information, identify whether it would make the most sense to store it in a variable or constant. Then, try writing the C# code to declare that variable or constant. Remember that variables and constants both need an appropriate name and data type, and constants must also be initialised when declared.

  1. The number of hours in a day.
  2. The amount of water, in litres, left in your water bottle.
  3. The number of meters walked so far in the day.
  4. The name of the species of wildlife you have most recently seen.

Answers There are other acceptable answers for the identifiers. The key to a good identifier is that it is short and meaningful. It should also follow the C# conventions, by using all upper case letters with words separated by underscores for constants, and camelCase for variables.

  • 1: Constant, because this value will not change. const int HOURS_IN_DAY = 24;
  • 2: Variable, because it will change if you drink from or fill up your bottle. double waterLeft;
  • 3: Variable, because it will increase as you walk. int metersWalked;
  • 4: Variable, because it will change when you see something new. string lastSpeciesSeen;

Debugging

Variables are fundamental and powerful tools in programming, but with that power comes the possibility for new types of bugs to emerge. Let’s see how to view and manipulate variables in VS Code’s debugger!