Professional Documents
Culture Documents
OBJECTIVES:
Using the REM command
Printing string constants
Printing more than one value in a line
Printing with tabs
Printing to the printer
The REM (short for remark) command is used to make the code more understandable to humans.
The computer ignores the command and everything that follow.
REM any message you choose to explain to fellow programmers what the
REM program is doing
You can insert as many remarks in your program as you want anywhere you want.
You will use the REM command at the beginning of each program to record you name, the file
name and the purpose of the program. You will use the REM command through out the program to
explain what your code is trying to accomplish.
Since the REM command appears so often in programs there is an abbreviation for the statement.
Instead of typing REM, you can type and apostrophe.
A string constant is one or more groups of characters inside quotation marks (“ “).
You can use string constants (sometimes called string literals) with a PRINT statement to label your
output by printing names, titles, addresses, and other messages on the screen and printer.
When you print string constants, everything inside quotation marks, including typed spaces, prints
exactly as it appears in the string constant.
To print more than one value on a line, you must separate each value with a semicolon or a comma.
You choose either a semicolon or a comma based on how closely you want the values to print.
To print two or more values next to each other, separate the values by using a semicolon in the
PRINT statement.
PRINT value1;value2;value3; . . .
If you put more than one value (the values can be variables, constants, or a combination of both)
after PRINT and separate them with semicolons, QBasic prints those values next to each other on
one line, instead of on two separate lines.
406713525.doc Zones, TAB, LPRINT
Print Zones
To print multiple values separated by several spaces, you use commas between the values. You can
use the commas in a PRINT statement to print columns of output.
PRINT value1,value2,value3,
The number of spaces separating the values varies. QBasic uses print zones to determine the
spacing between two printed values separated by commas. To QBasic you screen has five print
zones, and each print zone occupies 14 columns. Each comma represents the next print zone.
Print Zone 1 Print Zone 2 Print Zone 3 Print Zone 4 Print Zone 5
1,2,3,4,5,6,7, . . . . 14
Screen Screen Screen Screen Screen
Column 1 Column 15 Column 29 Column 43 Column 57
If your data is longer than a print zone, the data extends into the next print zone and the next value
prints one print zone over.
If there are more items listed in a PRINT statement than there are print zones in a line, the print
zones of the next line are also used, starting with the first zone
Example:
PRINT , , , “QBasic”
PRINT “Male”, “19”, “Junior”, “CS”, 18, 2.5
OutPut:
Print Zone 1 Print Zone 2 Print Zone 3 Print Zone 4 Print Zone 5
QBasic
Male 19 Junior CS 18
2.5
1,2,3,4,5,6,7, . . . . . 14
Screen Screen Screen Screen Screen
406713525.doc Zones, TAB, LPRINT
Printing Numbers
END
Output:
The age is 32
Note that in the printed output, a blank space occurs before each variable, although you did not type
a space in the program.
406713525.doc Zones, TAB, LPRINT
For positive numbers QBasic also prints spaces preceding the number. Think of this space as an
imaginary plus sign. You can not override this imaginary plus sign, so you can expect every
positive number to have an extra blank space in front of it.
For negative numbers, QBasic prints the negative sign (-) in front of the number.
QBasic does not insert a blank space before string constants or negative numbers.
Example:
END
Output:
B o o k s M o v i e s T h e a t r e
1 2 3 4 5 6 7 8 9
- 1 2 3 - 4 5 6 - 7 8 9
How could I change the code to place spaces between the works in the first line?
Place semicolon between the strings
You can suppress the carriage return-line feed sequence by putting a semicolon at the end of a
PRINT statement.
Output:
B o o k s M o v i e s T h e a t r e
END
Output:
Lion Whale Monkey Fish
Alligator Bat Seal Tiger
Dog Lizard Cat Bear
Printing with commas is similar to using tabs; a comma acts like a tab by moving the next value to
the next print zone.
The tab value is the number of characters that you want QBasic to tab over from the beginning of
the screen before printing the next value.
406713525.doc Zones, TAB, LPRINT
The semicolons before and after TAB are not required but considered good programming. The
semicolons tell you that the tab occurs immediately after the last value is printed.
END
Output:
Lion Whale Monkey Fish
Alligator Bat Seal Tiger
Dog Lizard Cat Bear
The LPRINT command is identical to PRINT except it sends output to the default printer rather
than to the screen.
If your printer is out of paper or turned off you will see the following error message:
Device fault
REVIEW QUESTIONS
9. In what column would the number 765 start, given the following LPRINT command?
LPRINT -21; 21, 0; TAB(30); 765
Lesson 3 Exercises
Write the code for each of the following exercises and save them to your Hdrive. Name your
programs Exer3_1.bas, Exer3_2.bas, etc. You will be required to run these programs. I will be
around to check your results.
1. Write a program that stores your weight (you can fib), height in feet, and shoe size in three
different variables. Print the values with descriptions in three separate lines. Then print the
values next to each other in the same line. Use appropriate REM statements to document the
program.
2. Modify the preceding program to print your weight in column in 15, your height in column
25, and your shoe size in column 35.
3. Write a program that prints (on the printer) the names and phone numbers of your three best
friends. Add appropriate REM statements to document the program. Make sure that you
print nicely underlined titles over the names and phone numbers and that the report’s
columns line up. You can use print zones or TAB.
4. For the linear equation y = (2/3) x – 4 complete the following table of values.
(2/3)x - 4 y (x, y)
Write a program that will print this completed table of values to the printer.
6. You can use PRINT to print pictures on the screen. Use a series of PRINT statements,
commas, semicolons and TAB with different keyboard characters to print your “picture” to
the screen.