Task 6  Task 6: Conditions and Branching  Task 6


Going back to our first program, wouldn't it be cool that instead of the general Hello World, we could say Good Morning World!, Good Afternoon World!, or Good Evening World! depending on the time of day? For our next program we'll make the computer say Good Morning World! if the time is earlier than 12PM; Good Afternoon World! if the time is between 12PM and 6PM and Good Evening World! if the time is greater than 6PM. Type the following program into your IDE then execute it to see it in action.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Hour% ' the current hour of the day

'----------------------------
'- Main Program Begins Here -
'----------------------------

Hour% = VAL(TIME$) '                get the current hour (military time)
IF Hour% < 12 THEN '                is current hour less than 12?
    PRINT "Good Morning World!" '   yes, it must be morning
END IF
IF Hour% > 11 AND Hour% < 18 THEN ' is current hour 12 to 17? (noon to 5PM)
    PRINT "Good Afternoon World!" ' yes, it must be afternoon
END IF
IF Hour% > 17 THEN '                is current hour greater than 17? (5PM)
    PRINT "Good Evening World!" '   yes, it must be evening
END IF


Before we analyze the program you probably noticed the use of the < and > characters. Just as you learned in math class, the < character means less than and the > character means greater than. These are known as relational operators because they test the relationship between two or more items.

Let's analyze the first line of code in the main program section:


Hour% = VAL(TIME$) '                get the current hour (military time)

We declared an integer variable called Hour% to hold the current hour of the day. TIME$ is a QB64 command that can either set the computer's time or retrieve the current time from the computer. If you haven't already guessed, the current time is returned as a string because of the $ contained in the command. TIME$ returns the current computer time in the format HH:MM:SS as 24 hour military time. So, for instance, 6PM is returned as 18:00:00.

VAL() is a QB64 function. Think of a function as a premade set of code that performs a task for you. In the case of VAL() it returns the value of a string, or in other words converts any number characters contained in a string to an actual number and returns that number. VAL() will stop converting numbers when it reaches a non-number character. So, since the first two characters of TIME$ are the hour, VAL() will grab these first two characters, convert them to numbers, and then return those numbers to the program as an actual numeric value. In our program that number is placed in Hour% becuase we told it to equal ( = ) the result of what VAL() returns. In reality what we have done is use a command, within a function, to return a desired value we need. This nesting of commands is a very common thing to do in all programming languages. Let's use the example of 6PM again and see how this is broken down:

Time$ returns the time as a string of characters. Since Time$ is contained within parenthesis it is handled first, following the rule of order of operations.

Hour% = VAL("18:00:00")

The first two characters are grabbed because they are numeric characters. The colon ( : ) is a non-numeric character so VAL() stops there.

Hour% = VAL("18")

Now that VAL() contains only numeric characters it converts those characters to an actual number and returns that number.

Hour% = 18

And there you have it, we grabbed only the portion of TIME$ we need to continue on with the rest of the program. Now, let's analyze the next three lines of code, which is actually one statement:

IF Hour% < 12 THEN '                is current hour less than 12?
    PRINT "Good Morning World!" '   yes, it must be morning
END IF


The IF...THEN statement is used to do Boolean true or false conditional testing. The code contained in between the IF and THEN keywords is known as a condition. This condition is evaluated to either a true or false outcome. If the condition is true then anything that follows THEN is executed, however, if the condition is found to be false then anything following THEN is ignored. We previously used the example of 6PM and from this set Hour% to 18. Let's plug this into these three lines of code to see what happens:

The number 18 is substituted for Hour%:

IF 18 < 12 THEN '                   is current hour less than 12?
    PRINT "Good Morning World!" '   yes, it must be morning
END IF


Then the condition is examined. Is 18 less than 12? No, of course not, so this condition is false:

IF FALSE THEN '                     is current hour less than 12?
    PRINT "Good Morning World!" '   yes, it must be morning
END IF


Since our condition is false anything following THEN will be ignored. Since 18 in military time is 6PM this can't possibly be morning, so our IF...THEN statement worked perfectly to prevent the program from stating that it is morning. Let's move onto the next three lines of code in the same manner:

The number 18 is once again substituted for each Hour%:

IF 18 > 11 AND 18 < 18 THEN '       is current hour 12 to 17? (noon to 5PM)
    PRINT "Good Afternoon World!" ' yes, it must be afternoon
