[go: up one dir, main page]

0% found this document useful (0 votes)
23 views33 pages

Qbasic Exercises

This document serves as a tutorial for programming in QBasic, covering essential commands such as PRINT, CLS, FOR, NEXT, and STEP. It explains how to use variables, including naming conventions and types, and introduces string variables for storing text. Additionally, it discusses user input through the INPUT statement and conditional statements using IF...THEN...ELSE for decision-making in programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views33 pages

Qbasic Exercises

This document serves as a tutorial for programming in QBasic, covering essential commands such as PRINT, CLS, FOR, NEXT, and STEP. It explains how to use variables, including naming conventions and types, and introduces string variables for storing text. Additionally, it discusses user input through the INPUT statement and conditional statements using IF...THEN...ELSE for decision-making in programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 1

Sometimes if you load Qbasic from DOS you will find that the remnants of the commands
they remain in the background. For example C:>QBasic would remain in the background if you load it from DOS. No
you need to worry about it, it is resolved with the CLS command, which allows you to clear the
screenbefore doing the next operation. For example, write the following and then
press F5.
CLS
PRINT "Hello World"
Once you have run the program, you will see that the remaining information is cleaned of
new and the screen goes black with 'Hello World' written in the corner. The commands
PRINT y CLS are useful in my Reference section. You can use the PRINT command
as many times as you want in your program. For example, write the following:
CLS
PRINT "Hello World"
Hello again
Goodbye
It will write the three complete lines, one after another on the screen. So far, you shouldn't...
having had no errors and if you have had one, it may be one of the following:
"Hello World"
You cannot put more than two quotes in any PRINT statement.
CLS
This command should be read as CLS not CL S.

CLS
PRINT "Hello World"
You should never forget to open or close quotes at the beginning or end of the PRINT statement.
This is a useful tip. Move the cursor to the end of the 'PRINT' statement. It will not happen.
nothing in it. Press F1. A help menu will be activated containing information about that
particular sentence. This will help you a lot if you are stuck within a command
particular.
Up to this point, you have learned two new commands: PRINT and CLS. Now we will teach you two more.
new commands called 'FOR' AND 'NEXT'. Both commands are very useful. For
To delete the previous program, press ALT+F simultaneously. Follow the instructions.
until I start with a clean page.
Now write the following program as shown below:
CLS
FOR i=1 to 10
PRINT "Hello World"
NEXT i
Press F5 to run the program and observe what happens. You will find that the
The phrase 'Hello World' appears on the screen 10 times in a row. Now we are going to
look at how each part of this program works.
CLS
Clean the screen of remnants from previous programs that might be there.
FOR i=1 to 10
This is an interesting part; 'i' is a variable, variables are commented on in the
tutorial 2, the variable contains a value. To begin with, the variable will take the value 1. Each time
when the statement "NEXT i" is reached, the program will check that the variable "i" is 10. If
it is not 10 the program will continue and return where the FOR command was. This is like
a requirement for the program to continue, as long as the variable "i" is not 10
it will return and add 1 to the number it had. If you modify the FOR loop so that it reaches
up to 20 'NEXT' will check that 'i' has reached the value of 20. For example:
1,2,3,4,5,6,7,8,9,10 STOP!!!!
Ó
FOR i=1 to 20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
STOP!!!!
Every time the program goes back, it executes all the statements contained within 'FOR'
and "NEXT". If you were to write a program like this:

CLS
FOR i=1 to 10
NEXT i
This program will still do the same but will not execute anything between 'FOR' and 'NEXT'.
the reason is that there is nothing in the first place. Imagine that you have awatchwith only 10
seconds. The clock will go 1,2,3,4,5,6,7,8,9,10 and then it will start over. The same happens with
the previous example, only that we are not starting over.
In the previous example, the program adds 1 to the variable 'i' each time it reaches the
"NEXT" statement. We could change the program and add 2 to the variable "i". You'll see what
You will be able to run the following program after pressing F5.
CLS
FOR i=1 to 10 STEP 2
PRINT "Hello World"
NEXT i
We just taught you another command called 'STEP'. This command specifies how much
you can continuously increase the interval until you reach the statement 'NEXT'. The
The 'FOR' and 'NEXT' loop is like a counter. It's as if you and I are counting.
how many fingers do we have. In the previous example, we will increase the interval by 2 in all
the sequences up to the sentence 'NEXT'. This is an example:
2, 4, 6, 8, 10 STOP!!!!
We understand what the statement 'PRINT' does. It will write 'Hello World' 10 times.
column unless you change any value contained in the "FOR" statement:
The sentence "NEXT" is easy and clearly explained above. Try to change the
Change the statement 'NEXT i' to 'NEXT' and observe what happens. Basically, you just need to remove the 'i'.
from the end of the sentence. If you do it by removing the variable y from the statement "NEXT", the
the program will still work. But what I suggest is that you do what I explained above
because in a long program it is good to see what 'NEXT' will do. Later I will explain it.
NEXT statement with greater depth.
Thus the program will write "Hello World" five times on the screen because we have
increased the interval to double. It may seem complicated at first but like in
In any language, everything has its place. The best thing is to try to change its value.
"STEP" to see what kind of results are obtained. Also try changing the value of "i".
and the one for 'to' for another. I tell you that you will have a goodgame.

Therefore, in the first part of this tutorial, you will have learned five new commands. They are
these
PRINT, CLS, FOR, NEXT, STEP.
Later I will show you what else you can do with these commands, but try to take a look at a
glance at the help menu. You will need to understand variables and strings and how
to use them. In the next chapter, I will teach you.
Chapter 2
Variables are very important in all programs in one way or another. With the
You will be able to store relevant numerical information that you can operate with.
Afternoon. For example, you could have five different numbers that you want to add together.
You can do it very easily in basic. You saw in Tutorial 1 how a variable is
used in the 'FOR' and 'NEXT' statements. We gave the variable 'i' the value 1 to
start, then we added 1 each time the loop started using the statements
descriptionformal.
A variable is simply a name you give to an area of thememoryin which the value
data is saved. When you need to extract this part of the data, or modify this value, you can
refer to the memory location by the name of the variable. For example in a
telephone program the variable age may contain the person's age in the
current events. As the program runs, the stored value in name could change many
times in a couple of years.

Naming Variables
You are free, in a program, to give the variable a meaning, a descriptive name.
When writing a program, think that the variable names should be
significant. In a program, try to make the variable name applicable to any
variable that you are storing. For example, the variable name phone is obviously that
it will contain some type of phone number. So if it is a phone number that
You want to keep it, it's better not to call it something like eggs. Trust me, it will make your life easier.
the later you go to write long programs.
Variables come in all shapes and sizes. This is a common list of variables:
The first character must always be a letter (i.e A,G,D,F,z,e,t, or y)
2. After that, the true first letter in the variable name can be letters, numbers
the underlined ( _ ).
3. The final character can be %, &, !, #, $; or nothing. Vbasic users should use a
own character as final letter and not leaving it with nothing.

