Course VVBasic B - Lesson 4
Overview
This lesson introduces the students to the commands IF (with THEN and ENDIF) and BREAK. (Complex IF tests (using AND and OR) and ELSE will be covered in lesson 5.)Introductory
The first part of the introduction can be done with a blackboard or whiteboard, but ideally you can run the following program on a computer with a monitor or TV-out that the entire class can see. The program is a simple infinite loop:
VVBasic commands | Output |
---|---|
NUMBER X = 0 REPEAT PRINT X LET X = X + 1 ENDREPEAT | 0 1 2 3 etc. |
Let this program run for a while. Ask the students when they think it will stop. You will hear a variety of interesting answers. You can have some fun with this: substitute "+ 1000" for "+ 1", or use multiplication instead of addition. (Watch out for the initial X value of 0!) This is an example of an infinite loop, a loop that has no way to stop. Of course, you can press the Stop button, or pull out the computer's plug.
The way to "break out" of a REPEAT loop is with the BREAK statement. Almost all programs with REPEAT - ENDREPEAT have a BREAK statement, usually with an IF:
VVBasic commands | Output |
---|---|
NUMBER X = 0 REPEAT PRINT X LET X = X + 1 IF X > 100 THEN BREAK ENDIF ENDREPEAT | 0 1 2 3 etc. 98 99 100 |
This example introduces the IF ... THEN ... ENDIF command structure. Between IF and THEN goes an expression which the computer can determine is True or False. Usually these expressions compare two numbers, using =, <, or >. Between THEN and ENDIF go any number of VVBasic commands. These commands are executed only if the IF expression is true. Here are some examples with IF. The last one demonstrates that IFs and REPEATs can be nested. It's important that matching ENDIF and ENDREPEAT statements go in the right places. Proper indentation of the code makes this easier to see.
VVBasic commands | Output |
---|---|
NUMBER Temp = 87 IF Temp > 80 THEN PRINT "It's hot!" ENDIF PRINT "Goodbye" | It's hot! Goodbye |
NUMBER Temp = 77 IF Temp > 80 THEN PRINT "It's hot!" ENDIF PRINT "Goodbye" | Goodbye |
NUMBER ComputersNumber = 7 NUMBER Guess TEXT WantToPlay INPUT "Do you want to play? " WantToPlay IF WantToPlay = "Yes" THEN REPEAT INPUT "What is your guess? " Guess IF Guess = ComputersNumber THEN PRINT "You guessed it!" BREAK ENDIF PRINT "No. Try again." ENDREPEAT ENDIF | Do you want to play? Yes What is your guess? 4 No. Try again. What is your guess? 3 No. Try again. What is your guess? 7 You guessed it! |
Programming Lab
As always, start by pointing the browser to http://vvbasic.csed.org.-
Enter this program, which we talked about in class:
NUMBER X = 1 REPEAT PRINT X LET X = X + 1 ENDREPEAT
Run the program. After a while, click the Stop button. Change the fourth line toLET X = X * 2
and see whether that makes a big difference.
Students have lots of fun with this program. Fortunately, VVBasic handles very big numbers without hiccuping. -
The program in step 1 never ends, which can be fun.
But sometimes we want to "break" out of a REPEAT loop.
Type in this program (you can modify what you have from step 1).
NUMBER X = 1 NUMBER Lines = 1 NUMBER MaximumLines = 10 REPEAT PRINT X LET X = X * 12.34 LET Lines = Lines + 1 IF Lines > MaximumLines THEN BREAK ENDIF ENDREPEAT PRINT "All done."
Press the Run button. How is this program different from the one without the BREAK ? -
Modify the program in step 2 so that it asks the user
what the maximum number of lines should be.
Your program should have an INPUT command that makes
a dialog box like this:
The hard part for most students is determining where to put the INPUT statement. When it's in the wrong place, ask the student to "play computer" and work through what is happening. -
Type in and run this program:
NUMBER Age INPUT "How old are you?" Age IF Age > 17 THEN PRINT "You can vote!" ENDIF
Add another IF ... THEN ... PRINT ... ENDIF at the end of the program which prints "You can drive!" if the age is 16 or older. Add a third IF ... THEN ... PRINT ... ENDIF which tells kids under six they are too young for school. - Write a new program that uses INPUT to ask the user what the temperature is (in Fahrenheit). If the temperature is less than 70, the computer prints "That's cool." Otherwise, the computer prints "That's warm." Use two IF ... THEN ... ENDIF blocks.
Concluding Questions and Answers
Have the students to identify all the errors ("bugs") in these programs:NUMBER NumSongs REPEAT INPUT "How many songs on the CD?" Songs IF NumSongs < 6 PRINT "That's not very many." ENDIF IF NumSongs > 12 THEN WRITE "That's a lot!" ENDREPEET
TEXT Gender REPEAT INPUT Gender "Is your dog male (M) or female (F)?" IF Gender = "M" THEN BRAKE ENDIF IF Gender = "F" THEN BRAKE ENDIF PRINT Please enter M or F. ENDREPEAT IF GENDER = "M" THEN INPUT "What is his name? " Name IF Gender = "F" INPUT "What is her name? " name ENDIF PRINT "Please give Name a pat for me."