# Wikijunior:Programming for Kids/Variables and Data Types

 Wikijunior:Programming for Kids Writing Your Algorithms Variables and Data Types Inputs, Outputs and Processes

Now that we've distinguished between statements and expressions, we need to know what data types are.

## Data types

When we program, we need to work with different kinds of data, which is why data types are very useful. Common data types include:

• Logical data types: The most common logical data type is the Boolean data type. It holds only two values: true and false.
• Textual data types: The most common textual data types are characters and strings. A character is a single symbol, which can be a letter, a digit, a symbol, or a Chinese character, etc. A string is a series of characters, and for this reason, they are sometimes considered to be data structures (see below). Most programming languages require textual data to be surrounded by single or double quotes.
• Numeric data types: Complex numbers aside, the most common numeric data types are integer and real. An integer is a number with no decimal places, such as -4, 0 and 45. Integers can be further divided into unsigned integers and signed integers. Unsigned integers cannot possibly be negative, e.g. the population of a country. Signed integers can be positive or negative, e.g. the floor of a building (the first level of the basement is -1). Real numbers can have decimal places. They can be subdivided into floating-point numbers, fixed-point numbers and rational numbers. Fixed-point numbers and rational numbers are represented as fractions, while floating-point numbers are represented in scientific notation. A detailed discussion of the three types are beyond the scope of this book.

The choice of data type depends greatly on the programming language. Some programming languages, such as Java and SQL, provide many different numeric data types suited for different purposes. Others, including JavaScript, have one generic Number type and the compiler or intepreter will do all the dirty work (deciding when to use which type) for you.

Sometimes, we need to store data in a structured manner. The most common structures are date and time, which are stored in different ways, depending on the language. We will learn the common abstract data structures and how they are implemented as arrays in a later chapter. Apart from arrays, date and time, objects are another common way to store structures of data. In an object-oriented language, date, time and arrays are all objects.

## Variables and constants

A variable is a 'jar' that stores data, objects (in an object-oriented language) or references. It can change during runtime. A constant is a datum that does not change during runtime. Good examples of constants are 6, 5, "Hello"/'Hello', etc. Some programming languages have special constants like `Infinity`.

## Choosing identifiers, data types and constraints

An identifier is a name for a particular item in a program. It can be a subprogram, a variable, a constant, a data type, a data structure or, in some object-oriented programming languages, a class.

Let's say, for some reason, we decided to give identifiers to the numbers 1 to 4. 1 is called Mary, 2 is called John, 3 is called Peter and 4 is called Susan. What if we decide to call 5 Mary again? That would cause a lot of problems! When we call Mary, we don't know if we're calling 1 or 5. That's where namespaces come in. A namespace is a space in which all the identifiers have a unique name. If our 1 and 5 are in the same namespace, we can't call 5 Mary any more; we need to choose another name, say, Harry.

Namespaces don't only apply to the same type of identifier. If we've already 1 is called 'Mary', we cannot call a variable, a subprogram or a data type 'Mary'.

[Note: When we refer to a constant by itself, e.g. 1 or 4 and not Mary or Susan, we are addressing its literal. There are other types of literals that we will discuss later.]

Different programming languages have different rules for identifiers. For example, PHP requires that all variable names start with \$. Apart from the rules, there are also conventions we should follow in order to enhance code readability. For example, in JavaScript, it would be a bad idea to start a variable's name with a capital letter. We should familiarise ourselves with the naming rules and conventions for a particular language before we act!

Another thing to consider is case sensitivity. Some languages, such as JavaScript, are case sensitive. Therefore, 'aGe', 'Age', 'AGe' and 'age' are different identifiers. Others, including Pascal, are case insensitive, so 'aGe', 'Age', et. al., are all the same thing.

Let's take another look at our program for calculating the area of a stadium. We'll change the input and output names to identifiers. (We'll use JavaScript identifiers.)

Module: 'Find the total cost of the project'
Outputs Processes Outputs
• flowerbedWidth
• flowerbedLength
• fullWidth
• fullLength
• costPerUnit
• Prompt the user to input the dimensions of the flowerbed, the dimensions of the whole area and the cost per unit
• Get the dimensions of the flowerbed, the dimensions of the cost per unit
• Validate the input data
• Find the area of the path
• Multiply the area and the per-unit path
• Output the total cost of the project to the screen
• totalCost
CamelCase looks like the humps of a camel.

Note that we smashed the words together. This is called CamelCase. It is very common in some programming languages, including JavaScript.

Now let's look at the inputs and ouputs in greater detail. We want to know the data type and accuracy of each item.

Module: 'Find the total cost of the project'
Input Data type Constraint Remarks

flowerbedWidth

Real

Three decimal places

In metres

flowerbedLength

Real

Three decimal places

fullWidth

Real

Three decimal places

fullLength

Real

Three decimal places

costPerUnit

Real

Two decimal places

May be one or two decimal places depending on currency

Input Data type Constraint Remarks

totalCost

Real

1-2 decimal places

May be one or two decimal places depending on currency

## Declarations

Although algorithms are not specific to any language, you might be anxious to try out what we've learnt. Therefore, we'll give you a choice between Pascal and JavaScript.

Pascal is a procedural programming language. Pascal is a compiled language and in order to use it, you need to use an integrated development environment (IDE) to write the code, and a compiler to turn the code into a low-level language for the computer to interpret. Examples of Pascal IDEs are DevPascal and Lazarus, both of which use the FreePascal compiler. Here is what a Pascal program looks like:

```PROGRAM myprogram;
CONST
my_constant: integer = 6;
VAR
my_variable: integer;
BEGIN
my_statement;
END.
```

JavaScript is a prototype-based language used for web pages, though we will only use its procedural features in this book as a detailed discussion about prototype-based programming would fall outside the scope. To use JavaScript, copy the following into Notepad and save it as myPage.html:

```<html>
<script>
const MY_CONSTANT = 6;
var myVariable = 7;
</script>
<body>
</body>
```

Then open the file in a web browser of your choice (Chrome, Internet Explorer, Firefox, Safari or Opera). [Note: the code enclosed in < and > are HTML tags. HTML is a markup language for displaying content in a web browser. It is not a programming language.]

In the above sample programs, we wrote two declarations one for a variable, the other for a constant. A declaration must involve an identifier. Depending on the language, it may also involve strong typing and initialisation. In order to better understand this concept, let's look at how we declared or variable again.

Pascal JavaScript
`my_constant: integer = 6;` `const MY_CONSTANT = 6;`

In our Pascal code, we first typed in the identifier, then a colon, then the data type: integer. Finally, we put in an equal sign and a literal showing the constant's value. We terminated the declaration with a semicolon. It looked like this:

`identifier:datatype = value;`

In our JavaScript code, we first typed in the keyword 'var', then the identifier. Then, we put in an equal sign and a literal showing the constant's value. We terminated the declaration with a semicolon. It looked like this:

`var identifer = value;`

There was no type declaration in our JavaScript code. This is because the browser decides for us what data type it will use. We are not in a position to change it. In our Pascal code, however, we tell the compiler explicitly that we want the constant to be an integer. Therefore, Pascal is said to be a strongly typed language and JavaScript a weakly typed language. After that, we assigned a value to the constant, and this is called initialisation.

The purpose of a declaration is to tell the compiler or interpreter to set aside some memory space in the client's RAM. However, some languages, such as PHP, do not require variable declarations at all. The memory space is reserved the moment we first assign a value to the variable. Furthermore, we don't need to write out our declarations in flowcharts or pseudocode.

In our JavaScript example, the equal sign (=) is also the assignment operator of the language, which brings us to our next chapter...