4. Remember that the variable name cannot be a reserved word. For example
In QBasic there is a statement called 'PRINT', you should not use this or any other.
sentence as a variable name.
In QBasic, it doesn't matter if the names of your variables are in uppercase or lowercase.
For example, if you write a variable called cost and later in your program you call it
with the name cost, you will find that the name of the variable will relate to
any other in the program. If you have a program lying around, please clean it using
NEW from the menu bar at the top of the screen. Here you have a program that
could you write as shown below:
CLS
coste=123456
PRINT cost
COSTE=789101
PRINT cost
As you can see, there are numerous things happening when you write the program in
QBasic. First of all, be aware of how the statements change from uppercase to
lowercase. You can see it only if you write it inside the program just as it is above. It
The next thing you need to notice is how the names vary from uppercase to lowercase.
As mentioned earlier, everything will change according to what you write in the program, but the statement
it will always be the same.

If you have written in the program before, press F5 now. The program will run. The
the screen would be cleared and the program will be displayed. What this program is doing is
store two different values within the variable name 'COSTE' or 'coste'
Depending on how you wrote it. Initially, the first value will be shown and from
from that moment it will change during the program showing the new value. Thus the
programs will be able to change the variable information immediately and as many times as
I want.
Imagine a variable as a box. First, you put a number in the box, for example, 1.
Then you replace it with the five. This operation is the same as taking out the 1 from the box and
put the 5 inside. You must remember that QBasic only allows variable names not
longer than 40 characters. Now we have talked about numerical variables. We will talk
now about chains.
Chains
We will talk about strings in the QBasic Tutorial 2, but for now we will just make a brief one.
description.
A string is a basic way to store information in a variable. For example
could you save the user's name or address in a string? Here you have a
small code to show you how it works:
Freddy Bloggs Sister
PRINT name1$
PRINT nombre2$
This is a very simple program that stores information in a string variable. If
you write this program and press F5 to run it, it will show you what the name of the
variable line by line. The name of the string variable does not have to have values
numerical inside. In the example above I have used two variable names both with
the numbers '1' or '2' at the end. I have only done this to separate the different parts of the text.
Could I have this:
CLS
Freddy Bloggs
Freddy Bloggs sister
PRINT nameone$
PRINT name2$
The dollar sign ($) must go at the end of the variable name. This implies that it is a
string variable (string only). VBasic can handle strings and variables in
different forms, these are called Variants. But for now it is better to know the method
easy for QBasic to understand. QBasic can handle up to 32,767 characters in
any string. The only time you might exceed is if you are writing in an editor of
text. There are methods in which you can take another approach to this problem. If I
Okay, I will explain it to you later.
Chapter 3
If you have read the previous tutorial, you will have seen how to handle variable strings in QBasic or
virtually at any other level of basic. If you haven't taken a look, I suggest you do.
do it now.
Below we have a small program that has information to appear in
screen and at the same time contains variable information to store in memory.
Write the program exactly as shown and press F5 to run it.

name1$="Freddy Bloggs"
valor1=500
Mr Blob
valor2=350
PRINT name1$
PRINT valor1
PRINT name2$
PRINT valor2
(value=money)
Once you have run the program, you will see that it will write all the information.
contained in the variables on screen. This is a simple method to write variables. By
for example, can you rewrite the program to change the values contained in the variables,
but asking the user to enter the information forkeyboardLet's do it
Now, clean the program we just made and write the following:
CLS
Your Name:
Your Age:
PRINT "Hello";name$;"Your age is";age
This is a good example of how to input information from the keyboard. First of all, the
the program will clear the screen. Then the program will ask for the name that should be
entered from the keyboard. Then it will ask for the age and then display the results.
on screen.
Let's take another step at a time and take a closer look at the statement 'INPUT'.
"INPUT" allows reading characters from the keyboard and files. We will take a look at the
files later. The statement "INPUT" can be used in many situations, these
these are some for now.
Your Name:
You should notice that the ';' have been replaced by ','. This will stop with a '?'
appearing at the end of the INPUT statement that I am entering once the program
It is running. Change the program that is showing to the line that is shown above.
INPUT name$
The example above will not give you a signal to enter something and will wait for you to
User meta something. Once you have pressed return, it will save the information in the variable.
name$. If you try to carry out the previous example, observe how the '?' sign appears in
the screen. You can clarify it by doing the following:
INPUT "", name$
This shows nothing and only waits for input from the keyboard. Once you press
return will move to the next sentence (if there is one). The only thing you must remember is when
a string is a string and a value is a value. You know you will have "$" at the end of the
variable name, and a variable value will only have the standard value and nothing else.

So we will recap the second program in this tutorial.


CLS
Your Name:
Your Age:
PRINT "Hello " + name$d
Be aware that in the previous example the statement 'PRINT' has been changed now. I
replaced some of the characters ';' with characters '+'. This makes it look more
understandable and the work is more efficient than before. The part that does not work well using
The character "+" only works with variable strings. This is because it does not
we can place it at the end of the line to connect the end of the text with the variable 'age'. It is
a pity, but again the good side of this method is that it is much more used in
Visual Basic. You will see in the following tutorial that this method is much more widely used.

TASK 1
1.1. Create a program that allows you to input personal information and display it on the screen.
The program should display the personal address on the screen in a single line with four
string variables for each part of the address. Remember that the string variable can
contain numeric characters. The solution is at the end of this part of the Tutorial.
The IF.....THEN.....ELSE statements
Surely in more than one program you will be able to use the statement 'IF'. 'IF' executes a
statement or a block of statements depending on the specified conditions. If this
It seems complicated to you, take a look at the following part of the code. Don’t give up if it seems difficult.
complicated, just look at it over and over again until you understand it and you will understand it.

CLS
How OLD are you:
If years < 18 THEN
You are underage
ELSE
PRINT "It's great!!!!"
END IF
Wow! Look at this. An interesting program that will allow you to verify the information that
you type and operate with it. What this program is doing is checking that the value
the entered value is greater than, less than, or equal to 18. It will write the solution to the value you have entered.
The best you can do is write it exactly as shown and press F5. I have used
the tab key to leave spaces in the 'PRINT' statement. It's a good way
to start doing it. The way to program it is very important.
First, let's take a closer look at this program; we are going to present some
rather important conditions when writing computer programs.

= is equal to

is not equal to

> is greater than

< is less than

is greater than or equal to

is less than or equal to

In the previous example, we used the condition 'is less than'. Let's set the
a simple sentence in plain Spanish. Everything you are doing is:
If the years are less than 18 then write 'you are a minor'. If they are 18
or more will write "it's great!!!!"
When we have 'EQUAL' in the previous operators, we are referring to the same or that value.
in itself. For example, "IF years <>18 THEN END" would mean that if the years are not 18
it would end. Just reflect for a minute on it, once you have done that you will be on the right track
I walk with the statements 'IF', 'THEN' and 'ELSE'.

1 or 2?
IF i% = 1 OR i% = 2 THEN
PRINT "OK"
ELSE
Out of range
END IF
The above is an example taken from QBasic. Observe what is happening. In the first
place the program will ask for a number. You can enter a 1 or a 2. The program
the "IF" statement will run to check if the entered number is a 1 or 2. If the number you have
if it is a 1 or 2 it will write 'OK', otherwise it will write 'Out of range'.

After this, it will only put a standard 'END IF' at the end.
Just by taking a look at some of the programs that have been written, you will be able to
check how easy it is to do the work. You can obtain more complicated information
about the statements "IF", "THEN", and "ELSE" in the QBasic help menu. You can
access it by typing "IF" and pressing "F1".
Solution to Task 1

