1 / 33

Introduction to Pascal

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.

lundy
Download Presentation

Introduction to Pascal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Pascal A High Level Language

  2. What is Pascal? • High Level Language • Designed 1967-71 by Niklaus Wirth • It is a complier and it is used in teaching programming principles

  3. 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

  4. 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

  5. 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

  6. 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)

  7. 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

  8. 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

  9. 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

  10. What are variables? • Variables are placeholders in memory • A variable must be declared • Var stands for variable Some variable types

  11. 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.

  12. 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

  13. What are Constants? • Data which never changes • Example: the value of pi, circumference of a circle, is always the same. 3.14159

  14. 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’);

  15. 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

  16. 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

  17. 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’);

  18. 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

  19. 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

  20. For… To… Do… Loop Counter := start number Action or actions Flowchart yes End loop

  21. For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number

  22. For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number

  23. For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number

  24. For… To… Do… Loop Counter := start number Action or actions Increment counter Flowchart yes End loop Counter > end number

  25. For… Downto … Do Counter := start number Action or actions Decrement counter Flowchart yes End loop Counter > end number no

  26. 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.

  27. 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

  28. 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.

  29. 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.

  30. 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;

  31. 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.

  32. 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

  33. 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!

More Related