END IF


This condition is now examined, but there is actually two conditions here being checked between an AND Boolean conditional operator. In this case both conditions have to be true for the code after THEN to be executed. In other words, this AND that must be true for the entire statement to be true. (More discussion will be done on Boolean conditional operators in a later task.)

Is 18 greater than 11? No, so the first condition is false. Is 18 less than 18? One again, this is not true so the second condition is false. If either condition on either side of the AND Boolean conditional operator is false, the entire statement becomes false. Once again, since 18 in military time is 6PM this can't possibly be afternoon, therefore this IF...THEN statement worked correctly.

IF FALSE AND FALSE THEN '           is current hour 12 to 17? (noon to 5PM)
    PRINT "Good Afternoon World!" ' yes, it must be afternoon
END IF


Finally, let's examine the last three lines of code. The number 18 is once again substituted for Hour%:


IF 18 > 17 THEN '                   is current hour greater than 17? (5PM)
    PRINT "Good Evening World!" '   yes, it must be evening
END IF


This condition is now examined. Is 18 greater than 17? Why yes, yes it is! So this condition becomes true.

IF TRUE THEN '                      is current hour greater than 17? (5PM)
    PRINT "Good Evening World!" '   yes, it must be evening
END IF


Since the condition evaluated to true the statement is allowed to execute the code after THEN, resulting in "Good Evening World!" printed to the screen. 6PM, 18 in military time, is in fact evening, so this IF...THEN statement worked as it should. Save this program as HELLO3.bas before continuing on.

-- What IF? --

The IF...THEN statement can be used in a number of different ways to perform conditional testing. To see all the different methods of testing that IF...THEN offers let's start out with a simple program that tests a single condition. Type the following code into your IDE and then execute it to see it in action.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Value1% ' stores user input

'----------------------------
'- Main Program Begins Here -
'----------------------------

INPUT "Enter a number between 1 and 100 >", Value1%
IF Value1% < 1 THEN PRINT "That number is less than 1 !"
IF Value1% > 100 THEN PRINT "That number is greater than 100 !"
IF Value1% > 0 AND Value1% < 101 THEN PRINT "Thank you for following directions."
END IF

Here we ask the user for a number between 1 and 100 and store that number into the interger variable Value1%. In previous example programs using the IF...THEN statement an END IF was associated on a separate line to indicate where the IF...THEN statement ends, but this is not needed for simple conditional checks as seen in the example code above. If only one action needs to be performed for a given condition the condition and action can be put on the same line:

IF Value1% < 1 THEN PRINT "That number is less than 1 !"

The condition of Value% < 1 is checked and if true the single action PRINT is acted upon. All three IF...THEN lines in the example show this same behavior, but it is possible to group these three statements together using a modified version of IF...THEN. Modify your code to reflect the changes below:

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Value1% ' stores user input

'----------------------------
'- Main Program Begins Here -
'----------------------------

INPUT "Enter a number between 1 and 100 >", Value1%
IF Value1% < 1 THEN
    PRINT "That number is less than 1 !"
ELSEIF Value1% > 100 THEN
    PRINT "That number is greater than 100 !"
ELSE
    PRINT "Thank you for following directions."
END IF

ELSEIF and ELSE are two optional pieces you can use with IF...THEN statements.  ELSEIF allows an statement to test for more conditions if a previous condition failed. IF...THENELSE can be considered a "catch all", that is if all IF and ELSEIF conditions are false, the action contained after ELSE is acted upon. The flow looks like this:

The user enters the number 123 which is subsititued for Value% in the IF line:


IF 123 < 1 THEN

However this condition is false as 123 is obviously not less than 1. Since this condition is false QB64 skips to the next ELSEIF line if present and once again 123 is substituted for Value%:

ELSEIF 123 > 100 THEN

This condition becomes true because 123 is greater than 100. Since this condition is true, the action associated with this ELSEIF is acted upon resulting in "That number is greater than 100 !" being printed to the screen. The rest of the IF...THEN statement is ignored because ELSE's action only activates when all other conditions were found to be false. Lets look at this scenario again with a different number:

The user enters the number 55 which is substituted for Value% in the IF line:


IF 55 < 1 THEN

This is false so QB64 skips to the next ELSEIF and tests its condition, once again substituting 55 for Value%:

ELSEIF 55 > 100 THEN

