Several alternatives with IF inside ELSE

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

Several alternatives with IF inside ELSE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which structure chooses between three alternatives in Cambridge pseudocode?
A IF ... THEN ... ELSE IF ... THEN ... ELSE ... ENDIF ENDIF
B IF ... THEN ... ELSE ... ELSE ... ENDIF
2. A grade program has IF, then ELSE IF, then ELSE. How many ENDIF statements does it need?
A 2, one for every IF
B 1, because there is only one ELSE chain
3. Mark is 85. The code checks IF Mark >= 70 THEN, then ELSE IF Mark >= 50 THEN. How many branches run?
A Both, because 85 is also at least 50
B Only one: the first branch whose condition is true
4. Which order is correct for checking grade boundaries?
A Highest boundary first: Mark >= 70, then Mark >= 50, then Mark >= 30
B Lowest boundary first: Mark >= 30, then Mark >= 50, then Mark >= 70
5. Mark is 85. The code tests IF Mark >= 30 THEN Grade <- "C" first. What happens?
A Grade is set to "A", because the program finds the best match
B Grade is set to "C", which is wrong, because the first true condition wins

Match the Pairs

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

Left columnRight column
1ELSE IF Mark >= 60 THEN OUTPUT "Merit" ENDIF ENDIF AA second IF placed inside the ELSE branch, so it is only tested when the first condition was false.
2IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ENDIF BIF Mark >= 80 THEN Grade <- "A" ELSE IF Mark >= 50 THEN Grade <- "B" ELSE Grade <- "C" ENDIF ENDIF
3Grade boundaries must be ordered from the highest boundary down, so a mark of 85 is tested against 80 before 50. CThe conditions are in the wrong order. Any mark of 50 or more is caught by the first IF, so the test for 80 can never run and A is never given.
4Every IF needs its own ENDIF, so a structure with two IFs closes with two ENDIF lines. DIF Score >= 40 THEN OUTPUT "Pass" ELSE IF Score >= 20 THEN OUTPUT "Resit" ELSE OUTPUT "Fail" ENDIF ENDIF
5IF Mark >= 50 THEN Grade <- "C" ELSE IF Mark >= 80 THEN Grade <- "A" ENDIF ENDIF EThe nested IF has no ENDIF of its own. Two IFs need two ENDIFs, but only one is written.
6IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF FThree alternatives where only one message is output, and it needs two ENDIFs because there are two IFs.

Definitions

ELSE IF

Fill in the Blanks

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

1.
IF Mark >= 70 THEN OUTPUT "A" ELSE __ Mark >= 50 THEN
2.
OUTPUT "B" _____ ENDIF
3.
IF Mark __ 70 THEN OUTPUT "A" ELSE
4.
ELSE IF Mark >= 50 ____ OUTPUT "B"
5.
IF Mark >= 70 THEN OUTPUT "A" ____

Spot the Error

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

Question 1
A IF Pts >= 80 THEN OUTPUT "A"
B ELSE
C IF Pts >= 60 THEN OUTPUT "B"
D ENDCASE
E ENDIF
Question 2
A IF Pts >= 60 THEN OUTPUT "A"
B ELSE
C IF Pts >= 60 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Question 3
A IF Pct >= 85 THEN OUTPUT "A"
B OTHERWISE
C IF Pct >= 60 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Question 4
A IF Pts >= 75 THEN OUTPUT "A"
B ELSE
C IF Pts >= 50 OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Question 5
A IF Pct => 70 THEN OUTPUT "A"
B ELSE
C IF Pct >= 40 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF

Answer Key

Several alternatives with IF inside ELSE
Section A: Quick Questions
Q1
A — IF ... THEN ... ELSE IF ... THEN ... ELSE ... ENDIF ENDIF
Q2
A — 2, one for every IF
Q3
B — Only one: the first branch whose condition is true
Q4
A — Highest boundary first: Mark >= 70, then Mark >= 50, then Mark >= 30
Q5
B — Grade is set to "C", which is wrong, because the first true condition wins
Section B: Match the Pairs
Q1
ELSE IF Mark >= 60 THEN OUTPUT "Merit" ENDIF ENDIF → A second IF placed inside the ELSE branch, so it is only tested when the first condition was false.
Q2
IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ENDIF → Three alternatives where only one message is output, and it needs two ENDIFs because there are two IFs.
Q3
Grade boundaries must be ordered from the highest boundary down, so a mark of 85 is tested against 80 before 50. → IF Mark >= 80 THEN Grade <- "A" ELSE IF Mark >= 50 THEN Grade <- "B" ELSE Grade <- "C" ENDIF ENDIF
Q4
Every IF needs its own ENDIF, so a structure with two IFs closes with two ENDIF lines. → IF Score >= 40 THEN OUTPUT "Pass" ELSE IF Score >= 20 THEN OUTPUT "Resit" ELSE OUTPUT "Fail" ENDIF ENDIF
Q5
IF Mark >= 50 THEN Grade <- "C" ELSE IF Mark >= 80 THEN Grade <- "A" ENDIF ENDIF → The conditions are in the wrong order. Any mark of 50 or more is caught by the first IF, so the test for 80 can never run and A is never given.
Q6
IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF → The nested IF has no ENDIF of its own. Two IFs need two ENDIFs, but only one is written.
Section C: Definitions
Q1
ELSE IF — A nested IF placed inside ELSE to choose between 3+ alternatives. Conditions are tested in order, only one branch runs, and every IF needs its own ENDIF.
Section D: Fill in the Blanks
Q1
IF Mark >= 70 THEN OUTPUT "A" ELSE IF Mark >= 50 THEN
Q2
OUTPUT "B" ENDIF ENDIF
Q3
IF Mark >= 70 THEN OUTPUT "A" ELSE
Q4
ELSE IF Mark >= 50 THEN OUTPUT "B"
Q5
IF Mark >= 70 THEN OUTPUT "A" ELSE
Section E: Spot the Error
Q1
D — The inner IF must be closed with its own ENDIF. ENDCASE belongs to a CASE statement. Line 4 should be ' ENDIF'.
Q2
A — Conditions are tested in order, so the highest boundary must come first. Testing 60 first gives A to every mark from 60 up, and the B branch can never run. Line 1 should test Pts >= 80.
Q3
B — OTHERWISE is only used inside a CASE statement. To choose between more alternatives, the next IF goes inside an ELSE branch. Line 2 should be 'ELSE'.
Q4
C — Every IF needs the keyword THEN after its condition, including an IF nested inside ELSE. Line 3 should be ' IF Pts >= 50 THEN OUTPUT "B"'.
Q5
A — 'greater than or equal to' is written >= in Cambridge pseudocode. => is not a valid operator. Line 1 should be 'IF Pct >= 70 THEN OUTPUT "A"'.