07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9. Decision Structures<br />

63<br />

Other Decision Structures<br />

The IF-THEN-ELSE structure gives you two possible branches for your decisions. The<br />

program makes a decision and then carries out one of two sets of statements. You can nest<br />

an IF structure inside another if you wish to make additional decisions, but this can be awkward<br />

if you have several related decisions.<br />

<strong>True</strong> <strong>BASIC</strong> includes two more decision structures that let you choose among three or more<br />

sets of statements. The programs shown below provide a quick introduction; these programs<br />

are in the TBDEMOS Directory.<br />

The ELSE IF statement expands the IF structure to allow for multiple decisions. Consider<br />

the guessing game played in the GUESS program. In that program there are just two things<br />

that might happen after you guess: the program says you are wrong, or it says you are correct<br />

and the game ends. The program GUESS2 can do one of five things based on your guess:<br />

! Program to play a guessing game.<br />

!<br />

RANDOMIZE<br />

LET answer = Int(Rnd*10) + 1 ! From 1 to 10<br />

PRINT “I’m thinking of a number from 1 to 10.”<br />

PRINT “You have 3 chances to guess it.”<br />

PRINT<br />

FOR chance = 1 TO 3<br />

PRINT “Enter your guess”;<br />

INPUT guess! Get a guess<br />

! Ask for number<br />

IF guess < 1 THEN! Check it out<br />

PRINT “Must be at least 1.”<br />

ELSE IF guess > 10 then<br />

PRINT “Can’t be more than 10.”<br />

ELSE IF guess < answer then<br />

PRINT “Too low.”<br />

ELSE IF guess > answer then<br />

PRINT “Too high.”<br />

ELSE! Must be right<br />

PRINT “Correct!!!”<br />

STOP<br />

END IF<br />

NEXT chance<br />

PRINT “The number was”; answer; “.”<br />

END

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

Saved successfully!

Ooh no, something went wrong!