Again, another false statement. Since there are no more ELSEIFs to go to QB64 looks for an ELSE, and if present acts upon its action:

"Thank you for following directions."

Actions may contain many statements when IF...THEN is used with an END IF statement in a block format. Again, makes modifications to your code as seen below to see this behavior in action.


'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Value1% ' stores user input

'----------------------------
'- Main Program Begins Here -
'----------------------------

INPUT "Enter a number between 1 and 100 >", Value1%
IF Value1% < 1 THEN
    PRINT "That number is less than 1 !"
    END
END IF
IF Value1% > 100 THEN
    PRINT "That number is greater than 100 !"
    END
END IF
PRINT "Thank you for following directions."

Here we are using two IF...THEN statements to test for the number entered being too high or too low. Let's take a close look at the first IF...THEN statetment:

IF Value% < 1 THEN
    PRINT "That number is less than 1 !"
    END
END IF


As you can see in the above code snippet you can have multiple commands contained between the IF and END IF in the action area. If this particular condition is true, the PRINT statement prints a message to the screen and then the END command ends the program. You can place as many commands as you wish between a block IF and END IF.

If neither one of the IF...THEN statements is true then logic dictates the user must have entered a number between 1 and 100 and the user is thanked for following the instructions. Go ahead and save this example code as 1_TO_100.bas now before continuing on.


-- Just in CASE --

Another form of conditional testing can be performed with the SELECT CASE...END SELECT statement that provides many powerful options available for testing conditions. Let's take the the first program from this task and rewrite it using SELECT CASE.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Hour% ' the current hour of the day

'----------------------------
'- Main Program Begins Here -
'----------------------------

Hour% = VAL(TIME$) '                    get the current hour (military time)
SELECT CASE Hour% '                     what is current value?
    CASE IS < 12 '                      is it less than 12?
        PRINT "Good Morning World!" '   yes, it must be morning
    CASE 12 TO 17 '                     is it 12 to 17?
        PRINT "Good Afternoon World!" ' yes, it must be afternoon
    CASE IS > 17 '                      is it greater than 17?
        PRINT "Good Evening World!" '   yes, it must be evening
END SELECT

In between the SELECT CASE and END SELECT statements is where you place CASE conditional checks, but first you need a value for the CASE conditional checks to evaluate.

SELECT CASE Hour% '                     what is current value?

The line of code above instructs the SELECT CASE construct to use the variable Hour% as the value to evaluate. All CASE conditional checks inside the construct will use the value contained in Hour% as the condition to check against.

    CASE IS < 12 '                      is it less than 12?
        PRINT "Good Morning World!" '   yes, it must be morning
    CASE 12 TO 17 '                     is it 12 to 17?
        PRINT "Good Afternoon World!" ' yes, it must be afternoon
    CASE IS > 17 '                      is it greater than 17?
        PRINT "Good Evening World!" '   yes, it must be evening


A block of code is created between each CASE statement to carry out additional statements if the current CASE statement is true. Let's assume the time is once again 6PM and go through each CASE statement one at a time. Once again, the VAL() function returned a value of 18 from TIME$ and placed it in Hour% meaning that it is currently 6PM due to the value of 18 in military time equalling 6PM.

    CASE IS < 12 '                      is it less than 12?
        PRINT "Good Morning World!" '   yes, it must be morning


This CASE statement is checking to see if the value of Hour% is less than 12. Since the value is 18 this CASE statement evaluates to false and the code contained after it will not be executed. The SELECT CASE construct moves onto the next CASE statement.

    CASE 12 TO 17 '                     is it 12 to 17?
        PRINT "Good Afternoon World!" ' yes, it must be afternoon


Here the CASE statement is checking for a range of numbers, 12 TO 17, to test against Hour%. Once again, since the actual value of Hour% is 18 this CASE statement evaluates to false and the code contained after it will not be executed. The SELECT CASE construct moves onto the next CASE statement.

    CASE IS > 17 '                      is it greater than 17?
        PRINT "Good Evening World!" '   yes, it must be evening


The final CASE statement however evaluates to true since the value contained in Hour% is greater than 17. This time the code contained in this CASE's block of code is executed informing the user that evening has arrived.  Let's look at another scenerio where SELECT CASE can check for multiple conditions.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Number% ' user provided numeric value

'----------------------------
'- Main Program Begins Here -
'----------------------------

