330 likes | 670 Views
Introduction to Pascal Programming. INFORMATION TECHNOLOGY CSEC CXC. Pascal Programming. PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides a teaching language that highlights concepts common to all computer languages
E N D
Introduction to Pascal Programming INFORMATION TECHNOLOGY CSEC CXC
Pascal Programming PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides a teaching language that highlights concepts common to all computer languages standardizes the language in such a way that it makes programs easy to write Strict rules make it difficult for the programmer to write bad code!
Pascal Background Allows programmers to use complex data types (structured) easier to build dynamic and recursive data structures such as lists, trees and graphs Blaise Pascal , June 19, 1623 – August 19, 1662
Data Types - defines a variable (data item) in such a way as a range of values which the variable is capable of storing, and it also defines a set of operations that are permissible to be performed on variables of that type Please note that four of these types of data (char, shortint, word, and longint) are not a part of the standard Pascal definition but are included as extensions to the TURBO Pascal compiler.
Declaration Statements Declaring Constant/Variables These statements are used to announce the Variables and Constants that are to be used in the program as well as the appropriate Data Type format they will take. constIdentifier1=value;Identifier2=value;Identifier3=value; varIdentifier1 , Identifier2: Data TypeIdentifier3 : Data Type; const Name = 'Tao Yue'; FirstLetter = 'a'; Year = 1997; pi = 3.1415926535897932; UsingNCSAMosaic = TRUE; var age, year, grade : integer; circumference : real; LetterGrade : char [1]; DidYouFail : Boolean;
Parts of a Pascal Program: 1. Program Title (Header) – this tells the name of the program and may state the actions the program carries out. The title should not contain spaces or start with a number or have in a special character sign Sample Pascal Program Write a program in Pascal to display the phrase Hello, World! PROGRAM HelloWorld (output); BEGIN Writeln ('Hello, World!'); END. 2. Body – this refers to the statements to be executed by the Compiler.
Use of Data Identifiers An Identifier is a name given to an item of data in a program. The Identifier name once used can only represent that one item of data when called or used in a program. The Identifier name should not begin with a number, special character or contains special characters. Name Age #Room Team 1 Full
Constant – Use of! Constants represents a value of data that remains fixed is not designed or expected to change. E.g. the number of days in a week could be set to 7 as there can be only 7 days in 1 week. The number of ounces in a pound of flour will always be 16 ounces represents 1 pound.
Assignment statements Assignment Statements in programming are used to give a value to a variable/constant. The Assignment Statement can also change the value of a variable or constant. How to use this statement: variable_name:=expression; E.g. carry a single value some_real := 385.385837; or an arithmetic sequence some_real := 37573.5 * 37593 + 385.8 / 367.1;
Operand Operation div and mod only work on integers. / works on both reals and integers but will always yield a real answer. The other operations work on both reals and integers. When mixing integers and reals, the result will always be a real since data loss would result otherwise. This is why Pascal uses two different operations for division and integer division. 7 / 2 = 3.5 (real), but 7 div 2 = 3 (and 7 mod 2 = 1 since that's the remainder).
Good and Bad practice Pascal compiler will not know if its to Multiply or Subtract. Use parentheses. Above is Pascal code not indented or properly, which makes for hard reading. Instead, indents are used on the right.
Sample Pascal Codes Useful for on screen output See example on Average of 5 numbers using constants and displaying results. Use of read and readln read treats input as a stream of characters, with lines separated by a special end-of-line character. readln, on the other hand, will skip to the next line after reading a value, by automatically moving past the next end-of-line character:
Sample Pascal Codes Use of the Write Statements Used to communicate results to the outside world. Used to print words on the terminal screen. Write (‘This is our first example of output.’); This is our first example of output.
Use of Write Statements PROGRAM First; {Demonstrates the use of Write Statements} BEGIN Write (‘This is our first example of output.’); END. OUTPUT ON SCREEN This is our first example of output. BAD CODING PROGRAM First; BEGIN write (‘This is our first example of output’.)
With using the write statement in Pascal Programming, the write statement when used, leaves the cursor where it is after printing is complete. write (‘My name’); write (‘is Andrew’) My nameis Andrew SPACE USED PROGRAM OneLine (output); BEGIN write (‘My name’); write (‘is Andrew’); Readln; END. write (‘My name’); write (‘ is Andrew’);
USE OF WRITELN STATEMENT With the use of the writeln statement for output, the compiler returns the cursor to the next line on screen. PROGRAM TwoLines (output); BEGIN writeln (‘My name’); writeln (‘is Andrew’); END. My name is Andrew
Use of Write and Writeln PROGRAM Song (output); BEGIN write (‘This is the land’); writeln (‘ of my birth!’); write (‘This is the land’); writeln (‘ of my birth!’); writeln (‘this is Jamaica, my Jamaica,’); writeln (‘this is the land of my birth.’); Readln END. Write a program in Pascal that displays the first stanza of the song “This is the land of my birth” by Eric Donaldson. Each line of output, should start on a new line. Output This is the land of my birth! This is the land of my birth! this is Jamaica, my Jamaica, this is the land of my birth.
Program that takes the value for Width as 32’ and Length 152’ of a Rectangle and calculate the Area. Demonstration of Variable declaration statements PROGRAM Storing (output); {Demonstrates use of variables} VAR Length, Width, Area: integer; BEGIN Length := 152; Width := 32; Area := Length * Width; writeln(‘Length = ‘, Length, ‘ Width = ‘, Width, ‘ Area = ‘ , Area); Readln; END. Length = 152 Width = 32 Area = 4864
Output Length = 15 Width 20 Area = 250 Length = 20 Width 20 Area = 400 PROGRAM Storing (output); {Demonstrates use of variables with assigned values}; VAR Length, Width, Area: integer; BEGIN Length:= 15; Width:= 20; Area:= Length * Width; writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘ ‘Area); Length:= 20; Area:= Length * Width; writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘ ‘Area); END.
Program that takes the value of two salary amounts $500.29 and $500.01 and provide the sum of both stored in a variable Total. Demonstration of Variable declaration statements PROGRAM Salary (output); {Adds two salaries}; VAR Salary1, Salary2, Total: real; BEGIN Salary1:= 500.29; Salary2:= 500.01; Total:= Salary1 + Salary2; writeln (‘Salary Week 1 =$’, Salary1, ‘Salary Week 2 =$‘, Salary2, ‘and Total Salary =$’, Total);END. Salary Week 1 =$500.29, Salary Week 2 = $500.01, and Total Salary = $1000.30
Try this~! PROGRAM Distance (output); VAR Time, Velocity, Distance: real; BEGIN Time:= .3; Velocity:= 24; Distance:= Time * Velocity; writeln (‘Distance travelled is =‘, Distance); END. Write a program code in Pascal to take a time of .3 seconds and velocity of 24 and calculate the distance by use of the formula Time travelled times Velocity and output this distance on a new line. Distance Travelled = 7.2
Write a program in Pascal to accept the Length and Width of a piece of lumber along with the price and provide the cost where Cost = Length x Width x Price. An output should be provided. Use of screen prompts PROGRAM Area (input, output); VAR Length, Width: integer; Cost, Price: real; BEGIN writeln (‘Type in Length, Width and Price’); readln (Length, Width, Price); Cost:= (Length * Width) * Price; writeln (‘The cost is:=$ ‘, Cost); END.
Write a program in Pascal that calculates the volume of a shape provided with the Pi as 3.14159 and Height being 6. The program should accept Radius and calculate the volume where Volume = PROGRAM AreaVolume (input, output); CONST Pi = 3.14159; H = 6; VAR Volume, R: real; BEGIN writeln (‘Type in the radius.’); readln (R); Volume:= Pi * (R * R) * H; writeln (‘Volume = ‘, Volume); END.