In this tutorial you will modify the Rental Car program you created in Tutorial One to incorporate REPETITIVE CONTROL STRUCTURE using the for/next statement. As in the earlier program, you will request four data items from the user (make of car, beginning and ending odometer mileage readings and number of gallons used.) And as before, the program will calculate the total miles driven and the miles per gallon and display that data in a short report on the screen. |
Unlike the first tutorial, however, this program will process three car rentals rather than just the one rental in the first tutorial. It will use the Basic for/next loop to run through the input, process and output steps for each car until all three of the rentals have been processed. | |
This tutorial also introduces the use of an accumulator (used to keep a running total of each car's resultant miles per gallon) so that the program can determine the average miles per gallon. A summary report, published after all cars have been processed, will display the number of cars rented and the average mile per gallon for these cars. | |
Follow the steps outlined below to create and run this VBasic application. | |
Note: the BASIC Reference Guide can be download from this link. It identifies the VBasic instructions you will use coding in these four programs. |
|
|
You will need all of the same variables you used in Tutorial One (i.e. the four data items requested
from the user and the two data items your program will compute.)
You will also need to declare three new variables:
|
After the existing variable declarations, enter the additional three statements:
|
|||
Notice that the code to display the Report Title is placed before the for/next processing
loop is started. That is because the title will only appear once at the top of the report. Be
sure to change the student name to yours!
|
The for/next statement will use the carIndex variable to control the number of times
the loop is processed. The for part of the for/next statement marks the start of the
loop.
|
The next three comment statements and their associated code statements are pretty much the same as used
in the previous tutorial program.
Since this code has been placed within the for/next loop, each car in turn will:
|
As currently written, each car will display three lines of data, followed immediately by the next car's
three lines of data. We can improve the way the report looks by including a blank line after each car's
listing.
|
Entering the next statement will mark the end of the for/next loop. This statement causes the program
to return to the for statement after incrementing the carIndex variable by 1 (i.e. it
adds 1 to carIndex then returns to the for statement and tests to see that carIndex is still less or equal
to 3.)
|
You're almost done! You only need to enter the code for the Summary Report. After the comment statement
that identifies the Summary Report section of the program,
|
Test your program in your browser
| |||
Once you have entered the last requested data, and if you have coded your program correctly, your program will produce the written report on the monitor and end. Be sure to verify the results with the image at the top of this tutorial. |
|