Download presentation
Presentation is loading. Please wait.
Published byShauna Austin Modified over 9 years ago
1
INFORMATION TECHNOLOGY CSEC CXC 10/25/20151
2
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! 10/25/20152
3
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 10/25/20153
4
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 Data type Range of values which the variable is capable of storing Integer Whole numbers from -32768 to 32767 Byte The integers from 0 to 255 Real Floating point numbers from 1E-38 to 1E+38 Boolean Can only have the value TRUE or FALSE Char Any character in the ASCII character set Shortint The integers from -128 to 127 Word The integers from 0 to 65535 Longint The integers from -2147483648 to 2147483647 10/25/20154 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.
5
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. 10/25/20155 var Identifier1, Identifier2: Data Type Identifier3 : 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; const Identifier1 = value; Identifier2 = value; Identifier3 = value;
6
Write a program in Pascal to display the phrase Hello, World! PROGRAM HelloWorld (output); BEGIN Writeln ('Hello, World!'); END. 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 10/25/20156 2. Body – this refers to the statements to be executed by the Compiler.
7
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. 10/25/20157 Name Age#Room Team 1 Full
8
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. 10/25/20158
9
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; 10/25/20159
10
10 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).
11
10/25/201511 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.
12
10/25/201512 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:
13
10/25/201513 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.
14
PROGRAM First; {Demonstrates the use of Write Statements} BEGIN Write (‘This is our first example of output.’); END. 10/25/201514 OUTPUT ON SCREEN This is our first example of output. PROGRAM First; BEGIN write (‘This is our first example of output’.) BAD CODING
15
10/25/201515 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 PROGRAM OneLine (output); BEGIN write (‘My name’); write (‘is Andrew’); Readln; END. write (‘My name’); write (‘ is Andrew’); SPACE USED
16
10/25/2015 16 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
17
10/25/201517 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. This is the land of my birth! this is Jamaica, my Jamaica, this is the land of my birth. 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.
18
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. 10/25/201518 Length = 152 Width = 32 Area = 4864 Program that takes the value for Width as 32’ and Length 152’ of a Rectangle and calculate the Area.
19
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. 10/25/201519 Output Length = 15 Width 20 Area = 250 Length = 20 Width 20 Area = 400
20
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. 10/25/201520 Salary Week 1 =$500.29, Salary Week 2 = $500.01, and Total Salary = $1000.30 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.
21
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. 10/25/201521 PROGRAM Distance (output); VAR Time, Velocity, Distance: real; BEGIN Time:=.3; Velocity:= 24; Distance:= Time * Velocity; writeln (‘Distance travelled is =‘, Distance); END. Distance Travelled = 7.2
22
PROGRAM Area (input, output); VARLength, 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. 10/25/201522 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.
23
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. 10/25/201523 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 =
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.