Name:
Address1:
address2$
Address3:
Address4:
Phone:
PRINT name$
PRINT address1$ + ", " + address2$ + ", " + address3$ + ", " + address4$
PRINT telephone$
Thus you have learned the statements "IF", "ELSE", "INPUT" and briefly "THEN".

Chapter 4
When you write a program in QBasic, the situation on the screen is one of the
most important features of your program. You must clear the program screen
to read and not have useless remnants of other programs on the screen. In the previous program
we have used a black screen with the text written in white as a base. In the following
Let's change all this so that your program stands out. Write the following
exactly and press 'F5' to run it:
SCREEN 12
COLOR 10
We are using green text on a black background
PRINT "The screen resolution is 640*480 with 256 Colors!"
FOR i = 1 TO 60000
NEXT i
SCREEN 9
COLOR 10, 8
We are using green text on a blue background
PRINT "The screen resolution is 640*350 with 64 colors!"
In the previous code example, we introduced two new commands. These commands
"SCREEN" and "COLOR". Let's take a look at the statement "SCREEN".
QBasic can work with different screen modes. When I say screen modes
I am talking about different sizes and colors. Here is a small list of different ones.
modes in QBasic.
SCREEN 0: Text mode only
SCREEN 1: 320 * 200 graphics
SCREEN 2: 640 * 200 graphics
SCREEN 4: 640 * 480 graphics
SCREEN 7: 320 * 200 graphics
SCREEN 8: 640 * 200 graphics
SCREEN 9: 640 * 350 graphics
SCREEN 10: 640 * 350 graphics, onlymonitormonochrome
SCREEN 11: 640 * 480 graphics
SCREEN 12: 640 * 480 graphics
SCREEN 13: 320 * 200 graphics
These screen modes are useful for all QBasic users. The modes of
Screens are very important in programs. For example, if you want to draw lines,
cubes and circles on the screen with some kind of graphic method you must use a mode of
screen that allows you to draw graphs. Many of the previous screen modes
They work with all recent PCs. A few years ago, it was not possible to work with some.
from them because you would surely need aadaptergraphics. That is why many PCs
Recent ones are designed to work with the old screen modes such as
CGA, EGA, VGA and now SVGA.
Very well, so you can benefit from different screen modes. Well, some of the
previous screen modes allow you to use more colors than the others. In the first
In this tutorial program, we had a program that could use two screen modes. One
it had a maximum of 256 colors and the other only allowed you to use 64 colors. When
write a program think about the colors that you will really need and the resolution
that the screen should have. 640*480 is a very good resolution that can be found in the
screen mode 11. Try changing the screen modes of the first program of this
tutorial and see what you get. Remember that some of these screen modes could
not work, so don't worry if this happens. For more information on the modes of
Screen type "SCREEN" and press F1.
Usingscreensin color.
Therefore, the first program used two different screen modes and some
different color ranges. Let's take a look at the "COLOR" command.
In principle, the color sentence allows the user to select specific colors to
the background and letters of the program. This also allows selecting the color of the border. The
border is the area that frames the screen. The statement 'COLOR' can occupy any
position in the program among the commands mentioned so far.
At the end of this chapter, there is a brief description of the commands learned so far.
To put this command into practice, we are going to write this interesting program. I am not going to
to tell you that it still works, so for now, just write it and run it by pressing 'F5'.
SCREEN 12
CLS
FOR i = 1 TO 15
COLOR i
PRINT "Checking Checking 1.2.3"
NEXT i
So come on, it's a simple program. Can you guess what it does with what we have
Learned so far? Good, let's go through command by command and see how it works.
SCREEN 12
Now we are going to select Screen Mode 12. This allows us to use 16 colors.
chosen from a palette of 256 colors. You can find more information in the Help Menu of
QBasic.
CLS
We know that this command will clear the screen of leftover letters that have remained in
the screen from before.
FOR i = 1 TO 15
We are giving the command 'COLOR' an initial value 'i' of one that will be incremented.
in 1 after.
COLOR
We are giving the statement "COLOR" an initial value of 1 that is given by the "i" of
command "FOR".
PRINT "Checking Checking 1.2.3"
Now we are writing words that will appear on the screen.
NEXT i
When the program runs, it will repeat the 'FOR' and 'NEXT' loop until the variable 'i'
reaches 15, a value defined in the previous "FOR" statement.
Thus, when the program runs, it will show 15 parts of text, each with a different color.
Notice that only 15 parts of text will appear when, in reality, we should be
showing all the possible ones which are 16. For this to happen we should change the
command FOR this:
FOR i = 0 TO 15
Notice how we have changed the initial value of 'i' to zero. It's because the value of
Color 0 is black. If we change it to 1 TO 16 it will give us an error because the statement
"COLOR" in Screen Mode 12 can only handle 16 colors at once. Let's do it.
a small game with the 'NEXT' and 'FOR' loop.
This is how the "COLOR" command works.
COLOR (foreground) (,(background) (,border)) --- Screen mode 0
COLOR (background) (palette)
COLOR (foreground) (,background) --- Screen modes 7-10
As you can see in the first program of this tutorial, we can define the color.
in the background but also on the surface (letters, numbers...). The command 'COLOR' works
in a quite different way than other Screen Modes. To see when it works and
when not, take a look under the help row 'COLOR'.
It will be an ideal reference to use when selecting colors for various parts of your
programs. The statement "COLOR" is explained in more detail in a couple of
tutorials from now on. Ah! The color command is not used by all programming languages.
Basic programming. When you need to check this, take a look at the guide.
user.
Task 1.2
Write a program with many colors in which you ask the user for their name and
age. When it appears on the screen, make sure that what is written is in a different color.
Hint: use screen mode 12. The answer is at the end of this tutorial.
Review of the learned programs
PRINT
FOR
NEXT
CLS
SCREEN
COLOR INPUT
STEP
IF
ELSE
THEN
ENDIF

Many of these commands will be mentioned in more detail in the tutorial later on,
but you are not doing it wrong. The most important thing is that you experiment with what you have
you will learn and you will understand it effortlessly.

Task 1.2 Solution


SCREEN 12
CLS
COLOR 3
Name:
COLOR 5
Age
COLOR 9
PRINT "Hello" ; name$
COLOR 10
PRINT 'Your age is' ; age
The code is good, simple, and gives a good result. Your program should be similar to this,
but the colors will probably be different from mine. Try to write my program and
observe if you get the same result as me. If it is similar to mine, very good, but if not, then no.
Don't worry!
So now you have learned a little about screen modes and colors in QBasic,
remember that these can be placed anywhere in the program in QBasic.
Remember that you don't need to use the 'CLS' command all the time, the screen should
automatically clear every time a new screen mode is selected.
Continue and very well.

In the next chapter we will learn about on-screen order and we will immerse you in a
little in the use of commands like 'LOCATE' and 'DATE$'. You will also learn how to make
that the computer responds with a warning signal like 'BEEP'. The statement
"IF" and "ELSE" will be mentioned and used in more detail later.
Chapter 5
When you write a text in QBasic, you must place the text in a good location on the screen. In
the previous examples seen so far the text was written in the upper corner
left of the screen. We can use a specific command to position the text in
any specific place on the screen; this command is called 'LOCATE'. Below there is a
simple synthesis:
LOCATE row, column, cursor
row
The number of a row on the screen; row is a numerical expression and must be a value.
whole. If the row number is not specified, then we will not be able to change the row.

