Module 4 - Knowledge |
Objectives: After studying the Module 4 Knowledge Button, you will be able to:
|
YOUR LESSON Introduction Controlling the appearance of output on the screen is an important consideration when developing a computer program. In Module 2, you learned how to use print zones to force information into preset columns. In Module 3, you learned how to control the horizontal placement of information in a row by using the TAB and SPC functions. Print zones, TAB and SPC will handle many of your output needs; however, there are two situations in which you need greater control than can be obtained with these simple methods.
Once again, QBasic has simple solutions for each of these common situations. |
Location, Location, Location With the TAB and SPC functions, it is fairly easy to control horizontal (side-to-side) spacing on the screen. The TAB function causes printing to take place at the column specified and the SPC function causes a specific number of spaces to be skipped before something is printed. However, there are times when you also need to control vertical spacing (up and down). A simple solution is to use several blank PRINT statements to force printing to be placed further down the screen as shown in the following program segment. PRINT |
|
What is the effect of all those PRINT statements?
|
|
QB has a point. Using blank PRINT statements to move vertically down the screen and PRINT TAB to move horizontally across the screen is pretty crude programming. Even though it works, it is not "good programming practice." There is another big problem with the method just described. What was the last position printed? Row 2 and column 35, right? In this program, since row 2 and column 35 has already been passed, there is no way to go back and print something higher up the screen! What's a programmer to do? |
|
When you need to control the horizontal and vertical position of output on the screen, a better choice is the keyword LOCATE. In the example above, LOCATE 2,35 and LOCATE 6,15 would have printed Name and Address in the specified row and column positions. This is illustrated in the following program segment. LOCATE 2,35 In order to gain some experience with LOCATE, type in the following simple program. Run it several times and enter different row and column positions. Notice that the INPUT statement is expecting you to input row and column positions on the same line separated by a comma. CLS After experimenting with this simple program, you may have discovered that LOCATE can position something on the screen vertically anywhere between row 1 and row 24 and horizontally anywhere between column 1 and column 80 on the screen. The big benefit of using LOCATE is that it lets you print anywhere on the screen. Also, notice that literals (Name and Address in the first example) and variables (greeting$ in the second example) can both be positioned with LOCATE. Now that you can print anywhere, it is time to gain greater control over the format (or appearance) of what is actually printed. |
|
What's Your Prediction? Study the following segment of code. LOCATE
15,40 |
This
code would force printing to begin in __________.
|
Formatting Numeric Output PRINT USING is most useful with numeric data, so we will start there. The critical part of the PRINT USING statement is the format. The format can control placement of dollar signs and commas as well as the number of places after the decimal point and even rounding a number. The way PRINT USING works printing numbers is best illustrated with an example. Download this file now: 4_5_1.bas You will notice some important remarks in the program. Be sure you take notice of this additional way to place REMs in a program to clarify what is happening. Once you download the program, study it carefully and see if you can predict the output. Then run it and see if you were correct. Be sure you notice how the format controls what is printed.
PRINT USING can be used to format any number, not just numbers that have to do with money. |
Formatting Alphanumeric Output PRINT USING can also be used with alphanumeric output, or string data. As with numeric data, a format is defined; however, the format for strings uses a backslash (\) to indicate how the output will be printed. The way PRINT USING works printing strings is best illustrated with another example. Download this file now: 4_5_2.bas Once again, you will notice some important remarks in the program that clarify what is happening. Once you download the program, study it carefully and see if you can predict the output. Then run it and see if you were correct. Be sure you notice how the format controls what is printed.
PRINT USING can be used to format string data in a variety of ways--as you will see in the rest of this module. In addition, numeric and string data can be printed with the same PRINT USING as long as the format is correct. Consequently, PRINT using is a very, very powerful way to control the appearance of output on the screen. |
In
a PRINT USING statement, number symbols (#) control the appearance of
numeric data and back slashes (\) control the appearance of string data.
|
YOUR ASSIGNMENT
Before moving on to write some simple programs, it's time to quickly check your understanding of LOCATE and PRINT USING with a short Quiz. Review the material presented in this lesson and then take the Quiz for Assignment 4.05. |
A Quick Review
|
After you complete the Quiz, return to the Game Board and begin working on Assignment 4.06. |