PRINT '                                            blank line
INPUT " Enter a number from 1 to 10 > ", Number% ' get value from user
SELECT CASE Number% '                              what value did user provide?
    CASE IS < 1, IS > 10 '                         is it within range?
        PRINT '                                    no
        PRINT " Follow directions much?" '         user did not follow instructions
    CASE 2, 4, 6, 8, 10 '                          is it any of these values?
        PRINT '                                    yes
        PRINT " You entered an EVEN number." '     then it must be even number
    CASE ELSE '                                    it was none of the above conditions
        PRINT
        PRINT " You entered an ODD number." '      then it must have been odd
END SELECT

The program listing above checks for a user provided value from 1 to 10 and then informs the user if the followed the directions or not. If the user did it then checks the value for EVEN or ODD characteristics. Once again, SELECT CASE is used to identify the value to be checked.

SELECT CASE Number% '                              what value did user provide?

This SELECT CASE construct will use the value contained in Number% to test the various conditions needed.

    CASE IS < 1, IS > 10 '                         is it within range?

CASE statements allow multiple checks on the same line by separating them with the use of a comma ( , ).  This CASE statement is testing the for the value to be either less than 1 or greater than 10 through the use of the IS statement. If either one of the conditions is true the code block associated with this CASE statement is executed. Once the SELECT CASE construct has identified a CASE statement that evaluates to true, the associated code block is executed and the SELECT CASE construct is exited. For example, if the user were to enter the value of 11, this CASE statement would evaluate to true, the code block associated with it would execute and the rest of the CASE statements would be skipped over since this was the first true CASE statement to be encountered. As soon as SELECT CASE indentifies a true CASE evaluation only that CASE statement's code is executed and the SELECT CASE construct is exited.

    CASE 2, 4, 6, 8, 10 '                          is it any of these values?

If the user followed the instructions and entered a value from 1 to 10 the next CASE ststement is then evaluated. Here, we are testing for five conditions on the same line separated by commas 2, 4, 6, 8, 10. If the value in Number% is any one of these values the CASE statement evaluates to true, executing the code block associated with it. In this situation the user is informed that he or she entered an EVEN number. However, if an ODD number was entered all of these conditions would evaluate to false and the SELECT CASE construct would move on to the next CASE statement.

    CASE ELSE '                                    it was none of the above conditions

If none of the previous CASE statements evaluated to true you can instruct the SELECT CASE construct to handle any other condition by using the CASE ELSE statement. With this program if the first two CASE statements evaluated to false then it can be assumed logically that the user must have entered an ODD number in the range of 1 to 10.

The following forms of CASE can be used with the SELECT CASE contstruct.

CASE value

Here a single value is checked such as a numeric or string value. Examples would be CASE 10, CASE Number%, CASE "Mississippi" or CASE Pname$.

CASE value1 TO value2

A range of values can be checked through the use of CASE statements, such as CASE 1 TO 10, CASE -50 TO 100 or CASE n1% TO n2%.

CASE value1, value2, value3

Multiple single values can be checked at the same time, such as CASE 1, 3, 5 or CASE "A", "B", "C".

CASE IS > value

Values can be checked against conditions which include less than ( < ), greater than ( > ), equal to ( = ), less than or equal to ( <= ), greater than or equal to ( >= ) and not equal to ( <> ).

CASE ELSE

Used as a "catch-all" if all of the other CASE evaluations preceding the CASE ELSE statement evaluated to false.

By using commas ( , ) you can mix and match these various forms of CASE one on line. Here are a few examples:


CASE 1, 5 TO 10, 15 '          check for 1, 5, 6, 7, 8, 9, 10 and 15
CASE IS < 1, IS > 10 ,         check for a value not in the range of 1 to 10
CASE -10 TO -1, 1 TO 10 '      check for values in these ranges
CASE "A", "B", "C", "D", "F" ' check for these string values (grades perhaps)

SELECT CASE can be used as an alternative to multiple ELSEIF statements because it makes the code cleaner and easier to read. The major difference between multiple ELSEIF statements and SELECT CASE is that SELECT CASE can only test for one condition while multiple ELSEIFs can check many different conditions. Take this code for example:


IF Count% = 1 THEN
    ' do this code
ELSEIF Count% = 2 THEN
    ' do this code
ELSEIF Count% > 10 THEN
    ' do this code
ELSE
    ' do this code
END IF