column
The column number on the screen; column is a numerical expression and must be
specified in order to change its location.
cursor
A value indicates whether the cursor is visible or not. The value 0 (zero) indicates that the cursor is
off, the value 1 indicates that the cursor is on.
Let's continue, let me just briefly explain, let me just explain what an integer is. An
integer is a number between -32,768 and 32,767. This means that the value of 21,566 is a
integer, and a value of 2342992 is a long integer. Please check in the type section of
file more details about this. But from now on the integer variables will beusedhow
the example below in this tutorial.
599
Ok, this is a small code that will show you how the 'LOCATE' command works.
SCREEN 12
CLS
LOCATE 5, 10
PRINT "Hello world we are at 5, 10"
LOCATE 10, 1
PRINT "Hello world we are in 10, 15"
If you write this program and press 'F5' you will find the words 'Hello World'
we are at y,x" located in different places on the screen. The y,x in the
The previous phrase is anything that was originally included in the program. Try
change the statement "LOCATE" in the previous program to something different. Press "F5"
to run it. This is a chapter that will teach you how row and column coordinates work.
column.
--------column----------
The first 'LOCATE' statement in the program we just translated moves the
next statement 'PRINT' 5 rows down and shifted 10 characters. What you must
to remember is that any 'PRINT' statement followed by 'LOCATE' will only make the
operation followed by sentence.
The locate statement does not need to have a value representing the row and the column.
time. For example, we can have:
LOCATE 5
This simply moves the 'PRINT' statement 5 columns down.
screen. We can also have this:
LOCATE ,5
This will simply move the 'PRINT' statements 5 columns, shifting the screen.
that you won't have to specify the arguments all the time again. The arguments
This is information that is necessary for the sentence to work. For example:
LOCATE argument1, argument2
In this case, argument1 corresponds to the row and argument2 to the column. Play with it.
command "LOCATE" and see what you get.
Dreaming a BEEP!!!
Within QBasic, you can force your computer with a simplesoundknowing that it is a
warning or showing information. This command is called 'BEEP'. It is very easy to
use. Try entering the following in line 1 of your program and pressing 'F5' to
run it.
BEEP
Simple! It will generate a sound at a high tone. You can place the command 'BEEP' in
any place within the program. In the following program, the command "BEEP" is
used together with other parts you have learned so far. Write the following and press
Press F5 to run it. Remember to delete the program you wrote earlier.
SCREEN 12
CLS
Fred
LOCATE 5, 5
Please enter the password.
IF password$ = check$ THEN
CLS
LOCATE 5, 5
Access Granted
ELSE
CLS
LOCATE 5, 5
PRINT "!!! Access Denied!!!"
BEEP
ENDIF
The previous program may seem complicated at first, but it is actually very easy.
First, the program changes the screen to mode 12. Then it will sweep the screen.
to clean up the remnants of another previous program. The following statement will make it so that the
variable "comprobación$" should have the word "fred" stored inside. We will prepare
the screen to ask for the password. Once the password has been written, it
will store the information inside the variable called 'password$'. The following
The 'IF' statement checks that the variable 'password$' is the same as the one contained
within the variable 'comprobación$', in which case it will write 'Access granted'. If the
password is incorrect will jump to the "ELSE" statement and write "!!! Password
Incorrect!!!". A loud beep will also come from the speaker.
Let's make a simple program to input and check. You can also check.
correct numbers. This is a program that allows you to do it:
SCREEN 12
CLS
50
LOCATE 5, 5
IF number% >= checknumber% THEN
CLS
LOCATE 5, 5
PRINT "¡¡¡ 50 or greater

ELSE
CLS
LOCATE 5, 5
PRINT '49 or less'
ENDIF
Again, another simple program that checks if an integer is greater or less than
another stored in the variable 'checknumber%'. Initially, the variable
"checknumber%" is set to 50, which is checked by the "IF" statements and
"ELSE", like the password of the previous program. The "IF" statement is checking
to see if the value you have written is greater than or equal to the value stored in the variable
called "checknumber". For example, if you type 49 you will be able to obtain a result.
of "49 or less" written on the screen. However, if you type 50 or more in your program
you will get a result of "!!!50 or greater!!!" displayed on the screen.
Date and time
QBasic allows the user to find out the date and time system they have.
To do this you can use two simple commands like 'DATE$' and 'TIME$'. You too.
you can display the date and time within a QBasic program, but this will be explained
much later.
Write the following program and press 'F5' to run it. Remember to clear the screen.
from the previous program that you have in memory.
CLS
PRINT "Today's date is:", DATE$
PRINT "The time is:", TIME$
Press 'F5' to run it and you will see the date and time on your screen. There's not much more.
What I can tell you about the statements 'DATE$' and 'TIME$' until later. You could
try writing a program that displays the date and time at the top of the
screen and ask the user for a password. You are going to learn 4 more commands now
from QBasic. Remember that you can get a help menu by pressing "F1" on some
screen command. In the next chapter, I will show you some basic technical loops.
that continue the operation until something happens like the user presses a key or
something caused the loop to stop.
Chapter 6
Within QBasic, you can create a program that does something in one period of time or another.
or until the user makes the computer do something else. In this part of the tutorial I am going to
show you a couple of techniques that allow you to do this. We have already seen a loop of this
like 'FOR' and 'NEXT'. This loop allows you to operate along a completely
different from those seen in previous examples.
Now I am going to introduce you to the 'DO' and 'LOOP' loop commands. This is a program
basic in which the loop repeats over and over until the user stops it by pressing
the CTRL & DEL keys. Write it and run it by pressing "F5".
CLS
DO
PRINT "HELLO WORLD"
LOOP
For this example, you can see the words 'HELLO WORLD' always written below.
screen. It may not be very clear at this moment but the program is really
doing something. Many times you will notice it if the cursor is at the bottom of the screen.
As you can see, the commands 'LOOP' and 'DO' are quite effective even in this
Wait a moment. Let's separate the commands to see what they really do.
DO
This command prepares the beginning of the loop that you executed earlier among the commands
'DO' and 'LOOP':
LOOP
The command 'LOOP' is only executed if 'DO' is present, as in the examples.
previous ones. All the time the "DO" command of your program is waiting to find a
command 'LOOP'.
There is more about 'LOOP' and 'DO' that are always repeating. Let's insert it into the
commands to extract some kind of result from our computer. Now you
I am going to teach a command called 'WHILE'. This command is quite effective but
sometimes it is complicated to understand. Here is a simple example that you need to copy and
to run.
CLS
DO WHILE I < 10
I=I+1
PRINT I
LOOP
The program may seem complicated but it really isn't. When you run the program
simplemente escribe de 1 a 10 uno debajo de otro en la pantalla. El programa está
really checking to see if the variable I=10 and if this causes the program to end
the loop.
In principle, we start the loop and say that while I is not 10, write the value.
on the screen. In this case, the program will go back to the beginning and check the variable for
see if it has reached the value 10. It's simple, but make sure it reaches it. Try to change the
Set the first line to a value of 20 and see what happens. The line should now read:
DO WHILE I < 20
It may seem strange to you, but if you run the program now, it will write from 1 to 20 one below the other.
from another on the screen. To take the next step, you can do it by choosing where to put the
command 'WHILE'. Before giving you another programming example, let me explain
briefly a little more about the command 'WHILE'.

