Back

Activity 1.10: More Than Two Choices

divider

Activity 1.10

More Than Two Choices

Key Concepts

A Chain of Questions

First Match Wins

The Exam Does It Differently

What You Are Building

A blue ball in the left third of the window, labeled Left third
A yellow ball in the middle third, labeled Middle third
A green ball in the right third, labeled Right third

Three zones, three answers, and only one of them ever true at a time.

Two Choices Was Not Enough

if x < 267 then
love.graphics.setColor(0.3, 0.5, 1)
else
love.graphics.setColor(0.2, 0.8, 0.3)
end

Two Choices Was Not Enough

if x < 267 then
love.graphics.setColor(0.3, 0.5, 1)
else
love.graphics.setColor(0.2, 0.8, 0.3)
end

if / else splits the world in two. To split it in three you need somewhere to put the middle.

elseif Adds a Question

if x < 267 then
love.graphics.setColor(0.3, 0.5, 1)
elseif x < 533 then
love.graphics.setColor(0.9, 0.8, 0.2)
else
love.graphics.setColor(0.2, 0.8, 0.3)
end

One if, as many elseifs as you want, and at most one else at the bottom.

How the Chain Runs

  • Check the first condition. If true, run that block and stop.
  • Otherwise check the next one. Same deal.
  • If none matched, run the else.

Exactly one block runs. Never two, never zero if there is an else.

Which Is Why Order Matters

if x < 533 then
label = "Middle third"
elseif x < 267 then
label = "Left third"
else
label = "Right third"
end

Which Is Why Order Matters

if x < 533 then
label = "Middle third"
elseif x < 267 then
label = "Left third"
else
label = "Right third"
end

"Left third" can never happen. Anything under 267 is also under 533, and the first question already caught it.

No error. A branch that simply never runs.

Separate ifs Are Not the Same Thing

if x < 267 then
label = "Left third"
end
if x < 533 then
label = "Middle third"
end

Separate ifs Are Not the Same Thing

if x < 267 then
label = "Left third"
end
if x < 533 then
label = "Middle third"
end

Both of these run. A ball at x = 100 gets labeled "Left third" and then immediately relabeled "Middle third".

elseif is not a tidier way to write two ifs. It means only if the one above did not match.

Today's Objectives

  • Write an if / elseif / else chain
  • Say why only one branch runs
  • Order conditions so every branch is reachable
  • Read the same idea written in exam notation

Key Terms

elseif
Another condition, checked only if the ones above it were false.
Chain
An if with one or more elseifs after it.
Unreachable
A branch whose condition can never be the first one true.
Nesting
Putting one decision inside another decision's block.

'F' → Fullscreen

divider

Build

Task 1: Draw the Zones

Copy 1-09-bounce to 1-10-zones. Add two faint lines at the top of love.draw so you can see where the thirds are:

main.lua
love.graphics.setColor(0.25, 0.25, 0.25)
love.graphics.line(267, 0, 267, 600)
love.graphics.line(533, 0, 533, 600)

love.graphics.line takes two points: the x and y it starts at, then the x and y it ends at. These two run from the top of the window to the bottom.

The ball will now be gray, because the color you set for the lines is still set when the circle is drawn. That is session 2's sticky color, and the next task fixes it.

Task 2: Three Colors

Replace the if / else in love.draw with a chain, and add a label:

main.lua
function love.draw()
love.graphics.setColor(0.25, 0.25, 0.25)
love.graphics.line(267, 0, 267, 600)
love.graphics.line(533, 0, 533, 600)
local label = ""
if x < 267 then
love.graphics.setColor(0.3, 0.5, 1)
label = "Left third"
elseif x < 533 then
love.graphics.setColor(0.9, 0.8, 0.2)
label = "Middle third"
else
love.graphics.setColor(0.2, 0.8, 0.3)
label = "Right third"
end
love.graphics.circle("fill", x, y, radius)
love.graphics.setFont(font)
love.graphics.print(label, 40, 500)
end

Add local font = love.graphics.newFont(24) at the top with the other variables. Run it and watch the ball change color and label as it crosses each line.

local label is inside the box on purpose. In session 7 that was a bug, because the value had to survive to the next frame. This one does not — it is built, used, and thrown away within a single picture, so inside is exactly right.

Task 3: Break the Order

Swap the first two conditions so the chain reads:

main.lua
if x < 533 then
label = "Middle third"
elseif x < 267 then
label = "Left third"
else
label = "Right third"
end

Run it and watch the ball cross the left third without ever saying "Left third". Write down what it says there instead, and explain why.

No error, no warning. A branch that can never run looks exactly like a branch that has not run yet.

Task 4: Four Zones

Put the order back, then add a fourth zone. Split the window into quarters instead of thirds: 200, 400 and 600. Four colors, four labels, three lines drawn.

Write the conditions on your sheet before you type them, and check that every one of the four can actually happen.

Challenge (Optional): Decide on Speed Instead

Add a second chain that labels how fast the ball is going — slow, medium or fast — based on speedX. Then change speedX in the code and check all three labels can appear.

Careful: a ball moving left has a negative speed. Decide whether −300 counts as fast, and make your conditions say what you decided.

Challenge (Optional): Vertical Zones Too

Give the ball a speedY if it does not have one, and add a second label for which third of the window it is in vertically. Two chains, two labels, nine possible combinations.

divider

Same Idea, Exam Notation

There is no ELSE IF on the exam reference sheet. The only two selection forms are IF and IF / ELSE. This is the first place Lua has something the exam does not.

So a three-way choice is written by putting a whole second decision inside the first one's ELSE:

Lua
if score > 100 then
print("Gold")
elseif score > 50 then
print("Silver")
else
print("Bronze")
end
Exam Notation
IF(score > 100)
{
DISPLAY("Gold")
}
ELSE
{
IF(score > 50)
{
DISPLAY("Silver")
}
ELSE
{
DISPLAY("Bronze")
}
}

They do exactly the same thing

Read the right hand column carefully. Silver is only considered if Gold was false, because the second IF lives inside the first ELSE. That is precisely what elseif means.

scoreLua chainExam nesting
120GoldGold
75SilverSilver
20BronzeBronze

The indentation is the whole story. Each nested decision sits one level further in, so a four-way choice on the exam is indented three times. Follow the braces when you read one — they always tell you which ELSE you are in.

You will not be asked to write this by hand often. You will be asked to read it in May and say which branch runs.

divider

Checkpoint

  • 1-10-zones shows four zones with four colors and four labels
  • Every zone can actually be reached as the ball crosses
  • The zone conditions were written on the sheet before being typed
  • You can say what happened when the first two conditions were swapped
divider

Reflection

Answer the following questions before submitting your work.

  1. Swapping two conditions made one branch impossible, with no error of any kind. Explain how a chain decides which block to run, and why that makes order matter.
  2. Two separate if statements and an if / elseif pair look almost identical. Describe a case where they give different results, using your own zone conditions.
  3. The exam has no ELSE IF and writes a three-way choice by nesting instead. Say whether the two are really the same, and give one reason someone might prefer each.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete