Information Technology Topic
Selection in Pascal
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- if ... then runs one action only when the condition is true, and if ... then ... else chooses exactly one of two actions
- No semicolon is allowed before else, and begin ... end groups several statements so that they all belong to the then part or the else part
- Conditions use the relational operators = <> < > <= >=, and two tests joined by and or or each need their own brackets
Section 1Selection: choosing between actions
A program often has to choose. A pupil with 50 marks or more passes, otherwise the pupil fails. A bill of 5000 dollars or more gets a discount, otherwise it does not. Choosing between actions is called selection. In pseudocode you wrote this with If-then and If-then-else, and in a flowchart it is the decision diamond with a Yes exit and a No exit. Here is the idea in pseudocode: If mark >= 50 then Display "Pass" Else Display "Fail" End If The line after If is the condition, a test that is either true or false. Pascal has the same structure, written with if, then and else.
Section 2if ... then
The simplest form does something only when the condition is true (Pascal-style): if mark >= 50 then writeln('Pass'); When mark is 62 the condition is true and Pass is displayed. When mark is 40 the condition is false, the statement after then is skipped, and nothing is displayed. The condition goes between if and then. Only the one statement after then belongs to the if, and the program carries on with the next statement either way.
Section 3if ... then ... else
Add else to choose between two actions (Pascal-style): if mark >= 50 then writeln('Pass') else writeln('Fail'); If the condition is true, the then action runs. If it is false, the else action runs. Never both, and never neither. Watch out: there is no semicolon before else. The semicolon goes at the very end of the whole if statement, after the else action. A semicolon in front of else ends the if statement early and the compiler reports an error. Here's the trick: indent the two actions so you can see the two branches. The compiler ignores the indentation, but a reader does not.
Section 4Several statements: begin ... end
Only one statement belongs to then or to else. To run several statements together, group them with begin and end. This is called a compound statement (Pascal-style): if total >= 5000 then begin discount := total * 0.1; writeln('Discount given'); end else begin discount := 0; writeln('No discount'); end; Inside the begin ... end the statements are separated by semicolons as usual. The end before else still has no semicolon after it. Easy to mix up: without begin and end, only the first statement belongs to the if. In if mark >= 50 then bonus := 5; bonus := bonus + 10; the second statement is not part of the if, so it runs every time, whatever the mark is.
Section 5Writing conditions
A Pascal-style condition compares two values with a relational operator: = equal to, <> not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to. Watch out: in a condition, equal to is a single equals sign, as in if grade = 'A' then. The sign := is only for assignment. Writing if mark := 50 then is a compile error. Both sides must be the same kind of value: numbers with numbers, and a char with a char. A char is written in single quotes, as in 'A'. Easy to mix up: < 50 means below 50, so 50 itself is false, while <= 50 means 50 or below, so 50 itself is true. The boundary value is where mistakes hide.
Section 6Joining two tests with and and or
Sometimes one test is not enough. Join two tests with and, when both must be true, or with or, when at least one must be true (Pascal-style): if (age >= 12) and (age <= 17) then writeln('Teen'); if (day = 'S') or (day = 'U') then writeln('Weekend'); Each comparison must be in its own brackets. Pascal works out and and or before it works out the comparisons, so leaving the brackets off, as in age >= 12 and age <= 17, makes the compiler report an error. A test of the form 12 <= age <= 17 is not allowed in Pascal. Write the two comparisons and join them with and.
Section 7Testing a selection
A selection has more than one path, so one run proves little. Choose test data that makes every path run, and check the boundary values. For a pass mark of 50 with >=, test 49 (just below, so Fail), 50 (exactly on the boundary, so Pass) and 51 (just above, so Pass). If the code used > by mistake, the value 50 would give the wrong answer, and only the boundary test would catch it. Work out the expected result of each test by hand first, then run the program and compare. A mismatch means there is a logic error.
Examples
A teacher in Portmore uses this Pascal-style program to give a result. program Result; var mark : integer; begin readln(mark); if mark >= 50 then writeln('Pass') else writeln('Fail'); end. (a) State what is displayed when the user types 62, 50 and 49 in three separate runs. (b) The programmer changes >= to >. State what is displayed when the user types 50, and explain. (c) Explain why there is no semicolon after writeln('Pass').
(a) For 62 the condition 62 >= 50 is true, so the program displays Pass. For 50 the condition 50 >= 50 is true, so it displays Pass. For 49 the condition 49 >= 50 is false, so the else part runs and it displays Fail. (b) With > the condition 50 > 50 is false, so the program displays Fail. A mark of exactly 50 is now failed by mistake. This is why 50 is a boundary value worth testing. (c) A semicolon in front of else would end the if statement early. The else would then be left without an if, and the compiler would report an error. The only semicolon is at the end of the whole statement, after writeln('Fail').
A shop in Mandeville gives 10 percent off a bill of 5000 dollars or more. This Pascal-style program works out what to pay. program Discount; var total : integer; discount : real; pay : real; begin readln(total); if total >= 5000 then begin discount := total * 0.1; writeln('Discount given'); end else begin discount := 0; writeln('No discount'); end; pay := total - discount; writeln('Pay: ', pay:0:2); end. (a) The user types 6000. State the value of discount and what is displayed. (b) The user types 4500. State what is displayed. (c) The user types 5000. State what is displayed and why this test is useful. (d) A pupil adds a semicolon after the first end, the one in front of else. State what happens.
(a) 6000 >= 5000 is true, so discount becomes 6000 * 0.1 = 600. The program displays Discount given, then pay is 6000 - 600 = 5400, so the second line is Pay: 5400.00. (b) 4500 >= 5000 is false, so the else part runs: discount becomes 0 and No discount is displayed. Then pay is 4500 - 0 = 4500, so the second line is Pay: 4500.00. (c) 5000 >= 5000 is true, so the discount is 500 and the program displays Discount given and then Pay: 4500.00. The value 5000 is the boundary, so it checks that exactly 5000 gets the discount. (d) The semicolon ends the if statement, so the word else has nothing to belong to. The compiler reports an error and the program does not run.
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 8 · 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, The if..then..else statement (the then part runs when the condition is true, the else part when it is false; no semicolon before else), checked against (https://www.freepascal.org/docs-html/ref/refsu57.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, into one), 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 12, Relational operators (= <> < > <= >=), checked against (https://www.freepascal.org/docs-html/ref/refsu50.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, Boolean operators (and, or and not join true/false conditions), checked against (https://www.freepascal.org/docs-html/ref/refsu46.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 (relational operators bind loosest, so each comparison needs its own brackets), 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 13, Assignments (:= is assignment, = is a comparison), checked against (https://www.freepascal.org/docs-html/ref/refsu52.html) · Facts