Information Technology Topic
Pascal Program Structure and Data Types
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- A simple Pascal program has a heading, then const and var declarations, then the statements between begin and end. with a full stop at the very end
- The three elementary types are integer for whole numbers, real for numbers with a fractional part, and char for one character written in single quotes
- Declarations come before the statements, so every name and its type are known before the program uses them
Section 1From the plan to the program
In Section 2 you wrote plans in pseudocode: Input a mark, Store a new value, Display the answer. Pascal is the programming language the syllabus uses to turn such a plan into a real program that a computer can run. A pseudocode plan has three jobs. It names the data the program needs, it says what kind of data each name holds, and it lists the steps. A Pascal program has the same three parts, in the same order. Here's the trick: the pseudocode below does the same job as the Pascal program you will meet in this topic. Store 120 in fare Store 3 in riders Store riders * fare in total Display total Everything labelled Pascal-style in these notes follows the ordinary Pascal layout and is accepted by the Free Pascal compiler.
Section 2The layout of a Pascal program
A simple Pascal program is laid out like this (Pascal-style): program Name; const (constants go here) var (variables go here) begin (statements go here) end. The heading is the line program Name; and it says what the program is called. The Free Pascal compiler accepts the heading but ignores it, yet you should still write it so a reader knows what the program is. The const and var sections are the declarations. The statements that do the work sit between begin and end, and the very last end is followed by a full stop. Leaving that full stop out is a compile error.
Section 3A first complete program
Here is a short complete program (Pascal-style). It works out the cost of 3 bus tickets when one ticket costs 120 dollars. program BusFare; const fare = 120; var riders : integer; total : integer; begin riders := 3; total := riders * fare; writeln(total); end. Reading from the top: the heading names the program, const gives a fixed value, and var lists two storage areas, each with a name, a colon and a type. Then the statements run in order: riders becomes 3, total becomes 3 * 120 = 360, and writeln displays 360. A semicolon separates one statement from the next. Spaces and line breaks are only there to make the program easy to read. Pascal also ignores capital letters in names, so Total and total are the same name.
Section 4Declarations come first
The compiler reads the program from the top. A name must be declared before it is used, so the const and var sections come before the begin that starts the statements. If a statement uses a name that was never declared, the compiler reports an error and the program does not run. The same happens if a var section is put after begin. Easy to mix up: declaring a name only reserves a storage area of the right kind. It does not give the area a value. Values are stored by statements, such as the Pascal-style statement riders := 3; A comment is text in curly brackets, such as { cost of one ticket }. The compiler discards comments, so they are notes for the human reader only.
Section 5The three elementary types: integer, real and char
Every variable has a type. The type says what kind of data the variable can hold. This course uses three types, which match the data types you met in Section 2. integer holds whole numbers such as 3, 120 and -7. How large an integer can be depends on the compiler: in Free Pascal's default mode it holds -32768 to 32767, and in other modes it is larger. real holds numbers with a fractional part such as 71.5 or 24.50. A real keeps only a limited number of digits. char holds exactly one character, such as a letter, a digit or a symbol.
Section 6Writing char and number values
A character value is written inside single quotes: 'A', 'y', '7'. The quotes must be single quotes, and exactly one character goes between them. 'AB' has two characters, so it cannot be stored in a char variable. Here's the trick: '7' and 7 are different things. '7' is a character, the symbol seven, and it cannot be used in arithmetic. 7 is an integer and 7.0 is a real. A real written in a program needs digits after the point, as in 7.5. A second way to write a character is #65, which means the character with code 65. The quote form is the one you will use most. Pascal checks types. An integer variable cannot be given a character or a number with a fractional part, so the Pascal-style statements mark := 'B'; and count := 3.5; are compile errors when mark and count are integers.
Section 7Choosing the right type
Ask what the stored data is. A count of things, with no fractions, is an integer: the number of customers, the number of tickets. An amount that can have a fractional part is a real: an average of several marks, or the price of an item in dollars and cents. A single letter or symbol, such as a grade A or the code M for medium, is a char. Watch out: a value that looks like a number is not always an integer. If a mark can come out as 71.5, the variable must be real. And a code letter is a char even if you never calculate with it.
Examples
A pupil in Kingston writes this Pascal-style program: program BusFare; const fare = 120; var riders : integer; total : integer; begin riders := 3; total := riders * fare; writeln(total); end. (a) Name the heading, the constant, the variables and the statement part. (b) State the type of each variable. (c) State what the program displays. (d) Explain why total is an integer and not a real.
(a) The heading is program BusFare; The constant is fare, declared in the const section. The variables are riders and total, declared in the var section. The statement part is everything between begin and end. (b) riders is an integer and total is an integer. (c) riders becomes 3, then total becomes 3 * 120 = 360, so the program displays 360. (d) The number of riders is a whole number and the fare is a whole number of dollars, so their product is a whole number. No fractional part can occur, so integer is enough.
A school in Portmore stores a pupil's mark, average and grade letter in this Pascal-style program: program Report; var mark : integer; average : real; grade : char; begin mark := 64; average := 71.5; grade := 'C'; writeln(mark); writeln(grade); end. (a) Give a reason for each type chosen. (b) State what the program displays, line by line. (c) The pupil changes the grade line to grade := "C"; Explain what happens. (d) The pupil changes the mark line to mark := 64.5; Explain what happens.
(a) mark is a whole number, so integer. average can have a fractional part such as 71.5, so real. grade is a single letter, so char. (b) The first writeln displays 64 and the second displays C, so the output is 64 on the first line and C on the second line. (c) Pascal uses single quotes for a character, so double quotes are a compile error and the program does not run. The line must be grade := 'C'; (d) mark is an integer and 64.5 has a fractional part, so the compiler reports a type error. To keep 64.5 the variable would have to be declared real.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 3, Specific Objective 5 · Syllabus scope
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 16, Programs (the program heading is accepted but ignored by the compiler; the program block holds the statements that run), checked against (https://www.freepascal.org/docs-html/ref/refse111.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 16, Blocks (declarations come first, a name must be declared before it is used, and the statement part comes after the declarations), checked against (https://www.freepascal.org/docs-html/ref/refse115.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Ordinal types and Integer (the range of Integer depends on the compiler mode), checked against (https://www.freepascal.org/docs-html/ref/refsu4.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Real types (numbers with a fractional part, limited digits), checked against (https://www.freepascal.org/docs-html/ref/refsu5.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Char (one character, written in single quotes), checked against (https://www.freepascal.org/docs-html/ref/refsu6.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 4, Variable declaration (name, colon, type, semicolon), checked against (https://www.freepascal.org/docs-html/ref/refse22.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 13, Compound statements (begin ... end groups statements separated by semicolons), checked against (https://www.freepascal.org/docs-html/ref/refsu55.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 1, Comments (text in braces is discarded by the compiler), checked against (https://www.freepascal.org/docs-html/ref/refse2.html) · Facts