WHILE
This is just a bit of what I will tell you about the 'WHILE' command because it is better.
explained alongside another command called 'WEND'. The 'WHILE' command allows
You check a condition of something when giving the result. As we explained in an example.
previous is classified as a checking command.
The "WHILE" command can be placed elsewhere in the programming example that you ...
of before and you will get the same result. This is a code that does the same as before.
CLS
DO
I=I+1
PRINT I
LOOP WHILE I < 10
So as you can see, the program above will give you the same result if you press 'F5'
and you run it. So the 'DO' and 'LOOP' loops will give you the same kind of flexibility when
decide where to place the code in your program. This allows for greater control over results.
and designs in the program, remember that. Loops are explained and used in more detail in
the tutorials later.
Understanding the user introduction
The introduction to the user is a vital part of any program. For example, I already talked
about how to handle variables and strings. I also talked about how to use the 'PRINT' command in
small detail. We are going to put all our previous knowledge into practice in a
full program. Don't worry if the program seems a bit complicated and laborious,
It's easy, just take it slowly and everything will become clear.

CLS
PASSWORD
DO
What is your password:
IF Password$ = Password$ THEN Enter=1
LOOP WHILE Enter <> 1
CLS
PRINT "You have accessed!"
After writing the program and pressing 'F5', the screen will clear and ask you for the
password. If you enter anything other than 'APPLE' it will ask you for it over and over again
until you enter it correctly.
Remember that the program is checking whether you write it in uppercase or lowercase.
This program will only accept it if you write the word apple as 'MANZANA' not
like "apple". In later tutorials, we will explain how to change words that you are
from lowercase to uppercase and vice versa without pressing the CAPS LOCK or SHIFT keys.
A quick look at WHILE
Below you have the main code for a simple program where I will show you the
command 'WHILE'. Write the following in QBasic and press 'F5' to run it.
DO
I=I+1
PRINT I
LOOP WHILE I <> 1000
The previous program makes it easier to understand the commands 'DO, LOOP,'
WHILE. If you write the program and run it, it will count from 1 to 1000. This is easy to
understand if you take a look at how it is made. First I will show you how the computer
start the loop using the command 'DO'. Then we are asking the computer to
start with a variable called 'I'. The following statement will write the value of i in the
screen, in which case the initial value must be 0. The computer has started the variable with
a value 0. The next part of the program will check to see if the value "I" of the
variable is 1000, if not it will jump to the beginning of the loop, which in this case is the statement
Then the computer will tell you to add variable 1 to the previous value of variable "I".
in this case it should do "I" = 1 then 2 then 3 etc.
With the previous program, you can change "I = I + 1" to "I = I + 2". This will cause the
computer adds 2 to the variable 'I' until the computer reaches the final statement which is
LOOP WHILE to check if 'I' has reached the value 1000.
CLS
FRED
DO
What is your name?:
IF name$ = oldName$ THEN access = 1
LOOP WHILE access <> 1
Above we have used a similar type of program again, like the program of
password, which allowed the user to verify the input information of a
variable with respect to another. The previous program asks for a name. If the information you have
written is the same as that contained in the variable "Nombreantiguo$" the program
It will continue, if it's not the same it will leave.

Have a small practice using variables with 'DO LOOP' classes and see what
you can obtain. In this chapter you have learned the basic control loops introducing
information from the keyboard. In the next chapter I will show you a program menu
simple which uses new commands like 'SELECT', 'CASE' and
I will warn you that now, in the next chapter, you might get a little lost, but not
it's okay this is just a part of the learning process. If you get lost don't give up, just
read the chapter again until you see everything clearly. I will write a
program in which we will use almost all the commands seen so far. This program
it will be ideal as a reference and to give you only a general sense of how to put the
commands together and make good programs.
Chapter 7
In this chapter we are going to write a simple program menu that can be used in
many of your own programs by simply cutting it and inserting it into it. You may need
change a couple of things, such as the text displayed on screen. Before
continue here you have the code you should write.
CLS
SCREEN 12
COLOR 12
LOCATE 3, 35
User Main Menu
LOCATE 4, 35
PRINT "------------------------------------"
COLOR 10
LOCATE 10, 35
PRINT "1. Sum"
LOCATE 12, 35
PRINT "2. Subtraction"

LOCATE 14, 35
PRINT "3. Division"
LOCATE 16, 35
PRINT "4. Multiplication"
COLOR 6
LOCATE 25, 32
Enter a Number (1-4):
SELECT CASE number
CASE 1
You have selected the number 1
CASE 2
You have selected the number 2
CASE 3
You have selected the number 3
CASE 4
You have selected the number 4
CASE ELSE
PRINT "The number you selected was not between 1 and 4"
END SELECT
Oh yes! Things are starting to get exciting. Take a minute and review yourself.
the code. As you can see, it contains quite a few things that you have learned so far.
The only part you haven't learned is this:
SELECT CASE number
CASE 1
You have selected the number 1
CASE 2
You have selected the number 2
CASE 3
You have selected the number 3
CASE 4
You have selected the number 4
CASE ELSE (ANY CASE)
The number you have selected was not between 1 and 4)
END SELECT
Trust me, this part is very easy. First, let's take a brief look at the meaning.
from the statement 'SELECT CASE'.

The statement 'SELECT CASE' allows you to execute one of many statements of a
block. For example, a statement of a block is this:
CASE 2
You have selected the number 2
In the previous main program, we have 5 statements in a block that are 'CASE 1',
CASE 2, CASE 3, CASE 4 and CASE ELSE. Within the 'SELECT CASE' part of
previous program, the first line says:
SELECT CASE number
This command prompts to execute a statement of a block in which it is contained.
the variable called 'number'. If you take a look at this part of the program you will see that
we have a command 'INPUT' that is asking the user for a number. The command
SELECT CASE number will use any statement from the block that is asked. So if
I write "2", within the program, the statement "SELECT CASE" will use "CASE 2", and
then it will execute anything contained in the program after this and before
that the phrase 'CASE 3' appears.
The command 'CASE ELSE' is used only in case the user has not provided a
number from 0 to 4. If the command "SELECT CASE number" does not find a case to which
referring what the user has written will refer to the 'CASE ELSE' to give an error message
or anything the user asks.
Advancing with 'SELECT CASE'
We are going to evolve something that is a bit more advanced than the first program of
SELECT CASE. In the following program, we are going to use a command called
TO. You will be able to see what happens next, but first let's write the program that
we show below and run it by pressing "F5".
CLS
SCREEN 12
COLOR 12
LOCATE 3, 35
Main User Menu
LOCATE 4, 35
PRINT "-----------------------------------"
COLOR 10
LOCATE 10, 35
1-2. Sum
LOCATE 12, 35
PRINT "3-4. Subtraction"
LOCATE 14, 35
5-6. Division
LOCATE 16, 35
COLOR 6
LOCATE 25, 32
Integer Number (1-8):
SELECT CASE number
CASE 1 TO 2
You have selected number 1 or 2
CASE 3 TO 4
You have selected the number 3 or 4
CASE 5 TO 6
CASE 7 TO 8
You have selected the number 7 or 8
CASE ELSE
PRINT "The chosen number is not between 1 and 8"

