Learn Programming

Learn D Programming Lesson 3 - Variables

What is a variable?

A variable in D is a name given to a block of the computer's memory. There are different types of variables which are used for storing the different types of data. Variables are a very important concept in programming because they are used in almost every single program that has ever been written so it is important to understand them well.

How to use a variable

Before you can use a variable you need to declare it. When you declare a variable you choose the data type of the variable and you choose a name for it. Here is an example of how to declare a variable of type int with the name MyVariable.

int MyVariable;

You need to be careful when you name your variables because variable names are only allowed to contain letters, numbers and underscores. Variable names are not allowed to start with a number but a number can be used anywhere else in the name. It is a good idea to capitalize the first letter of each word that makes up a variable name to make it easier to understand what words the variable name is made up of. If the variable is made up of only one word then it is fine for it to start with a lowercase letter. Variable names are case sensitive which means that when you use them after declaring them you must type them with the same uppercase and lowercase letters otherwise you will get errors.

After you have declared a variable you can set its value. You set the value of a variable using an equals sign(=). Here is an example of how you set the value of an int variable to 5.

int MyVariable;
MyVariable = 5;

If you like you can save time and lines of code by setting the value of the variable on the same line as the one that it is declared on. This is called initializing the variable. Here is how you do it.

int MyVariable = 5;

You can output the value of a variable to the screen using the writefln command. Here is an example of how you would output the value of a variable.

int MyVariable = 5;
writefln(MyVariable);

Numeric variables

There are 2 main types of numeric variables which are integers and floating point variables.

An integer is declared with the word int. They can store positive numbers, negative numbers and 0. They do have a maximum and minimum value that can be stored in them but they are so high and so low that it is not something you have to worry about right now. Here are some more examples of using an int including how to set the value of an int to a negative number.

int PosVar = 5;
int NegVar = -10;

Floating point variables can store whole numbers like integers and can also store numbers with a decimal point. You can declare a floating point number with the word float. There are other data types that can store numbers with decimal points but floats are good for general use. Here is an example of how to declare a float that shows its value being set to a number with a decimal point.

float MyFloat = 5.1234;

You can add, subtract, multiply and divide variables using the following operators.

+Add
-Subtract
*Multiply
/Divide

When you perform one of these operations on a variable or any numbers then you need to store the result in a variable. Here is an example where an int variable is given the value 5 and then 7 is added to it and stored in another variable and then printed on the screen.

int a = 5;
int b = 5 + 7;
writefln(b);

The other operators work in the same way.

int a = 3 + 2;
writefln(a);
int s = 5 - 2;
writefln(s);
int m = 2 * 4;
writefln(m);
int d = 8 / 4;
writefln(d);

Text variables

Programs are always showing words and sentences on the screen which means that there must be a data type that stores text. This data type is called a string. The words in a string need to be differentiated from the other code in a program which is why the value of a string must be put between double quotes("). Here is an example of how to declare a string called MyString and set its value.

string MyString = "This is a string";

Another text data type is a char which stores only one character. A char can store letters, one digit numbers, and any other special character such as !,@,#,etc. When a number is stored in a char it is no longer considered a number because it has become a character. The value of a char must be put between single quotes('). Here is an example of how to set the value of a char.

char c1 = 'a';
char c2 = '5';
char c3 = '#';

There is another type of string called a character array which is similar to the string data type but a lot more complicated to use. It is declared using the word char followed by a set of square brackets. The string data type is better because it is much simpler to use but you should be aware of character arrays. A lot of programmers who program in C like using character arrays in D because that is what they are used to doing in C. Here is an example of how to use a character array.

char[] CharString = "Testing";
writefln(CharString);

You can read what the user types in and store it in a string variable using the din.readLine command. The user has to press the enter key after they have typed what they want to enter when using din.readLine. You need to import std.cstream to be able to use din.readLine. Make sure that you use an uppercase L in readLine otherwise it will not recognize the command. Here is an example where the user is asked to enter something and then it is read into a variable and it is printed out on the screen again.

import std.stdio;
import std.cstream;

void main()
{
   writefln("Please enter something");
   string MyString = din.readLine();
   writefln(MyString);
}

Boolean variables

Boolean variables can only store one of 2 values which are true and false. They don't sound very useful but they are actually very important which you will see later on. Boolean variables are declared with the word bool. Here is an example of how to use a bool.

bool MyBool;
MyBool = true;
MyBool = false;

Type conversions

You can't unfortunately always set the value of a variable of one data type to the value of a variable of another data type. There are a few exceptions such as when both types are in the same group such as integers and floats. When you set the value of a float to the value of an int it will work. If you try to set the value of a float to the value of an int it won't work because the float has extra numbers after the decimal point that the integer can't store. For situations like this you can use the cast command. It takes the type to cast to as a parameter and it must be followed by the variable to cast. Here is an example of how to cast an integer to a float.

float f = 5.123;
int i = cast(int)f;
writefln(i);

Most of the time you will be wanting to convert strings to ints and ints to strings. To convert a string to an int you need to use the toInt command which belongs to the std.conv module. Here is an example of how to convert a string to an int.

import std.stdio;
import std.conv;

void main()
{
   string s = "123";
   int i = toInt(s);
   writefln(i);
}

You can convert an int to a string using the toString command in the std.string module.

import std.stdio;
import std.string;

void main()
{
   int i = 456;
   string s = toString(i);
   writefln(s);
}

Constants

A constant is like a variable but you can't change its value. Constants are used for values that must not be changed by mistake while the program is running. One example is the name of your program. The value of a constant is set when it is declared. The names of constants are usually all in uppercase and they have underscores between words. This makes it clear that they are constants. Here is an example of how to declare and use a constant for your program name.

const string PROGRAM_NAME = "My Program";
writefln(PROGRAM_NAME);

Practice

Try writing a program that asks the user to enter 2 numbers and then adds the 2 numbers together and prints the result on the screen. Remember that the din.readLine command reads in strings which means you will have to convert them to ints before you can add them together.