Module 4 - Knowledge
Assignment 4.05


Objectives: After studying the Module 4 Knowledge Button, you will be able to:

  1. Describe the purpose of the LOCATE statement.
  2. Distinguish between the use of LOCATE, TAB, and SPC.
  3. Describe the correct syntax for the LOCATE statement.
  4. Describe the purpose of the PRINT USING statement.
  5. Describe the correct syntax for the PRINT USING statement.
  6. Describe how to create the format of a PRINT USING statement for either numeric or alphanumeric data.

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.

  • The first situation arises when you need to be able to control the vertical (up and down) position of information.
  • The second situation arises when you need to format data in a specific way, such as the inclusion of a dollar sign.

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
PRINT
TAB (35) ;"Name: "
PRINT
PRINT
PRINT
PRINT
TAB(15); "Address: "

What is the effect of all those PRINT statements?

  • Name will be positioned at row 2 and column 35.
  • Address will be positioned at row 6 and column 15.

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
PRINT
"Name: "
LOCATE 6,15
PRINT
"Address: "

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
LET
greeting$="hi"
INPUT
"Enter a Row and a Column (Row,Column)";row,col
LOCATE
row,col
PRINT
greeting$
END

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
PRINT "Just Testing"

 
This code would force printing to begin in __________.

column 15 and row 40
row 15 and column 40
15 seconds at row 40
none of the above
 


Formatting Output

Programmers spend a great deal of time planning the appearance of the output on the screen. You have just seen how easy it is to control the vertical and horizontal alignment of information on the screen with LOCATE.

Precise positioning, however, would be wasted if the output did not look nice. Take a look at the example below and see if you can identify what is wrong with the appearance of the data. There are three basic problems.

$456.3
$2.3456
$10045.234967

Did you come up with the following list of problems?

  1. The decimal points do not line up.
  2. Since these data represent money, there should be two places after the decimal point.
  3. Technically, the third number should have a comma (for example, $10,045.23).

Fortunately with one minor, but very powerful, addition to the PRINT statement, all three of these problems (and more) can be easily solved. The solution is another print function called PRINT USING. Both numeric and alphanumeric (string) data can be controlled with PRINT USING.

Run the following small program and observe the results.

LET number1 = 1045.239
PRINT USING
"$##,###.##"; number1
END

PRINT USING was used to define a specific format to control the way the numeric variable was printed. Let's look at the required syntax of the PRINT USING statement more closely. The PRINT USING statement consists of three parts, regardless of whether numeric or alphanumeric data is being printed.

  1. The statement always begins with PRINT USING. No mystery there.
  2. Next, inside of quotation marks is the format that controls the way the output will be printed. The "code" is different for numeric and alphanumeric variables.
  3. Last, but not least, there is a variable list. More about that in the next section.

Remember that structure for the statement...PRINT USING, format , and a variable list.


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.

  • The number symbols (#) in the format represent the number to be printed and how many decimal places it will contain.
  • If you print fewer decimal places than the number actually has, rounding takes place if needed.
  • Adding one dollar sign to the format causes a dollar sign to be printed in a fixed position.
  • Adding two dollar signs to the format causes the dollar sign to "float" to the position in front of the number.

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.

  • A pair of backslash symbols (\) in the format represent the string data to be printed.
  • You can control the horizontal positioning of the data by increasing or decreasing the amount of blank spaces in the format.
  • You can chop off the string data if the spaces between the pair of backslashes in the format is too small.

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.

True
False

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

Before you move on to the next assignment, please make sure that you can do the following.

  1. Describe the purpose of the LOCATE statement.
  2. Distinguish between the use of LOCATE, TAB, and SPC.
  3. Describe the correct syntax for the LOCATE statement.
  4. Describe the purpose of the PRINT USING statement.
  5. Describe the correct syntax for the PRINT USING statement.
  6. Describe how to create the format of a PRINT USING statement for either numeric or alphanumeric data.

Adding LOCATE and PRINT USING to your programs will give you much greater control over the appearance of output on the screen.


After you complete the Quiz, return to the Game Board and begin working on Assignment 4.06.


 

previousnext