Using a BOOLEAN function in a condition

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Using a BOOLEAN function in a condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which IF statement correctly uses a BOOLEAN function IsEven to test a number?
A IF IsEven THEN Number
B IF IsEven(Number) THEN
2. What does the call IsEven(Number) give when it is used in an IF condition?
A TRUE or FALSE
B The text "even" or "odd"
3. Which function header is correct for a function that checks whether someone is an adult?
A FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
B FUNCTION IsAdult(Age : INTEGER) RETURNS INTEGER
4. Inside IsAdult, which line correctly returns TRUE when Age is 18 or more?
A RETURN Age = 18 THEN TRUE
B RETURN Age >= 18
5. Which condition is a tidier way to test the result of IsAdult(Age)?
A IF IsAdult(Age) THEN
B IF IsAdult(Age) = TRUE THEN

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF IsEven(Number) THEN AIF IsAdult(Age) THEN
2WHILE NOT IsPrime(Number) DO BComparing the BOOLEAN function call with = TRUE is not needed. The call already gives TRUE or FALSE, so it should be IF IsEven(Number) THEN.
3A function that RETURNS a BOOLEAN can use a comparison as its return value, because a comparison gives TRUE or FALSE. CRETURN Age >= 18
4A BOOLEAN function can be called straight in an IF condition without comparing it to TRUE. DThe function call is missing its brackets and argument. It should be IF IsAdult(Age) THEN so the function receives a value.
5IF IsEven(Number) = TRUE THEN ECalls the BOOLEAN function IsEven straight in the IF condition. The call gives TRUE or FALSE, so the THEN part runs only when it is TRUE.
6IF IsAdult THEN FThe loop keeps repeating while the BOOLEAN function IsPrime returns FALSE for Number, because NOT turns FALSE into TRUE.

Definitions

IF IsEven(Number) THEN

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF ______(Number) THEN OUTPUT "Even" ENDIF
2.
FUNCTION IsAdult(A : INTEGER) RETURNS _______ RETURN A >= 18 ENDFUNCTION
3.
FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N _ 100 ENDFUNCTION
4.
WHILE ___ IsValid(Pin) DO INPUT Pin ENDWHILE
5.
IF IsAdult(Age) ____ OUTPUT "Welcome" ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A INPUT Number
B IF IsEven(Number)
C OUTPUT "Even"
D ENDIF
Question 2
A IF IsAdult THEN
B OUTPUT "Entry allowed"
C ENDIF
Question 3
A FUNCTION Full RETURNS BOOLEAN
B RETURNS Count >= 10
C ENDFUNCTION
Question 4
A OUTPUT "Enter an even number"
B INPUT Number
C WHILE IsEven(Number) DO
D INPUT Number
E ENDWHILE
Question 5
A IF IsPass(Mark) = "TRUE" THEN
B OUTPUT "Well done"
C ELSE
D OUTPUT "Try again"
E ENDIF

Answer Key

Using a BOOLEAN function in a condition
Section A: Quick Questions
Q1
B — IF IsEven(Number) THEN
Q2
A — TRUE or FALSE
Q3
A — FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
Q4
B — RETURN Age >= 18
Q5
A — IF IsAdult(Age) THEN
Section B: Match the Pairs
Q1
IF IsEven(Number) THEN → Calls the BOOLEAN function IsEven straight in the IF condition. The call gives TRUE or FALSE, so the THEN part runs only when it is TRUE.
Q2
WHILE NOT IsPrime(Number) DO → The loop keeps repeating while the BOOLEAN function IsPrime returns FALSE for Number, because NOT turns FALSE into TRUE.
Q3
A function that RETURNS a BOOLEAN can use a comparison as its return value, because a comparison gives TRUE or FALSE. → RETURN Age >= 18
Q4
A BOOLEAN function can be called straight in an IF condition without comparing it to TRUE. → IF IsAdult(Age) THEN
Q5
IF IsEven(Number) = TRUE THEN → Comparing the BOOLEAN function call with = TRUE is not needed. The call already gives TRUE or FALSE, so it should be IF IsEven(Number) THEN.
Q6
IF IsAdult THEN → The function call is missing its brackets and argument. It should be IF IsAdult(Age) THEN so the function receives a value.
Section C: Definitions
Q1
IF IsEven(Number) THEN — A function that RETURNS a BOOLEAN can be called straight in an IF or WHILE condition. The call gives TRUE or FALSE, so no comparison is needed.
Section D: Fill in the Blanks
Q1
IF IsEven(Number) THEN OUTPUT "Even" ENDIF
Q2
FUNCTION IsAdult(A : INTEGER) RETURNS BOOLEAN RETURN A >= 18 ENDFUNCTION
Q3
FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N > 100 ENDFUNCTION
Q4
WHILE NOT IsValid(Pin) DO INPUT Pin ENDWHILE
Q5
IF IsAdult(Age) THEN OUTPUT "Welcome" ENDIF
Section E: Spot the Error
Q1
B — An IF statement must end its condition with THEN. The line should be IF IsEven(Number) THEN.
Q2
A — The function needs its argument in brackets. Without (Age) it has no value to test. It should be IF IsAdult(Age) THEN.
Q3
B — The keyword inside a function is RETURN, not RETURNS. RETURNS is only used in the function header. The line should be RETURN Count >= 10.
Q4
C — The loop must keep asking while the number is NOT even. Without NOT it repeats for even numbers and stops on an odd one. It should be WHILE NOT IsEven(Number) DO.
Q5
A — IsPass already gives the BOOLEAN value TRUE or FALSE. Comparing it with the string "TRUE" is wrong. It should be IF IsPass(Mark) THEN.