The code above lends itself well to being converted to a SELECT CASE statement because only one variable, in this Count%, is being checked for different values.

SELECT CASE Count%
    CASE 1
        ' do this code
    CASE 2
        ' do this code
    CASE IS > 10
        ' do this code
    CASE ELSE
        ' do this code
END SELECT


However, the following IF...ENDIF would not be possible to do using a SELECT CASE statement.

IF Count% = 1 THEN
    ' do this code
ELSEIF Player% = 2 THEN
    ' do this code
ELSEIF Score% < 100 THEN
    ' do this code
ELSE
    ' do this code
END IF


The reason is that three different variables are being evaluated in the above code and there is no mechanism to allow SELECT CASE to test for more than one condition at a time. Again, the main reason to use SELECT CASE is the versatility it offers when checking a condition and the readability it offers to the programmer. Many times you'll see programmers use SELECT CASE in the following manner.

Frame% = Frame% + 1
SELECT CASE Frame% '        which frame is game on?
    CASE 1: UPDATESCORES '  update score display on first frame
    CASE 2: ADDENEMY '      possibly add an enemy to playing field during this frame
    CASE 3: TOGGLEENEMIES ' change enemy appearance if needed on this frame
    CASE 30: Frame% = 0 '   reset frame counter after 30 frames
END SELECT


Notice the use of a colon ( : ) after each CASE statement. Colons are a throwback from the early days of BASIC and allow multiple statements to be placed on the same line of code. QB64 still supports the use of colons to separate statements but because they make code very difficult to read they are rarely ever used. However, sometimes with CASE statements they actually enhance the readability as seen above.

-- Dented Code --

You may have noticed by now that the IDE is indenting, or moving, some of the lines of code to the right automatically by four spaces. This is a habit you MUST get into! In the example code above indenting the lines of code inside the IF and END IF clearly shows the programmer where related blocks of code are located. The QB64 IDE automatically indents code for the programmer, which is a nice feature, but some IDEs do not. QB64 source code can be written in Notepad if you wish, but of course Notepad won't indent automatically as it will be up to the programmer to do this manually.

Tip: There is, however, one thing about the IDE I do not like when it comes to indenting code such as IF...THEN statements. Type the following lines of code into your IDE and make sure you press the ENTER key after the last line you type (you will get an error, and that is the point):


'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Value1% '   generic value
DIM Value2% '   generic value
DIM ValueSum% ' sum of Value1% and Value2%

'----------------------------
'- Main Program Begins Here -
'----------------------------

Value1% = 1
Value2% = 2
ValueSum% = Value1% + Value2%
IF ValueSum% = 5 THEN

Oops!
Figure 1 - The IDE reports errors while typing.

