Thursday 29 October 2009

Delphi Tutorial Part 1

Variables
Variables in programming are used to hold useful bits of information. Numbers, characters, words, lists, memos, and any kind of data that needs to be referenced to, should have an associated variable. Variables in Computing also need to be assigned to different classes. These will be covered in a latter part of the tutorial, but in summary, they tell us what kind of data input there should be, and restrain us from using inappropriate data. In Delphi for example, the string class holds 225 bytes of data (255 characters), the integer class can hold any number between -2147483648 and 2147483647, and the double class can hold a decimal value up to 308 or negative 308 base 10. These three classes are the only ones you will need for now, but other very useful classes will covered in the course of this tutorial. A variable should proceed var in the main program with a unique name, followed by a colon and the class. Below I have wrote a sample program to show how a variable can be used to store your name, and give you a welcome message.


program MyFirstProgram;

var
nameVar: String; //name variable

begin
writeln('What is your name? ');
readln(nameVar);
writeln('Hello '+nameVar+' how are you doing today?');
readln();
end.


Since you probably only just discovered the language five minutes ago, we will need to cover some syntax here.

Every application in Pascal must begin with the word Program and there by followed by the name you would like to assign to it. This is our first command in our program, so therefore like any other instruction we need to terminate the command with a semi-colon. After this we declare our list of variables following var and then we initiate the main body of our program with the word begin. Thankfully we end our program just as easily using the word end. Programmers often refer to these as the beginning and the end of a clause. writeln() is used to output text to the screen, and an input is taken from the next line using readln() where hopefully the user should type in their name. Note, the name of the variable has been placed in between the brackets in order to return the value we need. You may notice at the end of of our writeln statement, we have added text to the end of it by using two '/' characters. This is known as a comment, and in bigger and more commercial situations these permit us to post reminders or pointers for you or any colleagues you may have. Then lastly, the writeln command is used again to output text but with the addition of the user's name. A variable here can almost be treated like a number, the name is inserted just by using addition signs and the name of the variable. The final line uses a read command with no output variable, to ensure that the text can be read by the person. Without this the program would just instantly end on the input of your name.

Variables of the Integer or floating point class can be treated similarly. The main difference is that only values can be added to them as opposed to text. We could have a variable name called a, and then assign a value to this using an equals sign. Likewise we could do the same with a variable named b, add the two together and then return this value to a third variable (called c maybe).


program AnotherProgram;

var
a: Integer;
b: Integer;
c: Integer;

begin
a:=1;
b:=1;
c:=a+b;
writeln(c);
readln();
end.


Delphi was made to assign values to variables by using the colon and the equals sign, but it is worth remembering that the standard is just the equals sign.
The next part of the tutorial will be about conditional statements, to help you understand how to trigger actions under certain events where necessary.

No comments:

Post a Comment