Advertise here.

Tuesday 28 August 2012

Pascal Programming Lesson #3 - Variables and Constants


Variables are names given to blocks of the computer's memory. The names are used to store values in these blocks of memory.

Variables can hold values which are either numbers, strings or Boolean. We already know what numbers are. Strings are made up of letters. Boolean variables can have one of two values, either True or False.

Using variables

You must always declare a variable before you use it. We use the var statement to do this. You must also choose what type of variable it is. Here is a table of the different variable types:

Byte0 to 255
Word0 to 65535
ShortInt-128 to 127
Integer-32768 to 32767
LongInt-4228250000 to 4228249000
Realfloating point values
Char1 character
Stringup to 255 characters
Booleantrue or false

Here is an example of how to declare an integer variable named i:

program Variables;

var
   i: Integer;

begin
end.

To assign a value to a variable we use :=.
program Variables;

var
   i: Integer;

begin
   i := 5;
end.

You can create 2 or more variables of the same type if you seperate their names with commas. You can also create variables of a different type without the need for another var statemtent.

program Variables;

var
   i, j: Integer;
   s: String;

begin
end.

When you assign a value to a string variable, you must put it between single quotes. Boolean variables can only be assigned the values True and False.

program Variables;

var
   i: Integer;
   s: String;
   b: Boolean;

begin
   i := -3;
   s := 'Hello';
   b := True;
end.

Calculations with variables

Variables can be used in calculations. For example you could assign the value to a variable and then add the number 1 to it. Here is a table of the operators that can be used:

+Add
-Subtract
*Multiply
/Floating Point Divide
divInteger Divide
modRemainder of Integer Division

The following example shows a few calculations that can be done:

program Variables;

var
   Num1, Num2, Ans: Integer;

begin
   Ans := 1 + 1;
   Num1 := 5;
   Ans := Num1 + 3;
   Num2 := 2;
   Ans := Num1 - Num2;
   Ans := Ans * Num1;
end.

Strings hold characters. Characters include the the letters of the alphabet as well as special characters and even numbers. It is important to understand that integer numbers and string numbers are different things. You can add strings together as well. All that happens is it joins the 2 strings. If you add the strings '1' and '1' you will get '11' and not 2.

program Variables;

var
   s: String;

begin
   s := '1' + '1';
end.

You can read vales from the keyboard into variables using Readln and ReadKey. ReadKey is from the crt unit and only reads 1 character. You will see that ReadKey works differently to Readln

program Variables;

uses crt;

var
   i: Integer;
   s: String;
   c: Char;

begin
   Readln(i);
   Readln(s);
   c := ReadKey;
end.

Printing variables on the screen is just as easy. If you want to print variables and text with the same Writelnthen seperate them with commas.

program Variables;

var
   i: Integer;
   s: String;
begin
   i := 24;
   s := 'Hello';
   Writeln(i);
   Writeln(s,' world');
end.

Constants

Constants are like variables except that their values can't change. You assign a value to a constant when you create it. const is used instead of var when declaring a constant. Constants are used for values that do not change such as the value of pi.

program Variables;

const
   pi: Real = 3.14;

var
   c, d: Real;

begin
   d := 5;
   c := pi * d;
end.

0 comments:

Post a Comment