19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Condition 1<br />

True<br />

Statement set 1<br />

If (condition 1) Then<br />

Statement set 1<br />

False<br />

Condition 2<br />

True<br />

Statement set 2<br />

ElseIf (condition 2) Then<br />

Statement set 2<br />

False<br />

Condition x<br />

True<br />

Statement set x<br />

--snip--<br />

ElseIf (condition x) Then<br />

Statement set x<br />

False<br />

Default statements<br />

Else<br />

Default statements<br />

EndIf<br />

Figure 9-1: The structure of the If/ElseIf ladder<br />

Let’s look at another way <strong>to</strong> use the If/ElseIf ladder.<br />

Letter Grades<br />

In this example, you’ll create a program that reads a test score between 0<br />

and 100, and displays a letter grade from Table 9-1.<br />

Table 9-1: Letter-Grade Scores<br />

Score<br />

score ≥ 90<br />

A<br />

80 ≤ score < 90 B<br />

70 ≤ score < 80 C<br />

60 ≤ score < 70 D<br />

score < 60<br />

F<br />

Letter grade<br />

The complete program is shown in Listing 9-2.<br />

1 ' GradeLetter.sb<br />

2 TextWindow.Write("Enter the score: ")<br />

3 score = TextWindow.ReadNumber()<br />

4 If (score >= 90) Then<br />

5 grade = "A"<br />

6 ElseIf (score >= 80) Then<br />

7 grade = "B"<br />

8 ElseIf (score >= 70) Then<br />

9 grade = "C"<br />

10 ElseIf (score >= 60) Then<br />

11 grade = "D"<br />

Using Decisions <strong>to</strong> Make Games 113

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!