Thread: Pascal Tutorial
View Single Post
  #2 (permalink)  
Old 03-16-2008, 08:40 AM
PascalPro PascalPro is offline
Newbie
 
Join Date: Mar 2008
Posts: 9
Rep Power: 0
PascalPro is on a distinguished road
Default Re: Pascal Tutorial

Welcome!

In this lesson we will be teaching you how to ask questions and return feedback. Before we do that you must learn the basics of variables. There are many types of variables : string (A series of characters), integer(between about -30000 to +30000, numbers), and Boolean ( Has two values, true of false) To define variables we must use this format :
Code:
var
MyName : String; (* Sets the variable 'MyName' as a string *)
MyAge : Integer (* Sets the variable 'MyAge as a integer *)
AreYouRich : Boolean; (* Sets the variable 'AreYouRich' as a Boolean *)
Variables always come after the program name and before the 'Begin'. There are other variables like ShortInt, LongInt, Extended, Real, and Array. We wont cover these until later in the tutorial. If you have 2 or more integer variables you can put them on the same line seperated by a comma like so :
Code:
var
MyAge, MomsAge, DadsAge : Integer;
Now! You will ask your first 'Pascal' question. We must always start with Program name and begin. Now, will the answer to your question be a string, integer, or boolean? In this example we will use string. So you have your variables and your code should look like this
Code:
Program CopyCat;

var
WhatToCopy : String;
Now we will use the write command to ask a question.

Code:
Program CopyCat;

var
WhatToCopy : String;

begin
Write('What would you like us to copy? ');
Readln(WhatToCopy);
The write command asks the question. When the user types in a answer, the answer is then saved in the WhatToCopy variable by the readln command. Readln usually comes before the write command. Now you have the answer stored, so how are you going to sent it back out? With write of course. Your final program should look like this

Code:
Program CopyCat;

var
WhatToCopy : String;

begin
Write('What would you like us to copy? ');
Readln(WhatToCopy);
Writeln(WhatToCopy);
Readln;
End.
The readln before the main 'End.' makes the characters on the screen stay until the user presses enter. Now copy and paste this into your compiler. Presto! Notice how when your question and answer shows up the is stuff at the top. If you want to get rid of it we use the crt command which is defined as a use before the variable like so :
Code:
Program Uses;

uses
crt;

var
(* All variables here *)

begin
(* Code goes here *)
End.
If you would like to use the crt use to clear the screen your final program would look like this :

Code:
Program CopyCat;

uses
crt;

var
WhatToCopy : String;

begin
ClrScr; (* Clears screen and makes program easier to read *)
Write('What would you like us to copy? ');
Readln(WhatToCopy);
Writeln(WhatToCopy);
Readln;
End.
Just remember that uses come before variables so if you compile code like this

Code:
var
(* Put Variables Here *)

uses
crt;
You will get an error! Have fun until next time!

Next Lesson Includes : Procedures

Happy Programming!
Reply With Quote