1.48k likes | 3.01k Views
Introduction to Pascal. A High Level Language. What is Pascal?. High Level Language Designed 1967-71 by Niklaus Wirth It is a complier and it is used in teaching programming principles. A Simple Pascal Program. program first; begin writeln (‘Hello World’); end.
E N D
Introduction to Pascal A High Level Language
What is Pascal? • High Level Language • Designed 1967-71 by Niklaus Wirth • It is a complier and it is used in teaching programming principles
A Simple Pascal Program program first; begin writeln (‘Hello World’); end. Name of the program It is very important to put the right punctuation in your program. Reserved Words
Program and Program code • The process begins in the text editor • Here you write the program code • It is saved as a .PAS extension • Computers cannot understand Pascal text; the language has to be converted to machine code • This is done through the compiler
How does the compiler work? Stage 1: check the code for syntax errors (mis-spelled or mis-used commands, punctuation or items out of sequence) If the compiler finds any errors, it will report them and stop working. The programmer has to debug the program. It works in 2 Stages
How does the compiler work? Stage 2: the code is corrected and passed through the compiler again. When all the errors have been removed then the code is turned into an executable program (exe)
What are some reserved words? • Write or writeln (‘Text’): displays what ever is seen on the screen • Readln: reads an item from the user and places it in the variable in memory • Clrscr : Clears the screen • Uses crt: Calls a library of commands, which helps with displaying items on the screen • Textcolor (color): changes the color of the text
Using Color and Formatting the Output • There are 16 colors available on Pascal’s palette. • Textcolor (lightblue) will change the color to lightblue • Textbackground will change the background • Gotoxy starts writing in a particular position on the screen. At the x and y coordinates
Input In Pascal an input statement is achieved by using Readln. Readln can be used with text as well as a numeric value. Readln on its own may be used to pause the program until ENTER is pressed. Output Is achieved by using either Write or Writeln. Writeln with single quotes outputs text. Writeln without single quotes output the value. Writeln on its own outputs a blank line. Input and Output Statements
What are variables? • Variables are placeholders in memory • A variable must be declared • Var stands for variable Some variable types
What do the variables stand for? • Byte : can store a number between 0 and 255 • Integer : can store a number between –32768 to 32767 (no fractions) • Real : can store a number which are positive, negative, whole or fractional. • Char : can store a single character @; t; 6 • String : can store a set of zero or more characters.
Variable names • Names must be single words, but can normally be of any length • Ideal name is one that shows clearly what it is used to store –short and easy to type
What are Constants? • Data which never changes • Example: the value of pi, circumference of a circle, is always the same. 3.14159
What is the Assignment Statement? • This is used to fill in the storage location, which have been declared as variables, which actual data items and also for the purpose of processing those data items. Example Num4:= 25; Address := (’25 High Street’);
Mathematical Operations • Some basic operations which Pascal uses are: + is used for addition - is used for subtraction * is used for multiplication / div mod is used for division mod returns a remainder div returns the integer
What are Conditions? • Pascal uses Boolean expressions as a variable True / False • Pascal also supports other expressions: = is equal to <> not equal to > greater than < less than >= greater than <= less than and equal to and equal to
IF…Then… Else… • Pascal uses IF…Then… Else… as conditional statements. • An example of a conditional state is… If it rains, then I’ll go to the cinema. If x= 0 then write (‘wrong’); If it rains, then I’ll go to the cinema otherwise I’ll go for a walk. If x= 0 then write (‘wrong’) else write (‘correct’);
What types of errors can I get? • There are 3 main types of errors • Syntax errors :errors is the reserved language • Logical errors : errors in the logic • Run-Time errors :errors which occur during the running of the program
What are Loops? • A loop in its simplest terms is a structure that make a block of instructions repeat any number of times. • In Pascal there are 3 main loops for… to… do… used for a definite amount repeat… until… indefinite amount while… do… indefinite amount
For… To… Do… Loop Counter := start number Action or actions Flowchart yes End loop
For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number
For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number
For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number
For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number
For… Downto … Do Counter := start number Action or actions Decrement counter Flowchart yes End loop Counter > end number no
Mathematical Functions • These are a list of commands mathematical functions used by Pascal ROUND (x): returns the value of x to the nearest whole number. TRUNC (x) : returns the number before the decimal point INT (x) : returns the number before the decimal point as an integer.
More Mathematical Functions SUCC (x) : returns x + 1 ; the successor of x PRED (x) : returns x – 1; the predecessor of x SQR (x) : returns the square of x SQRT (x) : returns the square root ABS (x) : absolute value EXP (x) : returns to the power of SIN (x) : returns the sine COS (x) : returns the cosine ARCTAN (x) : returns the arctangent
Manipulating Strings • Strings are a collection of characters • We use the function length to see how long the string is. For example: length (phrase) • Copy: returns a sub string of a string • Ord : returns the ASCII value for a character • You can convert a integer to a string and vice versa.
Using Procedures • A procedure is simply a program within a program • Like a program it must have a name • It can have its own variables known as local variables or it could use global variables used from the program • Uses begin and end, but instead of a full stop you use a semi-colon.
Using Functions • Are similar to procedures • They have their own variables and can accept parameters too. • The main difference is that a function returns a value back to the program Example Function maximum (a, b : integer ): integer;
Arrays • Is a collection of variables of the same type. Example Var Array_name :array [1..10] of integer; Each of these 10 places in the memory will be an integer.
Generating Random Numbers • Sometimes we need to generate random numbers during a program • There are 2 methods 1. Random : generates a random number between 0 and number 2. Randomize : generates different numbers every time the same program is re-run
How can I get a free Pascal compiler? • There are many different versions of Pascal • The most popular is Turbo Pascal • We will be using Free Pascal Version 1.0.6 • You can get a copy at www.freepascal.org Make sure you follow the directions carefully!