The text output command is PRINT. This is the command that we will be exploring through this section. PRINT takes a series of arguments. What that means is that in QBasic, after typing PRINT (note, for a short cut, just type a question mark '?') a programmer types the things he wants displayed and PRINT will display them. The first program will display the words "Hello World" on the screen.
PRINT [Text to screen]
PRINT "Hello World"
First, copy everything (in this case, one line, but later programs will be much longer) into a text editor or into QBasic itself and save it as '1HELLO.BAS'. Next open it in QBasic (if you are not already there) and press F5. This will run the program. A black screen with the words "Hello World" in white on your left hand side of the screen should appear. This is your first QBasic program. Now, Press F5 again. Now there should be 2 lines saying "Hello World". This is because QBasic does not clear the screen every time a program is run. To change this we will use the command CLS, which stands for CLear Screen. We will be looking at some other new concepts in the next program as well.
PRINT "This line will be erased" CLS PRINT "Hello"; PRINT " World", PRINT "Hello Jupiter" PRINT "Good Bye",,"For";" Now" PRINT 1,2,3,4,5
Commas and Semicolons in PRINT and CLSEdit
Here is what the program output should look like:
Hello World Hello Jupiter Good Bye For Now 1 2 3 4 5
Addressing 2HELLO.BAS in line order, the first line prints "This line will be erased." to the screen, but in the second line, the CLS command clears the screen immediately after, so it will not be seen (technically it flashes at the bottom form columns). "Hello Jupiter" should line up with '2' at the bottom. More than one comma can be used in a row. In this example, after "Good Bye" two commas are used to move "For Now" over two tab columns. "For Now" should line up with '3'.
My final statement on this topic is to play around with it. Try using commas and semicolons in a program.
CLS hello$ = "Hello World" number = 12 PRINT hello$, number
Variables are used to store information. They are like containers. You can put information in them and later change the information to something else. In this first example they may not seem very useful but in the next section (Input) they will become very useful.
In this example we use two types of variables - string variables and numeric variables. A string variable holds words, a string of characters (a character is a number, letter or symbol). In this case the characters are letters. A string variable is denoted by ending the name of the variable with a dollar sign. The string variable in this program is hello$. What ever you set hello$ equal to will be displayed in the PRINT statement. The numeric variable is number. Numeric variables do not have a special ending like string variables.
CLS LOCATE 14, 34 'position the left eye PRINT "<=>" 'draw the left eye LOCATE 14, 43 'position the right eye PRINT "<=>" 'draw the right eye LOCATE 16, 39 'position the nose PRINT "o|o" 'draw the nose LOCATE 18, 36 'position the mouth PRINT "\_______/" 'draw the mouth LOCATE 19, 42 'the bottom PRINT "The Face by QBasic"
Locate allows you to position the cursor for the next piece of text output. Contrary to cartesian coordinates which read (X,Y), the locate statement is LOCATE Y,X. In this case Y is the distance down from the top of the screen and X is the distance from the left side of the screen. The reason that LOCATE does not follow the standard coordinate system is that it is not necessary to include the X portion, you can use the format LOCATE Y which just specifies the line to start on.
CLS LOCATE 14, 34 COLOR 9 PRINT "<=>" LOCATE 14, 43 PRINT "<=>" COLOR 11 LOCATE 16, 39 PRINT "o|o" COLOR 4 LOCATE 18, 36 PRINT "\_______/" COLOR 20 LOCATE 19, 42 PRINT "U" LOCATE 1, 1 COLOR 16, 1 PRINT "Hello World"
The program 5FACE.BAS is broken into sections to make it easier to read. This is an example of a good programming habit. Each three line piece of code each piece of code specifies what color it's part of the face should be, where it should be and what it should look like. The order of the position and the color is unimportant. The new statement COLOR allows you to change the color of the text. Once changed, all output will be in the new color until COLOR or CLS is used. The format for the COLOR statement is:
The colors are designated by numbers which will be discussed in the next section.
Color by NumberEdit
There are 16 colors, numbered from 0 to 15.
|0||Black||8||Dark Grey (Light Black)|
|6||Brown/Orange||14||Yellow (Light Orange)|
|7||Light Grey (White)||15||White (Light White)|
If you look carefully at this chart you can see that there are 8 main colors (0 through 7) and then those colors repeat, each in a lighter shade. You may also notice that the colours act as a combination of binary values (where blue=1, green=2, red=4, etc.) This makes it much easier to memorize the color scheme. Blinking colors are also available: at 16, the colors start over again with blinking black and extend through 31 (blinking white). However, the blinking option is not available for the background, only for the text (foreground).
It is possible to switch the blinking foreground text with an intense background, but this task is beyond the scope of the QBasic textbook, and may not work when Windows displays the console in windowed mode.
In this section we looked at several methods to manipulate output. All centered around the PRINT statement. LOCATE and COLOR modified where the text was displayed and how it looked. We used CLS to clear the screen. There was also a basic introduction to variables which will be expanded upon in later sections.