Jump to content

D (programming language)/Variables and User Input

From Wikiversity

Part of the D Programming Language Course

Variables

[edit | edit source]

A variable is the association of a name with a value of a particular type. This is done so that we can store values and retrieve them later on in a program.

To be more technical about it, the name is a pointer to a memory address that holds the value. The type declaration forces the value to fit into a certain number of bytes.

But we aren't concerned with that at the moment so here are some examples of assigning values to variables.

The integer we're calling x will hold the value of 3.

int x = 3;


The character a will hold the value n.

char a = 'n';


Basic Types

[edit | edit source]

This way of creating variables can be simplified into the following form.

type name = value;

  where type is a data type in D,
    name is the name we'll refer to when we want the value stored,
    value is a value of the same data type as type,
    and = assigns value to name.

The data type of a variable can be any of the following:

  • void - signifies the lack of a data type, like the word nothing
  • bool - shorthand for boolean and holds one of two values, true or false More detail
  • int - shorthand for integer, basically numbers that have no decimal point such as 3, 25, -1
  • float - shorthand for floating-point which are numbers that contain decimal places
  • char - shorthand for character

There are many more types, but since we've just started with the language we'll only look at those five.


Creating Variables

[edit | edit source]

You can create a variable with no value with the following general form:

 type name;

When you do this, the value associated with name is the default of the data type. This is detailed here.

For our basic data types, the default values are:

  • void:-
  • bool: false
  • int: 0
  • float: float.nan, in other words, Not A Number
  • char: 0xFF

Arrays

[edit | edit source]

Creating Arrays

[edit | edit source]

An array is a way of holding multiple objects of the same data type. There are two general forms for creating arrays.

One way creates an empty array that has no size specified.

type[] name;

The other way is to create an empty array that has a size specified.

type[] name = new type[size];

The important item to notice is the inclusion of [] in the type declaration. This tells us that name is an array of type.

Assigning Values

[edit | edit source]

There is one small difference between assigning values to a variable and to an array. With an array, you need to specify exactly which variable in the array, the index, you'll be assign the value to. The general form for this is:

name[i] = value;

The value must be of the same data type as the name array was declared. You cannot, for example, assign a float like 0.0 to an int. Also, i must be more than or equal to 0 and less than the size of the array.

Another way to assign values is when you're creating an array:

type[] name = [values];

The values must be separated by commas.

Getting The Values

[edit | edit source]

To get a value out of an array, all you need to do is specify the index of the variable you'd like to extract the value from.

The general form:

name[i];


Examples

[edit | edit source]

An array of integers.

int[] x = [1, 2, 3];

The values held by x are: 1, 2 and 3.

Modifying the integer array.

x[0] = 4;

The values held by x are: 4, 2 and 3. If we were to print x[1] out, we would see the number 2 on the screen.

Creating a character array, also known as a string.

char[] s = "hello";

This is the same as this:

char[] s = ['h', 'e', 'l', 'l', 'o'];

User Input

[edit | edit source]

Example 1: Reading strings

[edit | edit source]

Now that we know how to store values, we can start using variables for something exciting. The source code for a program that asks for a user to enter their name and then prints out their name is below.

import std.stdio;

void main()
{
    writef("Enter your name: ");
    char[] name;
    name = readln();
    writef("Hello ", name);
}

Line 1 imports the standard input/output module, using the import keyword, so that we can write to the screen and read input from the keyboard.

Line 3 declares the function main which is the starting point of any D program. When you compile a D program and run it, the main function is the first function executed.

Line 5 prints out Enter your name: using the function writef.

Line 6 creates a variable name that is an array of char.

Line 7 reads an array of char until the user presses the Enter Key (also known as Return Key) on the user's keyboard. Whatever has been read by the readln function is then assigned to name.

Line 8 prints out Hello and then the name, again using the writef function.

Lines 4-9 enclose the body of the main function.

Example 2: Reading numbers

[edit | edit source]

To read numbers from the user, you need to import std.conv which contains all the functions that convert strings (char[]) to a number.

import std.stdio, std.conv;

void main()
{
    writef("Enter a number: ");
    char[] text;
    text = readln;
    int number = toInt(text);
    writef("3 + ", number, " = ", 3 + number);
}

Line 7 reads in a line of text from the user. Notice how we've dropped the parenthesis? D allows us to do this when a function requires zero arguments.

Line 8 converts the line of text to an integer and stores it in number.

Line 9 prints out 3 + number = along with the result of 3 + number.

Modules Used

[edit | edit source]