Programming Exercise: Looping and SUBs

Video Modules 5 & 6

Due: In lab for full credit, or by February 9, 1997 at 10:00 pm for 70% credit.

Introduction

Welcome to your first programming lab! We hope that these labs will help you to understand the topics covered by using a "hands-on" approach to learning.

Pre-Exercise Questions

Starting with this lab, you will have questions presented to you that are due at the beginning of your lab. The questions are of an overview type, and are designed to help you focus on the lab's requirements. Since we expect you to read the lab description first, these questions will be found at the end of the programming exercise. Simply write your answers to the questions on the same page and turn in your answers (individually, or combined with your partner's) at the beginning of lab.

Post Exercise Questions

Additionally, some labs have questions which are designed to be answered AFTER you have completed the lab. Again, not all labs have post exercise questions. Your TA or grader will inform you how s/he wants these answers to be handed in, which are due at the end of the lab, just like the programming exercise.

Programming Guidelines

Before you begin programming, read this section! This section contains general programming guidelines for all Lab Exercises. You are responsible for following these guidelines in all future labs.

Header

ALL programs must start with a "HEADER" which includes your

Name

Student Number

Section Number

TA Name

Program Description

This information can be included in your program by beginning each line with the Visual Basic keyword REM (you may also use the single quote character ' ). The REM keyword (or ') tells Visual Basic that the characters which appear on this line are comments as opposed to Visual Basic Code.

Here is a sample header:

' Jana Brake/Karen Clevelend

' Student Number: A20486244/A28911234

' Section #99

' TA: David Hall

' Description: This program ...

(your description should normally be 2-4 lines in length)

Function Declarations

You must DECLARE any functions which your program uses. We have not discussed function declarations in the video modules. However, this is a step that needs to be done so that Visual Basic is aware of the functions which your program uses. The function declaration for this lab are described in your text in Appendix C.

In any program you write, you must have a function declaration for every function your program uses. We will always provide you with the syntax for the function declarations your programs will need.

END Statement

Visual Basic has a keyword END. This keyword is not discussed in the text or in the video modules. The reason for this is that the END statement is conceptually very simple. The END keyword must be included as the last line in your Main Module. This ensures that your program terminates gracefully. Try running your program without the END statement present! If you do not put the END keyword as the last line of your program, VB/DOS will get confused and NOT stop running your program. You will have to manually "break out" of the program. Now, both you and VB/DOS will be confused, so in short, put the END keyword as the last line of your programs!

Saving your program

You must save all of the programs which you create in lab so that they can be turned in and graded. This can be accomplished by using one of three options from the File pull down menu.

The options are Save Project, Save File and Save File As.

Save Project

You must use Save Project in order to turn in your work using the Hand-In program.

The first time you choose the Save Project option, it will prompt you for the name of the project. Do not put an extension on this file name (e.g. use U:LOOP instead of U:LOOP.MAK). Visual Basic will add the appropriate extension.

Next it will prompt you for the name of each of the modules in your project. Again do not put an extension on the name of the file. If you used U:LOOP as the name of the project, you can still use U:LOOP as the name of the module.

In this lab, you will only have one module in your project that contains several SUBs. Later in the course you will create larger programs which consist of several different modules. The Save Project option allows you to save all modules associated with a given project at once in one quick and easy step.

Save File and Save File As

The Save Project option will save any changes made to your modules. Normally, you will not need to use the Save File and Save File As options.

Save File will prompt you for the name of a file the first time you save that module. After that, it will automatically save the file under the name you originally gave it.

Save File As will always prompt you for a file name -- regardless of whether the file already has a name -- and then save the file you are currently working on to disk with the new name. Thus Save File As is a convenient way to save a second version of a file without deleting your original version.

Running your program

Run a Visual Basic program by either

1) Pressing Shift-F5

2) Using the mouse to click on the RUN menu and choosing the START option.

Looping Exercise

Goal of Exercise