END SELECT
At first glance, it may not seem different from the first program, but you should give yourself
note that we insert the statement 'TO' within 'SELECT CASE', 'END SELECT'
is part of the program. The 'TO' statement allows you to introduce a range of something within
from the statement 'SELECT CASE' and even within the 'NEXT' and 'FOR' loops. Remember
you have used 'TO' within the 'NEXT' and 'FOR' statements in previous chapters.
The current program will allow you to respond not only to one, but to two numbers.
selected within the range. So to execute the fourth 'CASE' statement
you would need to write 7 or 8 in the program when it is running.
The command below is common sense and can be understood if you think about what it is.
doing the program. In the next chapter, I will take you on a journey in which
You will handle numbers within the program. I have briefly explained the usage in tutorials.
previous ones. Let's take a closer look at this a little more in depth.
Chapter 8
Using numbers in QBasic is probably one of the most important things you are going to
you need to know when writing a program in QBasic. For example, if you are writing
a game you will probably need to keep track of the players' scores and some
bonuses that I obtain. A simple example of adding a bonus to the score is probably
this
bono = 100
puntuación = 500
score = score - bonus
PRINT score
The previous program shows how QBasic stores the value 100 in the variable 'bonus' and 500.
in the variable 'score'. The next line adds both 'score' and 'bonus' together and
assigns a new value to the variable 'score'. This example has just shown us a
path to sum variable values and equal them to a new result.
Could you do things a little more complicated than this? What would you do if you had 4 variables and
would you like to sum them together? You could do something like the following:

bono = 100
puntuación = 500
levelbonus = 200
luckscore = 100
score = score - bonus - levelbonus - luckscore
PRINT punctuation
Remember that you can add numerical variables in any way, but only while being
sure that you are not just concatenating strings together yet. The following examples
they show other ways to handle numbers in some program:
age = 21
new = 3
age = age - new
PRINT age
The previous program is using the operator "-" to do the required work. In
our language this is called subtraction or subtraction. The following program allows you to see
how working with numbers can affect the final result of a program.

"manzanas = 2"
"ciruelas = 5"
"naranjas = 3"
LOCATE 2, 1
PRINT " In the warehouse we have:" ; apples; " Apples, " ; plums;
Plums, oranges; "Oranges"
LOCATE 6, 8
Now we have sold two plums and one orange
plums = plums - 2
naranjas - 1
LOCATE 10, 1
PRINT "In the warehouse we have:" ; apples; " Apples," ; plums;
"Ciruelas," ; naranjas; "Naranjas"
I have explained in previous tutorials how many of these operators are used.
= is equal to

is not equal to

> is greater than

< is less than

is greater than or equal to

is less than or equal to

During some long program, you will probably use one or more of the above.
the previous operators can perform operations on different numbers. For example
you can check if something is greater than or equal to. I don't know if you know this, but in the United Kingdom
there is a television show called 'Play Your Cards Right'. In the game of the show
You have to say if the following letter is greater than the previous one. For example, let's suppose that
the first card is a 6. The contestant must say if the card is higher or lower than the
next. If he calls higher and comes out with a 7 up to an ace, he will have won this time. If it comes out 5
the smaller one and the player who appears on screen has said the larger one turns out to lose. The
games like this would be very easy to write, and they use quite a few operators
previous ones to simulate them. The one below is a simple guessing game.

