Introduction

Basics of Programming

Variables

Input and Output

Boolean Expressions and Branching

Loops

Functions and Procedures

Files

Arrays

HOME

# CHAPTER 3.

## Variables

All programs, except for the most simple ones, must save information in the computer's memory. To access memory, programmers must use variables. Variables are identifiers that refer to parts of the computer's memory.

The following program shows examples of how variables can be used. Please note that the comments explain how the program works

```Program VariableIntro;

var
a : integer;    {declare an integer}
x : integer;
y : real;       {declare a real}
z : char;       {declare a char}

Begin
x := 1;     {assign the number 1 to x}
Writeln(x);

y := 2.5;   {assign the number 2.5 to y}
Writeln(y);

z := 'R';   {assigns the letter R to z}
Writeln(z);

a := x;  {assigns the contents of x to a}
Writeln(a);

a := 2 * x;     {multiplies x by 2}
Writeln(a);     {and assigns it to a}

End.```

This example introduces variables. Before variables can be used they must be declared. This is done in the declaration section. The declaration section must come directly after the program heading, and is marked by the VAR keyword.

The following is the format for a variable declaration.

 name : type;

The first word is the name of the variable and must be a valid identifier. The name is followed by a colon. After the colon comes the type of data the variable will hold. Data types will be explained in detail later in this chapter. The last part of the declaration is a semicolon.

### Data Types

All variables must have a data type. Pascal has several data types. Here are the most common ones:

 Integer Holds numbers from -32,768 to 32,767 Real Holds numbers with digits after the decimal Char Holds single characters such as letters, digits, and special characters String Holds any combination of up to 256 characters

### Assigning values to Variables

For a variable to be useful it must contain data. The ":=" operator allows data to be assigned to a variable. The example at the beginning of this chapter shows how data is assigned to variables. It also shows how the contents of one variable can be assigned to another variable.

### Math Operators

One of the main reasons for using variables is to perform mathematical operations. Pascal provides math operators to perform the four basic math operations: addition, subtraction, multiplication, and division. The following are examples of mathematical operations in Pascal.

 Subtraction x := 2 - 1

 Multiplication x := 2 * 4;

Division is slightly more complicated than the examples above. Pascal provides several division operators that perform the task differently. The "/" operator divides two numbers and returns an answer with a decimal. The "DIV" operator divides two numbers and returns an integer answer. It truncates anything that comes after the decimal place. The "MOD" operator divides two numbers and returns the remainder if any. See the following examples.

 `x := 5 / 2 {x = 2.5}` `x := DIV 2; {x = 2}` `x := MOD 2; {x = 1 (remainder) }`

### Using Writeln with Variables

In the previous chapter we used Writeln to print a string on the screen. Writeln can also be used to print the contents of a variable on the screen. In the sample program at the beginning of this chapter there is an example of how Writeln prints the contents of a variable. Writeln will be explained in depth in the Input/Output chapter.