Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A youth club accepts members aged 11 to 16, both included. A programmer wrote:
DECLARE Age : INTEGER
INPUT Age
IF Age > 11 AND Age < 16
THEN
OUTPUT "Accepted"
ELSE
OUTPUT "Rejected"
ENDIFThe tester tries the ages 10, 11, 13, 16 and 17. How many of these five tests give the wrong output? Then correct the condition.
IF <condition> then THEN, an optional ELSE, and ENDIF; THEN and ELSE are two spaces in. CASE OF <identifier> lists single values, each with one statement, tested in order; OTHERWISE comes last; ENDCASE closes it. Relational operators: =, <, <=, >, >=, <>. Logical: AND, OR, NOT.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The right outputs. 11, 13 and 16 should be Accepted. 10 and 17 should be Rejected.
Step 2. What the program does. Age > 11 is FALSE for 11, so 11 is rejected. Age < 16 is FALSE for 16, so 16 is rejected. 13 is accepted. 10 and 17 are rejected, which is right.
Step 3. Count. The ages 11 and 16 give the wrong output: that is 2 tests. Both are boundary values. The normal value 13 hides the bug.
Step 4. The correction.
IF Age >= 11 AND Age <= 16Watch out. > and >= differ only at the boundary. That is why the syllabus asks for boundary data: the largest and smallest accepted values, and the values just outside them.
Log in to join the discussion.
Loading…