RANDOMIZE TIMER
number = INT(RND * 10)
DO
Guess the number:
IF guess > number THEN PRINT "It is High"
IF guess < number THEN PRINT 'It's Low'
LOOP UNTIL guess = number
PRINT "You have won!"
Oh yes, now I'm going to make it more difficult for you. I'm going to introduce you to two new commands. The
first is 'RANDOMIZE TIMER' and the other is 'INT(RND)'. Don't worry if it seems like a
a little complicated because it is really very simple. Let me explain the two commands.
"RANDOMIZE TIMER" is a way to actually generate random numbers.
"RANDOMIZE" is a number that is set without a formula, without a command, it is simply set.
and allows mixing the numbers when you enter the command "TIMER"
mix it much more. You probably won’t use "RANDOMIZE" unless you are
writing a number game or something of that nature.
However, "INT(RND)" is probably something you will use a lot. The "INT" part explains
what is looking for the integer part. I have already explained integers to you before in this tutorial. For
Explain it a little more, an integer is a whole number and not with decimals like 4.3454.
Integer variables are good sometimes because they ensure you get an integer.
omit the part "INT" (part of the previous program, you will find that the command "RND *
10" will generate some decimal value between 1 and 10, like 5.3. That's where the next one takes me.
point. The part "(RND * 10)" will generate a random number between 0 and 10. If I put (RND
You will get a random number between 0 and 20 generated by you. Remember that the number
The randomly generated number will be assigned to the variable 'number' in the previous program.

The routine verification of a number is this part.


IF guess > number THEN PRINT "It's High"
IF guess < number THEN PRINT "It is Low"
The first line checks if the number guessed by the user is greater than the
randomly generated number. If it is too high then the program will write 'It's High'.
the next line above is the opposite. It is checking to see if the guessed number
by the user is less than the randomly selected number.
In the following pair of tutorials, you will see many operators in use, especially some.
that we haven't explained yet. In the next tutorial, I will teach you some commands
graphics. You can also embellish your own programs to attract the user.
Chapter 9
Welcome to the next QBasic tutorial. In this tutorial, I will teach you some
basic techniques of programming graphics. The program's presentation is graphical to attract
to the user. Most programs now contain some graphic from
simply underline the text until all the windows are filled with colors and effects. You can
create anything you like in QBasic with a simple graphic step game.
On any computer where you program graphics in all languages, you must think a little.
First, you need to know where you are going to place the graphs on the screen. That is perhaps what
you have thought about it so far but you haven't succeeded. If you take a close look at the screen of the
on the computer or watching television, you will see small square pixels forming the image.
screen. Each of these pixels represents a simple location on the monitor. For
put a dot very in the upper left corner of your screen you need a
coordinate. A coordinate is a specific location number. Don't leave me
to confuse you, but for example if you want to have a point in the middle of the screen
You would need to have a value to place the point. Look at the diagram below:
To view the chart, select the 'Download' option from the top menu
The diagram above is displayed on the monitor or television screen.
If we move up the screen, we will do so along the y-axis. If we
we move along the bottom of the screen we will do it along the x axis. Now yes
If you look closely, you will see a point near the left side of the screen searched.
We want to place a point there; we would need to know the value of the coordinate on the screen.
We could easily do this in QBasic. Inside QBasic the corner
the top left of the screen is coordinate 0,0. This coordinate value
represent the x, y axes as explained above. Now we are going to create the point on the screen
searched, we will need some approximate coordinates of 100,100. These coordinates
They will move the point 100 pixels to the left on the screen and 100 from top to bottom.
If we want to move the point further down, we will need an approximate coordinate.
from 100,300. Let me show you a simple program that does what I just explained.
Up. Write it and press 'F5' to run it.
SCREEN 12
PSET (100,100),2
The previous program is very simple. First, we are changing the text screen for
omission to the graphic screen. The following line is our graphic command which is called
"PSET", which I believe means point code. In the previous line of code, we asked
to have a small dot (spot) drawn on the screen at coordinates 100,100.
100 pixels to the left and 100 up. The next number is the color of the point
situated on the screen. In the previous program, we used a simple green color. If
If you do not enter the color number, it will default to white.
The command 'PSET' can be used in many other screen modes as well.
graphic commands. You will need to try testing different screen modes, but
remember that the resolution may change. Resolution is what is known as different
sizes like the standard window mode that could be 800*600 pixels. The
the screen resolution we used in the previous example is 640 pixels wide and 480
downwards. So you can place a point (dot) at any value within these.
Now 'PSET' is a very simple command. Let me introduce you now to something a little
different. The lines are very popular and used quite often in the programs.
For example, a line can create shapes like a rectangle or a square.
The "LINE" command works a bit differently than the "STEP" command in the way it draws.
shapes on the screen. Below is a simple example of how you should write it
SCREEN 12
LINE (100,100)-(200,100)
The previous program will draw a line from one position to another. First, we are asking
to be put back in a graphical mode. Then we will draw the line of a
position on the screen to another. For example, we are drawing a line from 100 along
and 100 down to 200 along and 100 down. It's quite simple if you think about it this way.
way. Write the following in the program.
SCREEN 12
LINE (100,100)-(200,100)
LINE (200,100)-(200,200)
The program above is virtually the same as the previous one, but the following command
"LINE" is drawing a straight line down on the screen from the first command.
LINE
Task 3
Look to see if you can draw a simple program that draws a simple cube on the screen.
using the parts you just learned before. Tip: remember that you only need
information that is included in this tutorial to write the program. The program has
It is written at the end of the program to see if you did it right.

Even more graphics


To extend your knowledge of QBasic to the graphical features, I will immerse you in
a command that expands your previously learned knowledge. The following graphic command
is called "CIRCLE".
"CIRCLE" works similarly to the "STEP" command except that it requires a significant value.
at the end of the command. Please, write it down and try it in the following program.

SCREEN 12
CIRCLE
This program will draw a circle on your screen. First, we will simply ...
We will switch to graphic screen mode and then we will set a location of
coordinates on your screen. The next part of the command sets the radius of the circle. Before
we use a small radius of only 50 but it can be much larger. The command
"CIRCLE" places the circle in a different way on your screen. If you don't want to get confused, don't
read the following paragraph.

When you draw a circle on the computer screen, you are not actually drawing it on
100,100. As you will see if you change the line above to:

SCREEN 12
CIRCLE (0,100), 50
When you run this program, you will see that only half of the circle has been drawn. The value
The coordinate of the circle is the center of the circle. This seems complicated but is true.
You will notice that if you use the command 'CIRCLE' a lot, you will learn how it works.

Extending Lines to Cubes


The lines are very good in a program; but what do you do if you are drawing cubes? Well,
In principle, the command 'LINE' can do all the work for you. Write the following
program and then test it.
SCREEN 12
LINE (100,100)-(200,200), 4, B
Oh, yes. We have created a simple box. Before moving on, I would like to say that the
the previous program is not a solution to the task I gave you a little while ago. The solution to the task
It is beneficial a little later.
The previous program will draw a simple cube with just one line of code. The cube is
created in a red color and does everything possible from a simple code change. The
The command 'LINE' now has a 'B' at the end which stands for 'BOX'. To proceed a
more than this one tries to change 'B' to 'BF'. Run the program, and yes, you have created now
a simple colored cube.
So as you can see, many different effects can be created with just a few
few graphic commands. You will have to experiment with the graphic commands to do
things perfectly well. You will find that the QBasic coordinate system
It will also take a little time to understand it. After a while of time
You will really be able to add color to your programs.

Solution to Task 3
To draw a simple cube using the fourth command 'LINE' it is as follows. Give it a try!
SCREEN 12
LINE (100, 100)-(200, 100)
LINE (200, 100)-(200, 200)
LINE (200, 200)-(100, 200)
LINE (100, 200)-(100, 100)
The command has shown you a simple beneficial graphical command game in QBasic.
Remember that we are only superficially addressing what QBasic can do. In
In later tutorials, I will teach you more graphic commands and we will add what you have.
learned so far. In the following tutorial, I will guide you through paths using
procedures with which you can improve your programming code. With this you will be able to
make your programs look better and easier to understand.
Chapter 10
Welcome to tutorial 10. Before explaining what a procedure is, let me immerse you in it.
something called Subroutine. Subroutines are sections of code that are left for access
easy and basically to place the code well in QBasic so that it is easy to read and
write. First let me explain the two commands that will do all the work in QBasic.
Gosub
A 'GOSUB' command is a control statement that extends to another part of
program. In other words, it connects a part of your basic program to another part of the program.
fulfilling some kind of action. With the command "GOSUB" you will need a label
specific. A label is a part that will be chained with 'GOSUB', now I will show you
an example of programming within a moment.
Return
The command 'RETURN' allows you to jump back or to a label. For example, you can have
"GOSUB" in a part of your program to be able to go back or to continue later.
The method is different and all types of programs can be built. Here is a
simple programming command using these two commands.
CLS
A simple GOSUB program
PRINT "-------------------------------------"
In principle, we are going to jump to another program and write
something
GOSUB hello
Finally, we have returned safely
END
hello
PRINT 'Hello, we are jumping'
PRINT "We are going to return and finish the program"

RETURN
As you can see, everything seems quite simple. In principle, we are doing the same.
things you have learned in previous tutorials. The important part is the statement
GOSUB hello. Inside this line we are counting where the program goes and
finding the tag called 'hello'. Thus, the program searches the rest of the program until
That the label is similar, which is the label that contains 'hello'. I continue within the
label "hello", here is the code that executes it. The code simply writes a simple text
on the screen. Once it has done this, it jumps back to where it comes from, which is the
statement "GOSUB". The computer remembers all this information once the statement
"GOSUB" has been reached. To put this theory into an example, we will write another one.
short program that will jump to two different places in the program. Write the following and
press "F5" to run it.
CLS
Print 'Another simple GOSUB program'
PRINT "---------------------------------------"
In principle, we are going to jump to the First
Label
GOSUB label1
PRINT "We have finished, we return safely to the
label1
PRINT
PRINT "Later we are going to jump to the Second Label"
GOSUB label2
We have finished, we will return safely to the
label2
END
label1
PRINT "Hello I jumped to Label1"
PRINT "We are going to go back and jump again."

RETURN
label2
PRINT "Hello, let's jump to Label2"
PRINT "we are going to return and finish the program."

RETURN
As you can see, it is quite common to create subroutines in some QBasic programs.
Let me just advise you on a couple of things:
1. Make sure not to use reserved words like 'PRINT', 'STOP',
"LOCATE", etc for your labels.
2. Make sure to call your tags by some meaningful names that
have to do with what the subroutine is doing. For example, you could
call 'points': to the scoring subroutine of a game.
3. Subroutine labels always have a colon at the end. If you forget
it will give you an error.
Programming an example of selection 1
Here are a couple of examples of programs that you should want to try. I have used many.
commands from previous tutorials to show how programs are chained together.
Program password
CL
Program 2 password
PRINT "-----------------------------------"
PRINT
Please enter the password:
IF password$ = "apple" THEN GOSUB introduce ELSE GOSUB error
END
introduce:

PRINT "Welcome, you have entered correctly"


RETURN
error:
CLS
PRINT "I'm sorry, you have entered the wrong password"
The previous one is a simple program that asks for a password. You need to enter
apple
Program menu
CLS
PRINT "Program Menu"
PRINT "--------------------------"
PRINT
PRINT "1. Open File"
2. Save File
PRINT "3. Save Program"
PRINT
return
LOCATE 8, 10
Selection: number
IF number = 1 THEN GOSUB option1
IF number = 2 THEN GOSUB option2
IF number = 3 THEN GOSUB option3 ELSE GOSUB return
option1:
LOCATE 10, 10
Option 1 Selected
RETURN
option2:
LOCATE 10, 10
Option 2 Selected
RETURN
option3:
LOCATE 10, 10
You have completed the program
END
The previous program allows you to select 1, 2, or 3. Depending on the key that is
pressing it will link it to a relevant tag but will be left to remind you of something.
Many users use this command to remember what the line actually does.
Below you have a small example.
CLS
REM "Below is a simple Writing Program"
PRINT "HELLO"
This program is really basic but it still teaches us this point. If what you want
It really is attention-grabbing, you can use an accent as a rem. Here it is like.

CLS
PRINT "HELLO" This command will write the word "HELLO"
Anything that appears after the symbol ´ will not be used during the program.
You should try to place the symbol ´ on its own line. Below we have another simple program.
for example.
CLS
We are printing 'HELLO'
PRINT "HELLO"
Then we can end the program with the command "END"
END
As you can see, "REM" and the symbol ´ is a command you should know. It is a
excellent way to comment your programming code so that other users can
Understand. Now you should find examples of the use of it in some of my programs.
command 'REM' and the symbol ´. Try to use it wherever possible, you don't know how easy it is
forget what each part of the program does
Introduction to Subprocedures
I must warn you that some things may seem a little complicated when you start.
to read the following part of the tutorial. If you get lost, please try reading it again until
that it makes sense. However, I will try to make things as easy to understand as possible.
may it be possible.
The Subprocedure works a bit differently than the subroutines, as
we stated above. With a Subprocedure you can work on a particular part of
program without the rest intervening. For example, you could write the menu of a program
and putting the menu details of each program separated outside your program, but
to be able to access it very easily. Let me give you another perspective on this.

For example, if you have written a computer game that allows you to kill aliens. Each time
when an alien has been shot you increase your score by 50. Could you create a
Subprocedure to do this without having to write the code over and over again. Before
let me teach you how to write a complete Subprocedure, let me show you how to create one
inside the QBasic Editor.
How to create a Subprocedure
Make sure you have started with a clean screen, if you don't have it, please
select "NEW" by expanding it from the "FILE" menu. As soon as it's done
We are ready to create a New Subprocedure. Type 'SUB hello'. Once
that you have done it and pressed return, you must now enter the Subprocedure in mode
Edition. This is the developed part below.
Microsoft QuickBasic user
The previous theory works well with QuickBasic, but you could also do this. Inside
from the QBasic editor move your mouse over the 'EDIT' dropdown menu and click on
"NEW SUB". If you don't have a mouse, press ALT+E simultaneously. This will drop down the menu.
Edit
option "NEW SUB". Now press return.
Once you have done what I explained above, a window should pop up asking
by the name of the new New Sub. Let's call it 'hello'. Write the word 'hello' in the box
name: If you click now (mouse click) or press return on the 'OK' box,
your screen area will change and present something that looks a little different. The
the presentation should be like this.

To view the graph, select the "Download" option from the top menu.
Your menu of options may be different from mine. This is because I am using the version
commercial of QBasic called QuickBasic. Don't worry about this because everything you
I explain it will work the same.

As you can see in the screen shown above, your workspace should only have lines.
written. There are 'SUB hello' followed in the next line by 'END SUB'. Between these two
lines you should put all the code you want to make. Write the following in the window of
Editor.
SUB hello
PRINT "Hello, you have accessed a subprocess."
END SUB
Once this is done, you are now ready to continue writing the program in the
the way you have done it in previous tutorials. Basically, you can forget how you have
written so far. To return to the screen that has the program, you need to select
"VIEW" in the dropdown menu. You can do this as before with "EDIT", but instead of
click on 'EDIT' do it on 'VIEW' with your mouse. Again you can press ALT
"V" to do it. Once the dropdown menu appears, you should have something like this.
To view the chart, select the 'Download' option from the top menu.
Move the menu selection bar to the detail 'SUBs', once it shines, click on it.
on the mouse button press the 'Return' key. Remember that my menu may look like
different because I am using a commercial version of QBasic. Once you have done
this must be entered on another screen in a different window. You should have something
like the following.

To see the graph, select the 'Download' option from the top menu
*Nota:
detail
Now you will see the word "Untitled" shining in the window. The word "Untitled" is the
name of your main program. This is called Module. Your main program
"(module" will call or link to the Subprocedure to complete its task. Your program
the principal (module) is called "Untitled" followed by the new Subprocedure called
"hello". In order for us to call this new procedure "hello" we need
let's return to the main program (module). We can do this with two clicks
followed on the screen called "Untitled" or by pressing the return key now.
We can also press the 'Edit active' button with the mouse.
If you do the above correctly, you will now enter the main program (module). This
is where we were at the beginning before getting you into all these procedures. You
You are probably thanking heaven for it.
Okay, now you will be ready to write a program naming or chaining the
subprocedure you have created. This is probably an easy part, however for
I will punish you by introducing you to a new QBasic command.

Declare
Every time you create a new Subprocedure, you must declare its use in the program.
main (module). The reason is very simple, all the time the program runs in
In QBasic you need to count every Subprocedure that is there to be used.
Thus, in the construction of the current program, we are going to create a simple Subprocedure.
But to be able to use it, we need to inform QBasic that it is available to use.
First, we are going to look at the basic methods of the 'DECLARE' statement. To tell us
that we have a Subprocedure called "hello" we must first type this line in the
main program window (module), which you should have in front of you now and not
it should not contain any programming code in it. Please enter the following
code.
DECLARE SUB hello ()
CLS
hello
The statement 'DECLARE' works like this. Basically, we are saying that the
The subprocedure is called "hello", as you already know. Secondly, we have some
opening and closing parentheses. These parentheses represent some argument that we are going to
add later. Don't worry about this for now, just make sure to enter them in the
program always unless told otherwise.
The next line clears the screen. The next line is an interesting part. There is
where all the interesting things happen. Now we are calling a Subprocedure called
Hello
of the Subprocedure and run everything within the subprocedure, within the program.
It's very easy to understand once you know how. Press 'F5' to run the program and
see what happens.
So we have a very basic introduction to Subroutines and Subprocedures. Remember
this tutorial covers the subprocedures in a very basic way, about which
you will learn more in future tutorials. In the next tutorial, I will write a program that uses
not just one Subprocedure, but a couple, just showing you how it works.

You might also like