Visual Basic
Assignment #8
This page has been visited
times since May 10, 1999.
What new topics are covered in this assignment?
About the For Loop
The For Loop is used to execute a section of code a predetermined number of times. Unlike the other loops that we have studied, the programmer knows in advance exactly how many times the For Loop will execute when the program is run. This is because it is determined by the programmer in the code and is not affected in any way by the user input.
For example, the following code could be used to create a Celsius to Fahrenheit table like that in assignment number 7. This creates a table for celsius values from -30 to 30 in steps of 5.
For celsius = -30 to 30 step 5
fahr = celsius * 9 / 5 + 32
celsiuslbl.caption = celsiuslbl.caption + cstr(celsius) +
chr(13)
fahrlbl.caption = fahrlbl.caption + cstr(fahr) + chr(13)
Next celsius
The first line creates a For Loop that steps the celsius variable from -30 to 30 in 5 degree increments. This means that the first time the code inside the loop is executed, celsius will be -30. The next time it will be -25, the next time -20, etc. The loop will stop once celsius equals 35, and the code will not be executed for this value.
The first line inside the loop converts the celsius value to a fahrenheit value and stores the answer in the variable fahr. The next two lines copy the values from the variables to two labels on the screen, putting the newline character at the end each time a new variable value is printed.
The loop body ends with the Next celsius line. Once this line is encountered the computer returns to the top of the loop, adds 5 to the previous value of celsius, and if celsius is still less than or equal to 30 the loop body is executed again.
Description of Assignment #8
In this assignment you will use a For Loop to create a Miles to Kilometers conversion table for Miles values from 50 to 500 in increments of 50. You can use the Celsius to Fahrenheit code above to help you create the loop, since it is much the same at this one. [KM = Miles * 1.62]
Hint: Use one label for the Miles and one for the Kilometers when printing the table. It will make it easier to line them up.
Take a look at my executable to see how mine works. Try to make yours work identically.
As always, you should read the pertinent parts of the textbook before doing the assignment. If you have any questions please email me. The executable of this assignment can be downloaded below.
Good luck.
Below are the assignment 8 downloads:
If you have Visual Basic 6 installed on your computer, download the Assignment #8 file only.
If you do not have Visual Basic 6 installed on your computer, you will need the Visual Basic 8 .dll files in addition to the assignment #8 file. The .dll download below is the same one used for assignments 1 to 7, so if you already downloaded it you will not need to download it again. Simply use the same files. Otherwise download both the Assignment #8 file and the Assignment 8 required .dll files below. To run, place all files in the same directory and double-click on mtokm.exe.
Assignment #8 file (4,000 bytes)
Assignment 8 required .dll files (1.3 million bytes)
To e-mail me click here
Page created and maintained by Ron Patterson