Course VVBasic B - Lesson 3

Overview

In this lesson the REPEAT, ENDREPEAT, and INPUT commands are introduced. These two commands represent two powerful programming capabilities. INPUT allows the program to interact with the user, and the lines betwee REPEAT and ENDREPEAT can be run multiple times. In this lesson, students are introduced to simple versions of each command.

Introductory

Ask the students what VVBasic commands they remember from previous weeks. Hopefully PRINT, NUMBER, TEXT, and LET will be mentioned. Remind the student what an "expression" is. Write a short program on the board that uses these commands.

Computer systems often use a dialog box to ask the user for information. A dialog box is a small window which enables the user to enter some information or choose some options; it usually has OK and Cancel buttons. For instance, when you need to select a printer most word processing programs use a dialog box. In VVBasic, we use the INPUT command to create a dialog box. A simple INPUT statement (the kind encountered in this lesson) has three parts: the word INPUT, some text in quotation marks called a prompt, and the name of a numeric or text variable. The prompt text appears in the dialog box, and the variable appears as a box. When the user types something into the box, that becomes the value of the variable. (Children who write and run their own programs can get confused about the distinction between setting a variable's value in the code with LET, and setting the value as the program runs, with INPUT. You may want to mention the difference as you introduce INPUT, and be alert for any confusion in labs.)

You may want to mention that VVBasic does not accept non-numeric input in numeric fields; or let the students discover this themselves.

VVBasic commands Dialog Box Output
TEXT Name
INPUT "What is your name?" Name
PRINT "Hi, " Name
INPUT Dialog Box Hi, Sue

Often we want to run a program more than once. One option is to click the Run button again and again. Another way is to use VVBasic's REPEAT and ENDREPEAT commands. These two commands always go in pairs, and they tell the computer that the commands between REPEAT and ENDREPEAT should be run again and again. Computer programmers call the code between REPEAT and ENDREPEAT a loop. (The issue of exiting from the loop is addressed in the next lesson. For this lesson, the student can use the INPUT Dialog Box's Cancel button or the Stop button.)

VVBasic commands Dialog Box Output
TEXT Name
REPEAT
  INPUT "What is your name?" Name
  PRINT "Hi, " Name
ENDREPEAT
INPUT Dialog Box Hi, Sue
Hi, Mike
Hi, Lee
--Program cancelled by user.--

It's fairly hard to give the students a good sense of INPUT and REPEAT in a lecture situation. The above introduction should go quickly, and some of the students' bewilderment will clear up during the lab.

Programming Lab

As always, point the browser at http://vvbasic.csed.org to start.

  1. Enter this program.
    CLEAR
    TEXT Name
    INPUT "What is your name?" Name
    PRINT "Hello, " Name
    
    Run this program a few times, entering different names. Make sure the output is correct. Click the Clear button when you're done.

  2. Enter this program:
    CLEAR
    TEXT Name
    NUMBER Age
    INPUT "What is your name?" Name
    INPUT "How old are you? " Age
    PRINT "Next year, " Name ", you will be " Age + 1
    
    Press the Run button. Do you see two different dialog boxes? Make sure the output is correct. Clear your program.

  3. Type in this program:
    CLEAR
    NUMBER X
    REPEAT
      INPUT "Enter a number:" X
      PRINT X " squared is " X*X
    ENDREPEAT
    
    Press the Run button. Try entering a few different numbers. Does the program work for negative numbers? For numbers with decimal places? What happens if you type in something that isn't a number? When you are ready to stop, click on the Cancel button in the dialog box. Don't clear the program.

  4. Change the program from step 3 so that you can enter two numbers (X and Y), and the program tells you what the sum of X and Y is.
    This is a challenging assignment. Feel free to give students reasonably specific hints, such as "Try adding another INPUT statement after the existing one."

  5. To convert degrees Fahrenheit to degrees Centigrade, subtract 32 and multiply by 5/9. Here's part of a program that does the conversion. You should fill in what's missing (the underlines).
    CLEAR
    NUMBER DegreesF
    NUMBER DegreesC
    INPUT ________________________________________________
    LET ______________________________________
    PRINT "The temp in Centigrade is " DegreesC
    
    Run the program. If the temperature is 212 F, how many degrees Centigrade is that? Put a REPEAT and an ENDREPEAT in the right positions, so that the user can convert as many degrees Fahrenheit to Centigrade as he or she likes.
    The missing lines should look like this:
        INPUT "Enter degrees in Fahrenheit: " DegreesF
        LET DegreesC = (DegreesF - 32) * 5/9
    A common mistake is to omit the parentheses.

Concluding Questions and Answers

Write the following program on the board, and have the students "play computer" and determine what the output will be.
NUMBER X = 0
REPEAT
  PRINT X
  LET X = X + 1
ENDREPEAT
More on this in the next lesson!

Evaluation Activity

Ask students to tell you how to write a program that will ask for your height in inches, and will print out your height in feet, using decimals. If they need a hint, give the three commands (NUMBER, INPUT, PRINT). Their programs should be along these lines:
NUMBER HeightInInches
INPUT "How many inches tall are you? " HeightInInches
PRINT "That's " HeightInInches/12 " feet!"