The goal of this exercise is to enhance your understanding of the four looping constructs that can be implemented in Visual Basic and to acquaint you with the concepts of creating SUBs.

The Design Team has created one SUB. Recall that a SUB is just a block of code that does a particular thing every time it's CALLed. The SUB has been added as an extension to the Visual Basic library and is therefore accessible from your program. The idea of the exercise is to CALL the same SUB with each of the four different looping constructs. As you might expect, the loops will behave differently. By observing this difference in behavior, you will gain a deeper insight into the intricacies of the four different types of loops.

After you have implemented the four looping constructs, you will need to answer questions about the behavior of each loop. You may find it helpful to read the questions after you have implemented each of the four looping constructs. This will allow you to answer the questions while you have each looping construct "on the screen."

Background

Review chapters 5 and 6. Pay particular attention to sections 5.3-5.6 and 6.3-6.7. These sections cover the four looping constructs that you will be using.

Exercise (8 points)

We have supplied you with the SUB DisplayAphorism. When CALLed, it will

  • Display an aphorism on the screen and present the user with the OK Button.
  • When the user clicks on the OK Button, control is returned to the line after the CALL to

DisplayAphorism.

You will also use the generic function FUNYesNo. This function is described in the Generic Library Modules section in Appendix C.

You must create four SUBs corresponding to the four available looping constructs in VB/DOS. Each SUB must:

  • CALL DisplayAphorism in the body of the loop.
  • Use the function FUNYesNo("Do you wish to see another Aphorism?") as the Test Condition for the loop.

Note, you will use the same message for FUNYesNo for each loop even though in some circumstances it may not seem like the proper question to ask. In fact, you will find that the question "Do you wish to see another Aphorism?" is entirely inappropriate for use with some of the looping constructs.

Create your SUBs with the following names.

SUB NAME DESCRIPTION

TopWhileThis will be the top tested WHILE loop.

BottomWhileThis will be the bottom tested WHILE loop.

TopUntilThis will be the top tested UNTIL loop.

BottomUntilThis will be the bottom tested UNTIL loop.

Next, you must write a Main module for your program with which to test each of the SUBs. The Main module will consist of a CALL to the above SUBs and lastly, the END statement. HELPFUL HINT: You should display a message indicating which SUB you are about to CALL -- this will help you distinguish which SUB's behavior you are actually examining when your program contains CALLs to all four of your SUBs. In summary:

CALL the SUBs from the Main module.

Save your program as described earlier.

Run your program.

Turning in Your Work

Save your program using U:LOOP as the project name and as the file name. The U: prefix tells Visual Basic that you want to save the file to your AFS disk space. For future labs, when you are not given a specific file name to use, your filename should clearly indicate what is contained in the file. Your TA or instructor should not have to look at all of your files in order to determine which one s/he should grade.

In this and in all future Labs, you will hand in your work by using the Hand-In program.

Pre-Exercise Questions ( 4 points)

1.In this lab, we are asking you to create new SUBs. What are their names? (1 point)

2.What is your email address? (1 point)

3.Before any coding is started, programmers design the solution to the problem. One way to help visualize the solution to a problem is to draw a structure chart. Draw the structure chart for this lab exercise. (2 points)

HINT: In this particular programming assignment, your main module will call each of the SUBs.

Post Exercise Questions (3 points)

When you have saved, run, and observed all of the SUBs, answer the following questions.

1.Which loop(s) present an aphorism before asking if you want to see one? Write the name of the loop, not its number.

2.Which loop(s) ask you if you want to see an aphorism before showing you the first one?

3. Which loop(s) quit as soon as you say you DO want to see another aphorism?

4. Which loop(s) continue to display aphorisms as long as you continue to reply that you DON'T want to see another aphorism?

5. Instead of asking "Do you wish to see another Aphorism?" for each loop, create a question that you should ask for each of the loops. You may be as creative as you like when writing the questions, and you don’t need to use the same question for each loop. Refer to your answers to Post-Exercise questions 3 and 4 if you need some help getting started.

Lab #3: Looping, p. 1