Information Technology Topic
Assignment, Input and Output in Pascal
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- Input becomes readln, Display becomes writeln and Store becomes := , and the right-hand side of := is always worked out first
- The operator / always gives a real result, so it cannot be stored in an integer variable, and a real is shown neatly with a format such as average:0:2
- Statements run in order, so every variable must be given a value, by an assignment or by readln, before a calculation uses it
Section 1From pseudocode to Pascal statements
A plan in pseudocode uses Input to get a value, Store to work something out and Display to show a result. Pascal has a statement for each. Input a becomes readln(a); Store a + b in sum becomes sum := a + b; and Display sum becomes writeln(sum); Here is a plan that adds two numbers: Input first Input second Store first + second in sum Display sum Each pseudocode line becomes one Pascal-style statement, written in the same order. The variables used must be declared first, in the var section.
Section 2Assignment with :=
An assignment statement stores a value in a variable (Pascal-style): total := 0; count := count + 1; price := 24.5; The variable goes on the left and the value, or an expression that works one out, goes on the right. The right-hand side is worked out first, using the current values of the variables. Then the answer is stored in the variable on the left, and the old value is replaced. So count := count + 1; reads the old count, adds 1, and stores the result back in count. It is an instruction, not an equation. Watch out: the value must fit the type of the variable. An integer can go into a real variable, but a real cannot go into an integer variable, and a char can only go into a char variable. Pascal refuses a mismatch when it compiles.
Section 3Output with write and writeln
writeln displays what is inside the brackets and then starts a new line. write displays it and stays on the same line, so the next output continues straight after it. Text goes in single quotes and values are variables or expressions. Items are separated by commas (Pascal-style): writeln('Total calls: ', total); If total holds 245, this displays Total calls: 245. The text is shown exactly as written, including the space after the colon, and the value of total is shown in its place. Here's the trick: use write for a prompt, a message that tells the user what to type, so the answer is typed on the same line. Use writeln when the line is finished.
Section 4Input with readln
readln(mark) pauses the program until the user types a value and presses Enter. The value is then stored in the variable mark. The type of the variable decides what may be typed: a whole number for an integer, a number for a real and one character for a char. Typing the wrong kind of value stops the program with a run-time error. One readln can fill several variables, as in readln(a, b); the user types the values with a space between them. Here is a short complete program (Pascal-style): program Double; var mark : integer; twice : integer; begin write('Enter a mark: '); readln(mark); twice := mark * 2; writeln('Double is ', twice); end. If the user types 35, the screen shows Enter a mark: 35 and then Double is 70 on the next line. The variable mark must already be declared, and readln gives it its value.
Section 5Arithmetic: + - * and /
Pascal uses + for add, - for subtract, * for multiply and / for divide. The usual order applies: brackets first, then * and /, then + and -, working left to right when operators are equal. So 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. If both values are integers, +, - and * give an integer. If either value is a real, the answer is a real. Easy to mix up: the operator / always gives a real result, even when both values are integers and the answer is exact. 12 / 3 is the real number 4.0, not the integer 4. So the Pascal-style statement average := total / 3; needs average to be a real variable. Storing the result of / in an integer variable is a compile error.
Section 6Showing a real neatly
If you ask writeln to display a real with no format, the answer appears in a long scientific form that is hard to read. Give it a format instead (Pascal-style): writeln(average:0:2); The second number, 2, is the number of decimal places to show. The first number is the minimum width of the output, and 0 means use only as much room as the number needs. The value is rounded, so an average of 81.6666 and so on is shown as 81.67. Only a real can be given decimal places. The same format is used for money amounts, as in writeln('Pay: ', pay:0:2);
Section 7Whole-number division: a short aside
Sometimes you want the whole-number part of a division and the remainder, for example when splitting an amount into notes and change. Pascal has two words for this, which work on integers only (Pascal-style): n := 17 div 5; gives 3, the number of whole fives in 17. r := 17 mod 5; gives 2, what is left over. Dividing by zero is not allowed. The program compiles, but when it runs and reaches a division by zero with /, div or mod, it stops with a run-time error. A compile error is found before the program runs. A run-time error appears only while it is running.
Examples
This Pascal-style program adds two numbers. program SumTwo; var first : integer; second : integer; sum : integer; begin readln(first); readln(second); sum := first + second; writeln('Sum: ', sum); end. (a) State what each readln does. (b) The user types 7 and presses Enter, then types 5 and presses Enter. State the value of each variable after each statement and what is displayed. (c) Write the pseudocode plan that the program follows.
(a) Each readln waits for the user to type a whole number, because the variables are integers. The first readln stores the typed value in first and the second stores its value in second. (b) After the first readln, first is 7. After the second, second is 5. Then sum := first + second works out 7 + 5 = 12 and stores 12 in sum. The program displays Sum: 12. (c) The plan is: Input first Input second Store first + second in sum Display sum Run on the same two numbers, the plan shows 12. The Pascal program also writes the text Sum: before the 12.
A Montego Bay call centre records the calls handled in three shifts. This Pascal-style program works out the total and the average. program ShiftAverage; var morning : integer; afternoon : integer; evening : integer; total : integer; average : real; begin readln(morning); readln(afternoon); readln(evening); total := morning + afternoon + evening; average := total / 3; writeln('Total calls: ', total); writeln('Average per shift: ', average:0:2); end. (a) The user types 70, 85 and 90, each followed by Enter. State what is displayed. (b) Explain why average is declared as real. (c) The user types 80, 80 and 80. State what is displayed on the second line and explain why it is not just 80. (d) Explain what the compiler does if average is declared as integer.
(a) total is 70 + 85 + 90 = 245. average is 245 / 3 = 81.666 and so on, which is shown to 2 decimal places as 81.67. The program displays Total calls: 245 on the first line and Average per shift: 81.67 on the second line. (b) The operator / always gives a real result, and an average often has a fractional part. A real variable can keep it, but an integer variable cannot. (c) total is 240 and 240 / 3 is 80 exactly, but / still gives a real. With the format :0:2 it is shown as Average per shift: 80.00. (d) The compiler reports a type error and the program does not run, because the real result of / cannot be stored in an integer variable.
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 7 · 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 13, Assignments (:= evaluates the expression on the right and stores the result in the variable on the left, replacing the old value), 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 12, Arithmetic operators (+ - * give an integer or a real by operand type, / always gives a real, div and mod take integers, division by zero is a run-time error), checked against (https://www.freepascal.org/docs-html/ref/refsu44.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 12, Expressions and operator precedence, checked against (https://www.freepascal.org/docs-html/ref/refch12.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, System unit, Writeln (writes its arguments to standard output and then starts a new line; Write does not), checked against (https://www.freepascal.org/docs-html/rtl/system/writeln.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, System unit, Write (an output item may be followed by :NumChars and :Decimals, a real is otherwise written in scientific notation, and a width of 0 gives its natural length), checked against (https://www.freepascal.org/docs-html/rtl/system/write.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, System unit, ReadLn (reads values from standard input into variables, which may be Char, Integer or Real, then moves to the next line), checked against (https://www.freepascal.org/docs-html/rtl/system/readln.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 (statements inside begin ... end are separated by semicolons), checked against (https://www.freepascal.org/docs-html/ref/refsu55.html) · Facts