Information Technology Topic
Declaring Variables and Constants
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- A var section lists each variable as a name, a colon and a type, and a const section lists each constant as a name, an equals sign and a value
- Choose meaningful names, give every variable a starting value before it is used, and keep fixed values in constants so a change is made in one place
- The compiler refuses a program that assigns a new value to a constant, uses an undeclared name, or declares a name in the wrong form
Section 1Plan the names first
In Section 2 you met variables and constants in pseudocode: Store 0 in total gives a variable its first value, and a fixed value such as a price is stored once and never changed. Before writing a Pascal program, list every name the plan uses. For each one decide three things: is it a variable or a constant, what type does it hold, and what is its starting value? Here's the trick: build a small table. For a tuck shop it could say: price, constant, integer, 250; sold, variable, integer, 0; takings, variable, integer, 0. The declarations of a Pascal program are that table written in a form the compiler understands.
Section 2The var section: declaring variables
Variables are declared in a var section, which comes before begin. Each declaration is the name, a colon, the type and a semicolon (Pascal-style): var sold : integer; takings : integer; average : real; grade : char; This reserves a storage area for each name, of the kind the type says. It does not give the area a value yet. Write one name on each line. It is easy to read, easy to change, and every line has the same shape.
Section 3The const section: declaring constants
Constants are declared in a const section. Each constant is a name, an equals sign, the value and a semicolon, with no colon and no type (Pascal-style): const price = 250; rate = 2.5; size = 'L'; The value you write decides the kind of constant: 250 is an integer, 2.5 is a real and 'L' is a char. Watch out: a constant is declared with = and not with :=. The sign := belongs to assignment statements. A constant cannot be given a new value later. A statement such as price := 300; makes the compiler report an error, and the program does not run.
Section 4Giving variables their starting values
Declaring a variable does not give it a value. Until a statement stores one, the value is unknown, so a calculation that uses it can give a wrong answer. Some compilers happen to start a variable at 0, but others do not, so never rely on that. A name must be given a value before it is used. In pseudocode you wrote Store 0 in total. In Pascal-style code the same plan is an assignment at the start of the statements: begin total := 0; count := 0; Here's the trick: after the declarations, the first lines of the statement part should set every variable that is used in a calculation. Free Pascal also lets you write the starting value inside the declaration, as in count : integer = 0; (Pascal-style). Not every Pascal compiler accepts that form, so the assignment at the start of the statements is the safe habit.
Section 5Meaningful names and naming rules
Choose a name that says what the storage holds. sheets, price and totalCost tell the reader what is going on. x, a and t1 tell nobody anything. Meaningful names make a program easier to read, to change and to debug. The Pascal-style rules: a name starts with a letter, then uses letters, digits and underscores. It has no spaces, so pass_mark is fine and pass mark is not. It cannot start with a digit, so 2ndmark is refused. It cannot be a reserved word, a word that already has a job in Pascal, such as begin, end, var or const. Pascal ignores capital letters in names, so Sheets, sheets and SHEETS are the same name. Easy to mix up: a legal name is not the same as a good name. a is a legal name, but sheets is a good one.
Section 6Why keep fixed values in constants?
Suppose a hardware shop in Mandeville uses the price 2400 in several statements. When the price changes to 2600, every copy has to be found and edited, and missing one gives a wrong bill. If the Pascal-style program declares price = 2400; in the const section and every statement uses the name price, the change is one edit, and the program is otherwise untouched. The name also explains the number. A reader understands sheets * price straight away, but sheets * 2400 needs a guess. A value that changes during the run, such as a running total, must be a variable. A constant is only for a value the program never changes.
Section 7Declaration errors the compiler catches
The compiler checks declarations before the program runs. These mistakes stop it from compiling (Pascal-style examples): total = integer; in a var section: the type must follow a colon. price := 250; in a const section: a constant uses an equals sign. price := 300; in the statements, when price is a constant: a constant cannot be changed. Using a name that was never declared, or declared with a slightly different spelling. A missing semicolon at the end of a declaration. Here's the trick: when the compiler reports an error, check the declarations first. A wrong declaration can cause a mistake on a line far below it.
Examples
A tuck shop in Portmore sells patties. This pseudocode plan is to be written in Pascal: Store 250 in price Store 0 in sold Store 0 in takings Store sold + 4 in sold Store takings + 4 * price in takings Display sold Display takings (a) State which names are constants and which are variables, with their types. (b) Write the plan as a Pascal-style program called TuckShop. (c) State what the program displays.
(a) price is a constant, because it is set once and never changes. sold and takings are variables, because they change as sales are made. All three hold whole numbers, so all three are integer. (b) The Pascal-style program is: program TuckShop; const price = 250; var sold : integer; takings : integer; begin sold := 0; takings := 0; sold := sold + 4; takings := takings + 4 * price; writeln(sold); writeln(takings); end. The price goes in the const section with an equals sign. The two variables go in the var section with a colon and a type, and the Store lines become assignments with :=. (c) sold becomes 0 and then 4. takings becomes 0 and then 0 + 4 * 250 = 1000. The program displays 4 on the first line and 1000 on the second line.
A pupil writes this Pascal-style program for a Mandeville roofing shop. It has mistakes in the declarations and in one statement. program Roofing; const price := 2400; fee = 500; var sheets = integer; bill : integer; begin sheets := 6; fee := 600; bill := sheets * price + fee; writeln(bill); end. (a) Find and correct the three mistakes. (b) State what the corrected program displays. (c) The price rises to 2600 dollars. State how many lines must be edited and what the program then displays.
(a) First, price := 2400; uses := in the const section. A constant is declared with an equals sign, so it must be price = 2400; Second, sheets = integer; uses an equals sign in the var section. The type follows a colon, so it must be sheets : integer; Third, fee := 600; tries to change a constant. fee is declared in the const section, so the statement must be removed. If the fee really is 600, the const line is edited instead. The corrected program is: program Roofing; const price = 2400; fee = 500; var sheets : integer; bill : integer; begin sheets := 6; bill := sheets * price + fee; writeln(bill); end. (b) bill is 6 * 2400 + 500 = 14400 + 500 = 14900, so the program displays 14900. (c) Only one line must be edited: price = 2600; in the const section. Then bill is 6 * 2600 + 500 = 15600 + 500 = 16100, so the program displays 16100. Every other line uses the name price, so none of them needs editing.
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 6 · 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 4, Variable declaration (name, colon, type, semicolon; a start value may be given in the declaration), 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 2, Ordinary constants (const, a name, an equals sign, a value and a semicolon; assigning to a constant is a compile error), checked against (https://www.freepascal.org/docs-html/ref/refse9.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, Assignments (the := statement evaluates the right-hand side and stores it in the variable), checked against (https://www.freepascal.org/docs-html/ref/refsu52.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 are made before the statement part and a name is declared before it is used), checked against (https://www.freepascal.org/docs-html/ref/refse115.html) · Facts
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Chapter 3, An Informal Introduction to Python, using Python as a calculator (language-neutral: a name must be given a value before it is used or the program fails), checked against (https://docs.python.org/3/tutorial/introduction.html) · Facts