Remember how I told you before that the IDE is always watching what you type? Well, in this case it can be rather annoying (but it's still necessary). When you create a block of code, such as an IF...THEN...END IF, the IDE will continually complain that END IF is missing while you are typing the code in that's contained in the block. I've gotten around this by getting into the habit of pressing ENTER about 3 or 4 times after I type an IF...THEN statement. Then I type in the END IF, leaving a blank block area that I can simply arrow back up to to write my code. See Figure 2 below:

That's better
Figure 2 - That's better, not seeing red any longer

Some programmers like the constant reminder from the IDE while others, like my self, do not. As a new programmer it's completely up to you how you develop your coding style, but I'll try to include these tips along the way for those who may wish to try them.

-- Branching --

The next portion of this task is a controversial subject in the programming world. I am going to introduce you to the GOTO command which allows program execution to jump from one place to another within the source code. Most programmers today will tell you that the GOTO command, included in many languages by the way, is poor programming style and should be avoided at all costs. Others will tell you that using it in BASIC keeps with the spirit of the language allowing the programmer an easy way to jump around within the source code. My personal opinion is that the GOTO command should be avoided because it can make following a program's flow very hard to comprehend. But, I would be lacking if I did not show you this command that has been a staple of BASIC programmers since the beginning of the language.

So far all of the example programs you have worked with have started at the top of the source and worked toward the bottom one line at a time. The GOTO command will you to jump back, or forward, in the source code allowing source code to be reused. Let's write a small example program showing this process in action. Type the following program into your IDE then execute it to see the program in action.


'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Count% '                     a generic counter

'----------------------------
'- Main Program Begins Here -
'----------------------------

Count% = 1 '                     set initial value

START: '                         GOTO label

PRINT Count% '                   display current value of variable
Count% = Count% + 1 '            increase the value of the variable by 1
IF Count% < 25 THEN GOTO START ' go back if variable is less than 25

What this program has done is set up a loop until a certain condition is met. In order to use the GOTO command you need to set up jump points for the command to use. These jump points are known as labels. Labels follow the same naming rules as variables, that is, they can contain upper case characters, lower case characters, numbers and the underscore ( _ ) character. There is one label in the program above:

START: '                         GOTO label

This label can be used by the GOTO command as a place to jump to. The next line of the code is a simple PRINT statement that display the contents of the interger variable Count% to the screen:

PRINT Count% '                   display current value of vaiable

Then Count% is incremented, or increased, by a value of 1. This is a very common thing to do in all programming languages. Basically this line of code is saying, "Take the current value in Count%, add 1 to that value and then put the new value back into Count%."

Count% = Count% + 1 '            increase the value of the variable by 1

Finally an IF...THEN statement is used to test the condition for Count% being less than 25. If this condition is true the GOTO statement sends code execution back to the START: label and the whole process begins again. This keeps happening until the IF...THEN condition becomes false which happens when Count% reaches the value of 25.

IF Count% < 25 THEN GOTO START ' go back if variable is less than 25

Let's take a look at another program that uses GOTO branching that emphasizes why I avoid using GOTO in my programs. Go ahead and save the previous example code as GOTO.bas before typing in the next program and executing it.

'--------------------------------
'- Variable Declaration Section -
'--------------------------------

DIM Count% '     a generic counter
DIM Remainder% ' the remainder of modulus division

'----------------------------
'- Main Program Begins Here -
'----------------------------

Count% = 1 '                     set initial value

START: '                         GOTO label

PRINT Count%; '                  display current value of variable
GOTO TESTREMAINDER '             check to see if number is odd or even

EVEN: '                          GOTO label

PRINT "EVEN" '                   display EVEN after number
GOTO CONTINUE '                  jump to label to continue program

ODD: '                           GOTO label

PRINT "ODD" '                    display ODD after number
GOTO CONTINUE '                  jump to label to continue program

TESTREMAINDER: '                 GOTO label

IF Remainder% = 0 THEN '         was there a remainder?
    GOTO EVEN '                  no, number must be even, go there
ELSE '                           yes, there was a remainder
    GOTO ODD '                   number must be odd, go there
END IF

CONTINUE: '                      GOTO label

Count% = Count% + 1 '            increase the value of the variable by 1
Remainder% = Count% MOD 2 '      get the remainder of modulus division
IF Count% < 25 THEN GOTO START ' go back if variable is less than 25

Now granted, I've taken the use of GOTO to a slight extreme here, but even this little program is difficult at best to follow. Using GOTOs in a program may seem like a good idea at first to get around a nagging code flow problem you are having, but using them can easily turn your program into the nightmare scenario you see above. In later tasks you will learn superior alternatives to using GOTOs for looping. Go ahead and save this example code as EVENODD.bas before continuing on.

-- Your Turn --

Create a Guess The Number game that exhibits the following features:

- The program should display the game rules to the user.

- The number should be in the range of 1 to 100.

- The program keeps track of how many guesses the user made until guessing the right number.

- The program should tell the user if the number they guessed was too high or too low.

- The program should end once the user correctly guesses the number.

The commands you will need to use in order to create this program are: PRINT, INPUT, GOTO, IF...THEN and DIM. Save the program as GUESS.bas to your hard drive. Figure 3 below shows a screen shot of how your Guess The Number game should play out.


Guess the number
Figure 3 - An example of the Guess The Number game


-- COMMAND REFERENCE --


Commands learned in previous tasks:

PRINT
INPUT
DIM
REM or '
CONST

New commands introduced in this task:

TIME$
VAL()
IF...THEN
AND
END
GOTO
SELECT CASE...END SELECT
CASE
TO
IS
CASE ELSE
colon ( : )

Concepts learned in previous tasks:

execute
statement
expression
literal string
syntax error
variables
type declarations
literal strings
concatenation
integers
long integers
single precision
double precision
strings
null strings
reserved words
operators ( +, -, *, / )
declare
constant

New concepts introduced in this task:

relational operators
functions
nesting
order of operations
Boolean
conditions
indenting
loops
labels