Hello everyone and welcome to the text version of the QBasic/QuickBasic PRINT Tutorial! In this tutorial we will cover how to using the PRINT command it’s various different uses for formatting text to the screen. We will also cover comments, CLS and the END commands.
Before we begin you will need the QBasic/QuickBasic compiler. Note that this tutorial will also work with QB64. You may also watch the video version of this tutorial HERE.
Below is a tutorial on setting up QBasic with DOSBox-Staging and 86Box to work with QBasic. If you would prefer you can also use QB64 - Phoenix Edition which will work on Windows, Mac and Linux.
So first up? Comments
There are two types of comments inside of QBasic. The first is the old REM and the 2nd is the newer ‘ apostrophe. What is the difference between the two? Well REM has to go on a line by itself. This means that you can’t put REM to the right of any command. The ‘ however can go anywhere even to the right of your code! Below is an example of the two in action. Simple right?
REM - This is a comment
CLS ‘This is also a comment
CLS and END
CLS and END? What do they do? Well CLS clears the screen. If you hit F1 in the editor it will pull up the online help which is built-into QBasic. CLS has quite a few options available but we will cover those in a later tutorial. For now we will just use the default which will clear the entire screen. What happens if we didn’t use CLS? Well if we didn’t then everything we print/draw to the screen would stay. It would create quite the mess eventually.
The END command tells our program, procedure or code block that it has ended. We will us this later on future tutorials. But for now just know it tells the program that we’ve finished and to END.
REM - This is a comment
CLS ‘ This is also a comment
END ‘Tells the program to exit
PRINT
So now that we have the basic framework of our small example program setup. Let’s get to the meat of it all which is PRINT. Using PRINT is extremely simple the syntax is just PRINT [expression list] and optionally we may use [{,|;}] we will cover , and ; shortly. So for now let’s print Hello World! to the screen.
PRINT “Hello World!”
If we were to hit F5 to run this. We would get what you see below.
Congratulations! You have completed your first full program that has printed something to the screen. Now what if we wanted to print another line but put a space in between the lines? Well that is easy because if you use PRINT without an expression it prints a blank line.
PRINT “Hello World!”
PRINT “3rd Line Hooray!”
As you can see we have a blank line between our first and second line. This is a good time to mention that in QBasic we are working in text mode at the moment. Which means rather than pixel space instead we are using character space. What that means is that the screen is broken up into columns and rows. The default of which is 80x25. There are ways to run it in other text modes. But we are working in the default. This means we have 80 characters columns and 25 character rows. So our 3rd line is on Row #3.
We will cover this more in depth as we go along but it will be useful for later on this tutorial. Below is our code so far
REM - This is a comment
CLS ‘Clears the screen
PRINT “Hello World!”
PRINT “3rd Line Hooray!”
END ‘Ends the program
Print variables and ;
Now let’s take a look at printing variables and what the optional ; does with the print command. So first let’s create two new variables a and b. Let’s give a the value of 10 and b the value of -20.
a = 10
b = -20
Now before we print them let’s quickly talk about ; and positive/negative numbers in relation to print. So the ; argument essentially what this does is it prints everything without spacing. However as you will see in a moment numbers do have spacing. Or do they?
Well no you see with the PRINT command QBasic knows what’s a number and what isn’t. It always leaves the left side blank when it’s a +10 number and when it’s a negative number it displays the - symbol. So let’s go on ahead and print this so we can see exactly what it looks like.
PRINT a; b
As you can see 10 is 1 character over and -20 also has a space between a. The reason for this is as explained above. This is handy to keep in mind depending on what program you are writing as it may mess up your formatting if you were unaware.
Now let’s talk about the ; argument. The semicolon terminates the print expression and immediately prints the next one. So let’s take a quick look at what that looks like.
PRINT “Hello”; “World”; “!”
As you can see it prints it all without any form of spacing. If you wish to add spacing you will have to add it within your quotations.
PRINT with ,
So the comma argument works similarly to the semicolon in that it terminates the PRINT expression. However rather than printing the new expression without spacing it starts printing it in the next PRINT zone. Within QBasic there are 5 print zones.
Zone 1 - Starts at Column 1
Zone 2 - Starts at Column 15
Zone 3 - Starts at Column 29
Zone 4 - Starts at Column 43
Zone 5 - Starts at Column 57
This allows you to PRINT your text into any of the zones. NOTE however that if your expression is longer than a zone it will roll over into the next zone and even into the new line if it runs out of space. Now let’s type out an example of what this looks like
PRINT “Zone 1”, “Zone 2”, “Zone 3”, “Zone 4”, “Zone 5”
PRINT with TAB
So when you read the word TAB you probably think of tabbing like hitting your TAB key on your keyboard. Well you’d be sorta not really correct. Essentially how the TAB command works is that it tells print to start printing on X column. Remember we have 80 columns and if it goes out of bounds it will print on the next line. Let’s take a look at this in practice and see what we get.
PRINT TAB(10); “This is a tab of 10.”
If you were to run this you would see that it starts printing 10 character over or at column 10. But what happens if we add another TAB but also at TAB 10?
PRINT TAB(10); “This is a tab of 10.”; TAB(10); “This is also a tab of 10.”
Now hang on here!? Why is it printing on the next line? Well the answer to that is simple! You are telling to print at a TAB of 10 but it’s smart enough to know that you already printed something there so it moves on to the next line at column 10! Pretty nifty right?
So what are tabs useful for? Well tabs are useful for setting your own style of formatting. You can easily control where the data goes so long as you don’t use to many characters and push it all onto the next line.
LPRINT
The last command we are going to look at is the LPRINT command. This functions exactly as the PRINT command except it prints straight to the printer! Very useful if you wish to print files or anything you’ve made to the printer. There isn’t really a way to show this working however if you happen to have a printer hooked up or DOSBox/86Box setup with a printer it will send it right off and print anything you tell it too!
Conclusion and FULL CODE
So that ends this tutorial! I hope you all enjoyed it. You can watch the video version which is linked below.
REM - This is a comment
CLS ‘Clears the screen
a = 10
b = -20
PRINT “Hello World!”
PRINT “3rd Line Hooray!”
PRINT a; b
PRINT “Hello”; “World”; “!”
PRINT “Zone 1”, “Zone 2”, “Zone 3”, “Zone 4”, “Zone 5”
PRINT TAB(10); “This is a tab of 10.”; TAB(10); “This is also a tab of 10.”
END ‘Ends the program
Thank you for reading The Retro Dev! Please consider supporting us if you like our content.
Site * Forum * Odysee * YouTube * Merchandise