Course VVBasic B - Lesson 5

Overview

In lesson 5 students learn more about the IF command: using ELSE and making more complex tests with AND and OR. The multiline capability of INPUT is introduced.

Introductory

Review IF - THEN - ENDIF and INPUT with the students. Write the following two programs on the board, and show the students that they are equivalent in function:

VVBasic commands Output
NUMBER Age
INPUT "How old are you? " Age
IF Age > 15 THEN
  PRINT "You can drive!"
ENDIF
IF Age <= 15 THEN
  PRINT "You have to wait."
ENDIF
  How old are you? 11
You have to wait.
NUMBER Age
INPUT "How old are you? " Age
IF Age > 15 THEN
  PRINT "You can drive!"
ELSE
  PRINT "You have to wait."
ENDIF
  How old are you? 11
You have to wait.

The first program uses <= for "less than or equal." Similar are >= for "greater than or equal" and <> for "not equal." These are needed since the keyboard does not have keys for ≤, ≥, or ≠. The second program uses ELSE. Every IF command can optionally have an ELSE. When it does, commands between THEN and ELSE are run if the IF test is True, and commands between ELSE and ENDIF are run if the IF test is False.

The students have seen INPUT commands which ask the user to enter a value for one variable. An INPUT can actually ask for more than one value. To do this, use multiple variables, and put a text string prompt before each variable. Also, put a comma after a variable if it's not the last one. Here are some examples:

VVBasic commands Dialog Box Output
TEXT Name
NUMBER Age
INPUT "What is your name?" Name,
      "How old are you? "  Age
PRINT "Hi, " Name ", how do you like being " Age "?"
INPUT Dialog Box    What is your name? Mike
   How old are you? 11
Hi, Mike, how do you like being 11?
TEXT Movie1
TEXT Movie2
TEXT Movie3
INPUT "What are your three favorite movies? " Movie1,
      "" Movie2,
      "" Movie3
PRINT "I like those movies, too!"
INPUT Dialog Box    What are your three favorite movies? Shrek 1
   Shrek 2
   Shrek 3
I like those movies, too!

The second program shows that the prompt can be the empty string "". We can put some IFs after the INPUTs to make the programs more interesting. An IF test can have AND or OR connecting two separate tests. If you use AND, then both small tests have to be True for the entire test to be True. If you use OR, then either small tests can be True and that makes the entire test True. Make the following examples more fun by using specific values provided by the students.

VVBasic commands Output
TEXT Name
NUMBER Age
INPUT "What is your name?" Name,
      "How old are you? "  Age
IF Name = "Mary" AND Age = "12" THEN
  PRINT "What a coincidence!"
ENDIF
   What is your name? Mary
   How old are you? 12
What a coincidence!
TEXT Movie1
TEXT Movie2
TEXT Movie3
INPUT "What are your three favorite movies? " Movie1,
      "" Movie2,
      "" Movie3
IF Movie1 = "Toy Story" OR Movie2 = "Toy Story" OR
   Movie3 = "Toy Story" THEN
  PRINT "I like Toy Story, too!"
ELSE
  PRINT "I don't like those movies."
ENDIF
   What are your three favorite movies? Shrek 1
   Shrek 2
   Shrek 3
I don't like those movies.

Programming Lab

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

  1. Suppose you want to write a program that will check if three words, typed in by the user, are in alphabetical (dictionary) order. Here's a start on the program. Type it in, replacing the underlines with the correct VVBasic code.
    TEXT Word1
    TEXT Word2
    __________
    PRINT "Please enter three words."
    INPUT "First word: " Word1,
          "Second word: " Word2,
          ______________________
    IF Word1 < Word2 AND Word2 < Word3 THEN
      PRINT "The words are in alphabetical order."
    ELSE
      __________________________________
    _____
    

  2. Here is a program where you can try to guess the roll of two dice:
    NUMBER R1
    NUMBER R2
    NUMBER Guess1
    NUMBER Guess2
    REPEAT
      LET R1 = Random(1,6)
      LET R2 = Random(1,6)
      INPUT "What is your guess? " Guess1,
            "and your second guess? " Guess2
      IF Guess1 = R1 + R2 THEN
        PRINT "That's it!  The computer rolled " R1 " and " R2
      ELSE
        PRINT "Sorry, the computer rolled " R1 + R2
      ENDIF
    ENDREPEAT
    Type in the program and run it a few times. You'll see that your second guess is ignored. Change the IF command so that it tests if Guess1 or Guess2 equals R1 + R2. (Use OR and two = signs.)

  3. Type in and run this program:
    NUMBER Test1
    NUMBER Test2
    NUMBER Average
    INPUT "Score on test 1: " Test1,
          "Score on test 2: " Test2
    LET Average = (Test1 + Test2) / 2
    PRINT "The average is " Average
    IF Average >= 90 THEN
       PRINT "That's an A."
    ENDIF
    
    Make sure the program is giving you the right answer! Now change the program:
    1. Put a REPEAT and an ENDREPEAT in the program so that the user can get averages for multiple sets of test scores.
    2. Make the program ask for three test scores, and compute the average using all three.
    3. Have the program print out "That's not an A." if the Average is less than 90.

Concluding Questions and Answers

Write the following program on the board. Ask the students if it works correctly for all ages. Do the four IFs cover all the possibilities?
NUMBER Age
INPUT "How old are you?" Age
IF Age = 11 THEN
  PRINT "You are a pre-teen."
ENDIF
IF Age = 12 THEN
  PRINT "You are a pre-teen."
ENDIF
IF Age < 11 THEN
  PRINT "You are not a pre-teen."
ENDIF
IF Age > 12 THEN
  PRINT "You are not a pre-teen."
ENDIF
Ask, can the four IFs be replaced by a single IF, which uses OR and ELSE? The answer is yes:
NUMBER Age
INPUT "How old are you?" Age
IF Age = 11 OR Age = 12 THEN
  PRINT "You are a pre-teen."
ELSE
  PRINT "You are not a pre-teen."
ENDIF
Why is the second version preferable?

Evaluation Activity

The English teacher, Mr. Smith, wants you to write a VVBasic program for him. Mr. Smith wants to be able to enter two scores. If both scores are 100, then the program should print out "Terrific!" Otherwise, the program should print the average of the two scores.