Qbasic Tutorial
Qbasic Tutorial
1
2
2
3
QBASIC-TUTORIAL
FOR
INTRODUCTION
3
4
Since QBasic is very versatile, it is a must know for today’s programmers. Almost
everything can be done with QBASIC. Although today’s fashion isn‘t DOS anymore, it can be quite
useful to know something about DOS and about QBASIC, which is a DOS-oriented programming
language.
QBASIC certainly is the easiest programming language of all to learn. Also, when knowing
this language, other programming languages are very easy to learn, like Visual Basic, or even C or
Java. Also, beginning with a DOS-language, opens new ways and knowledge when stepping to
Windows-based languages. Because the most you can use in DOS can also be used in Windows.
Although QBASIC isn‘t too fast, a lot can be done with it, easily.
THIS TUTORIAL
In this tutorial, specific ways of text-decoration are used to keep things clear. I will explain
them now.
VARIABLES
Variables are temporary storage places of different kinds of values. Variables have a unique
name. There are different types of variables which can all contain different values. (For the types,
see section ‘Data Types’). To store something in a variable, use the equals-character ( =). To get the
value that is stored in the variable, just type its name.
If you still don‘t understand this explanation, try this: a variable can be seen as a drawer in
e.g. a closet. You can put things in the drawer, and you can look into a drawer to see what’s in it,
you can remove the contents of the drawer, and you can remove the drawer completely.
DATA TYPES
There are 6 data types available in QBASIC.
Integer is the first and most important data type. This data type is the most used data type of
all. You can store only non-decimal numbers in an integer. Since an integer contains 16 bits, you
can only store values from –32768 up to 32767.
Long integer is the second. Long integers are not used that often, since mostly integers
work fine. Long integers are actually the same as normal integers, so only non-decimal numbers.
However, a long integer contains 32 bits, allowing you to store values from –2147483648 to
2147483647.
Single, which can contain decimal numbers. Singles are 32 bit and you can store values
from around 9e40 to 9e-39.
Double, which can also contain decimal numbers. The only difference is that doubles are 64
bits large, thus providing space for values from around 9e305 to 9e-310.
String. The string is a special type. It can contain characters like ‘A’ or ‘ƒ’. Strings are
always notated with double quotation marks around them. (“). The length of the character-list can
range up to 64k.
String * n%, which is a string with a set length. Viz.: n characters can fit in the string.
Each data type also has its own postfix-sign. I.e. that if you call a variable for instance
‘number%’, the variable is called ‘number’ and declared as an integer. If variables don‘t have a
post-fix sign, they are singles, unless you said them not to be ;-). Here are all postfixes:
Integer: %
Long: &
Single: !
Double: #
String: $
VARIABLE DECLARATION
Just as visitors needs an introduction into the new environment, so every variable needs to be
declared so that QBasic can recognise the Data type each variable should hold. To declare variable
in QBasic, the word Dim is use. Thus;
Dim num As Integer ‘[num is declared as integer]
Dim Total As Long ‘[Total is declared as Long]
Dim number As Single ‘[number is declared as Single]
Dim youname As String ‘[yourname is declared as String]
5
6
REMARKS IN CODE
You can also put remarks in your code, which can make your code more clear to yourself
and other people reading your program. To make a remark in code, use a single quotation mark (‘)
followed by the remark. You can also use the command called ‘Rem’ which actually does nothing
so it’s suitable for remarking (Rem is an abbreviation of remark).
OPERATORS
Just as in Mathematics, QBasic has some Operators it uses in its expressions. Take a look;
Addition +
Multiplication *
Division /
Subtraction -
Upper caret ^
Less than <
Greater than >
Equal to =
Not Equal to <>
Square root SQT
PRINT “Hello!”
PRINT “This is my first program!”
END
Then run the program by pressing the Shift key and the F5 key simultaneously. And…
There you see your first program. Although the program will be ended immediately after you
pressed F5, the program still gives some output.
PRINT [var] Print shows something on the screen. The command itself accepts everything
from strings to numerical values.
END Stops your program (and returns to the QBASIC-IDE if running from the
IDE).
6
7
You don‘t have to write the commands all in capital, the IDE does this automatically for
you.
You can always use the keycombination [Ctrl]+[Break] to immediately stop the program.
COLOR 7
PRINT “This is grey text!”
COLOR 4
PRINT “This is red text! :)”
COLOR 15
PRINT “White text!”
COLOR 0
PRINT “You can‘t see black text though! :)“
END
Run the program. If you already knew what Print did, you’ll probably now know what Color
does.
COLOR fground% Sets the current foreground color to fground% and the background color
[, bground%] to bground% (bground% is optional, thus you don‘t have to specify one).
(Note the integer-postfix, so number must be an integer). For now, we
only use 16 colors:
0 = Black 1 = Blue 2 = Green
3 = Cyan 4 = Red 5 = Magenta
6 = Brown 7 = Gray 8 = Dark gray
9 = Light blue 10 = Light green 11 = Light cyan
12 = Light red 13 = Light magenta 14 = Yellow
15 = White
You should know these values and colors by heart!
This isn‘t the end. You’ll now watch how you can position some text on the screen:
CLS
LOCATE 1, 1
PRINT “Upper left”
LOCATE 1, 69
PRINT “Upper right”
LOCATE 13, 30
PRINT “Somewhere in the middle”
LOCATE 25, 69
PRINT “Lower right”;
END
Using locate, we can though position text on the screen. With positioning, it might be handy
to know what the numbers behind Locate mean. Well, the first is the y-coordinate, the second is the
x-coordinate. The y-coordinates range from 1 to 25 (1 is the first line, 25 the last). The x-
coordinates range from 1 to 80 (1 is the most left and 80 the most right).
7
8
Of course, Locate can also be used in conjunction with Color to create colored and
positioned text. Note that Locate only applies to the next Print-command encountered, Color
applies to all Print-commands until another color is set using another Color-command.
Did you notice the ‘;’ behind the last print command? I hope so… A ‘;’ has been put at the
end of the print command because the last two lines of the screen (y-cords 24 and 25) are a little
weird. Normally, when you put text there, the entire screen shifts 1 line up, thus removing
everything on the first line. The ‘;’ at the end of a print command, prevents the screen from shifting
up.
CLS
PRINT “Please wait 3 seconds…”
SLEEP 3
PRINT “Now press a key to end the program”
SLEEP
END
SLEEP [number%] If you specify a number behind sleep, the Sleep command waits that many
seconds before proceeding. If you don‘t specify a number, Sleep waits
until a keypress before proceeding.
Pressing a Control-key during a Sleep-command (with a value specified), will cause the
Sleep command to be skipped. So if you say ‘Sleep 1000’ and someone presses the Control-key, the
computer immediately proceeds with the next command.
CLS
DIM MyInteger AS INTEGER
DIM MyString AS STRING
MyInteger = 7
MyString = “My name is Dela”
PRINT MyString
PRINT “And my lucky number is:”; MyInteger
END
As you could see in this example, first, 2 variables are defined. MyInteger is defined as an
integer and MyString is defined as a string. Then, the value 7 is put into MyInteger (assigned to…),
and the text “My name is Dela” is put intro MyString. Then, MyString is printed to the screen.
Finally, the text “And my lucky number is:” is printed on the screen with at end of the text a 7.
8
9
Do not confuse PRINT “MyString” with PRINT MyString. (The quotation marks are the
difference). The reason is very simple: the first print prints the text “MyString” literally to the
screen, and the second prints the contents of the MyString-variable to the screen.
CLS
DIM number AS INTEGER, answer AS INTEGER, yourname AS STRING
INPUT “Now enter a number:”, number
INPUT “Now your name:”, yourname
PRINT “So “; yourname; “ you wanted to know what is”; number; “
times 5?”
answer = number * 5
PRINT “Well, it is:”; answer
END
You see, when you run this program, you are asked for a number and your name. It shows
some text and the correct answer of the number you entered times 5. (the asterisk means ‘times’).
INPUT [text$,] variable First, it shows the text stored in text$, then asks the user to
INPUT [text$;] variable enter a value for variable. If you use a comma or a semicolon
as separation-mark you’ll get a question mark or no question
mark at the end. (A comma gives none, a semicolon gives one
at the end of the text).
Note: you don‘t have to put double quotation marks when entering a string value. If your
program asks you to.
Exercise:
1. Write a program to calculate the Area of a Triangle. Ask the user to enter the height and
breadth.
9
10
2. Write a program to calculate the Area of a Circle. Allow the user to provide the radius, and
name. Call the user’s name and thank him or her for using your program. Take π = 3.142
CHAPTER PROGRAM
You probably don‘t know yet, but at the end of each chapter, there is an example program
and some exercises about everything you know so far. You at least should have made all exercises
before proceeding to the next chapter.
CLS
DIM favoritecolor AS INTEGER
LOCATE 10, 10
PRINT “Please enter your favorite color-number beneath:”
LOCATE 11, 10
INPUT “Yes here:”, favoritecolor
COLOR favoritecolor
PRINT “So your favorite colornumber is:”; favoritecolor
PRINT “Now press a key to proceed”
SLEEP
PRINT “Quitting this program… Please wait 5 seconds”
SLEEP 5
END
Exercise #1: Show all colors on screen by showing the text ‘this is …… text’ with on the …’s the
color’s name.
Exercise #2: Same as Exercise #1 but now show it exactly at the most right of the screen.
Exercise #3: Ask the user for his ASL then show the statistics in an emptied screen.
Exercise #4: Make a kind of interview with the user with about 10 questions. At the end, show a
report about the user’s answers. (E.g. if the user said his name is ‘Harold’ in the report it must say
‘His name is Harold’).
CLS
DIM yourname AS STRING
INPUT “Please enter your name:”, yourname
PRINT “Your name in upper case: “; UCASE$(yourname)
PRINT “In lower case: “; LCASE$(yourname)
10
11
I understand if you can‘t understand it all at once, but that is no problem. The only thing you
haven‘t had yet are Functions, Statements, Expressions and Metacommands. I’ll explain them here
(except the metacommands).
11
12
Well, the thing is, that there are Functions too. Functions always return a value or a string
and can never stand-alone. Just try to type CHR$(139) in the IDE and try to run. You’ll get an
error! Why? Simply because CHR$ returns a value, but the QBASIC IDE can‘t do anything with it,
does the IDE has to show it on the screen? Or does it have to put it in a variable? That’s why
functions never are standalone, you always have to specify what the QBASIC IDE has to do with
the return value. You can say: PRINT CHR$(139) or MyString$ = CHR$(139). Because now the
IDE knows what it has to do with it. In the first case, it prints the returns value of Chr$(139) to the
screen, in the latter, it stores the return value of Chr$(139) in MyString$.
QBASIC also knows things that are called ‘expressions’. Expressions are some kind of
formulas like 3 + 1. You have often used expressions already, although you probably didn‘t know
that. Expressions can also contain functions like this one: 3 + SQR(5). The SQR function simply
calculates the Square Root of, in this case, 5. Just like functions, expressions are not standalone.
This is very easy to explain. For instance, if you type 3 + 3 in the IDE then try to run it, you’ll get
errors, why? Because when the compiler sees the line ‘3+3’ you just typed, it thinks ‘hrrmmm 3+3
and then? 3+3 so what? What do I have to do with 3+3?’ You can for instance specify that the
answer of 3+3 must be printed to the screen, or that it must be stored in a variable. By doing this:
PRINT 3 + 3
Answer% = 3 + 3
Also, functions and expression can be nested, the statements cannot. Simply look at the
following example, it looks kinda complex, but it is actually the ABC-formula (Maths!!
Remember?).
STRUCTURE I: CHOICES
QBASIC is also able to make choices, by a special command called ‘IF’. You must specify
an expression (which must return a boolean) behind the IF, then a ‘THEN’ then type what is going
to happen if your expression is true. Perhaps it’ll be more clear if I show you an example:
12
13
When you read this program, it should have been cleared up by now, because you now know
that the first IF checks if number% = 0 and if so, it prints ‘you entered a zero’ on the screen. The
second checks for number% 0 and if so, it prints ‘you didn‘t enter a zero’ on the screen.
In QBASIC, the character doesn‘t exist. Instead of that, you should use <> for ‘not equal
to’.
Actually, the first example was inefficient, because 2 if commands were needed. I could
extend your knowledge now by showing you this example:
You’ll probably already understand what this program does, but let’s explain anyhow. The If
command now checks if number% is equal to 0, if so, it prints ‘zero!’ on the screen, if not it prints
‘no zero!’ on the screen.
This If-structure can be more extended by starting a second line. This is very handy, because
then you can execute more commands when an expression is true. Watch this:
I think this example should be clear enough. For the ones who don‘t understand, this
program will calculate 1/number% for a number entered by the user. If the user enters a 0, the part
between the If and the Else will be executed. If it isn‘t a 0, the part between the Else and the End If
is exectuted.
Note: if you put the whole IF statement on one line, which is possible, you don‘t have to use
an ‘End If’. This is only used for If statements which are not written on one line. The End If simply
defines where the piece of code, which belongs to an If-statement, ends.
There’s actually 1 function of If left, which is ‘Elseif’. Try to understand this example:
13
14
As you can see here, the Elseif-part of an If-statement simply is some kind of another If
statement. The only difference between writing it like in the example and writing it separated (2
different If-statements) is that, when you write them separated, they can both be executed, if you
write them as above, only 1 of the choices that are presented will be executed, which usually is the
first If-part, which has an expression which is true.
You can add as much Elseif-parts as you want. Also, the Else part is not necessary. Also,
Elseif-parts may only be used if an If-statement is not on 1 line, with Else this is possible. (See 2nd
example of this chapter).
For large selections, you shouldn‘t use If, but Select, which is the following subject.
(Because writing 100 Elseif’s is quite some work ;-) ).
Select not only is less typing, it’s also much clearer than If. Everyone should understand
what this program does.
Jumping is not too often used, but is worth talking about. (Simply because newbies and
beginners use jumping a lot ;-) ). Jumping can be achieved by 2 commands, Goto and Gosub. I’ll
also explain them in that order. So goto first:
Again:
PRINT “I’ll calculate 1/n for you”
PRINT “(type a 0 to end)”
INPUT “Please enter n:”, n%
IF n% = 0 THEN GOTO nowend
Answer# = 1# / n%
PRINT “The answer is:”; Answer#
PRINT
PRINT “Do you want to enter another n?”
TypeAgain:
INPUT “(j/n):”, yesorno$
Yesorno$ = UCASE$(yesorno$) ‘convert to uppercase
IF yesorno$ = “J” THEN
GOTO Again
ELSEIF yesorno$ = “N” THEN
GOTO nowend
ELSE ‘user typed no N or J
GOTO TypeAgain
END IF
nowend:
END
As you could see in this very exaggerated program, Goto is used to jump back in your code
(like GOTO Again does), or forward (like GOTO nowend). Goto must be used in conjunction with
a label. You must specify a label somewhere in your code, by typing your label’s name followed by
a colon “:”. Then you can use Goto yourlabel’sname to jump to your label. From there, the program
starts running as normal.
Label: Defines a label with name ‘label’. You can fill your own name in for ‘label’, like
done in the example.
GOTO label Jumps to the label called ‘label’. From there, the program will continue.
If you refer to a label (using Goto) that doesn‘t exist, you’ll get an error.
In some cases, Goto can be handy. However, overuse of the command will result in what is
called ‘spaghetti-code’. The code will then be that complex, that even the best programmer can‘t
understand the code, which is due to the fact that when using Goto, the code can be run on
numerous ways. So use Goto with caution.
That’s why they invented ‘Gosub’. Which will, in some cases, work more efficient than
Goto. By declaring a module-level sub, this works almost the same as a procedure or function (Sub
or Function). The only difference between Gosub and Goto is that Gosub requires a ‘Return’
statement. As you’d already seen in the example above, Goto doesn‘t require any. Well, let’s just
show you an example:
CLS
PRINT “I can calculate contents”
PRINT “Please enter which contents you want to calculate”
15
16
Calccubecont:
Cont% = l% ^ 3
RETURN
Calccylindercont:
Cont% = 3.1415! * (r% ^ 2) * h%
RETURN
Calcballcont:
Cont% = (4 / 3) * 3.1415! * (r% ^ 3)
RETURN
I think it is clear by now, but let’s explain anyhow. The Gosub command on line 21, refers
to a label called ‘calccubecont’. In the previous section about Goto, I’ve already explained what a
label can do. The program then jumps to that label, running everything what is said below. Until the
program arrives at a ‘Return’. Which is used to show the program the piece of code to be run is over
and the program jumps back to the Gosub command it came from, but now ignoring it and thus,
proceeding with the code after the Gosub command.
GOSUB label Works almost the same as Goto. Have the program jump to the specified
label.
RETURN Gosub requires this command, specifying the end of the ‘label-code’.
When reaching this command, the program jumps back to the last Gosub
command it came from.
When either overusing the Gosub command, or nesting them, this will result in a memory
error. So try to avoid it.
16
17
Nesting is consequently putting commands of the same purpose in eachother. This is e.g.
possible with If-statements (putting an If inside an If, which also is inside an If). It can also be done
with Gosub or Select. Later on, you’ll learn that it is also possible with Do, For and While.
CHAPTER PROGRAM
Ready? I hope so.
Again:
CLS
COLOR 15, 1
PRINT “==================================”
PRINT “ =========== ===========”
PRINT “ === Palindromotron ===”
PRINT “ =========== ===========”
PRINT “==================================”
COLOR 7, 0
PRINT “This program is capable of reversing”
PRINT “words, sentences or entire texts.”
GOSUB reversestring
PRINT “This is it, reversed: ”; EndString
PRINT
EnterAgain:
PRINT “Do you want to enter some text again? (y/n)”
INPUT “(y/n):”, choice$
Choice$ = LTRIM$(RTRIM$(UCASE$(choice$)))
IF Choice$ = “Y” THEN GOTO Again
IF Choice$ = “N” THEN END
‘the program will only get here if Choice$ ≠ “Y” AND Choice$ ≠ “N”
GOTO EnterAgain
Reversestring:
‘this function can actually be made much better using FOR or WHILE
‘but since you haven‘t had them yet, I do it with GOTO
NextChar:
CurrentPos = LEN(TheString)
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos – 1 ‘decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar
RETURN
EXERCISES
Exercise #1: Make a kind of quiz. Show about 20 questions and at the end how many you
had guessed wrong.
17
18
Exercise #2: Make a program that counts the number of e or E in an entered string, and
shows that number.
Exercise #3: Make a program which asks the user to enter as much values the user would
like to, then calculates the average of the numbers.
Exercise #4 (HARD!): Make a text adventure. If you don‘t know what a text adventure
game is, try downloading ‘Neptune Caverns’ at www.nemesisQBasic.com.
18
19
END IF
END IF
END IF
This code looks clean. But can you imagine how much you have to type when you have
more criteria? Look at this one:
If you pronounce the code just like it says, it is already clear. In this code, ‘AND’ is a
boolean operator, which, in this case, pastes two criteria together. Here’s the rest:
These schedules are called ‘TF-tables’ which stands for ‘True-False Tables’. In these
schedules you can see what an operator returns when evaluating the criteria. As you already know,
an If-statement is only run if the criteria is TRUE. So if the criteria is ‘5 = 5’ then this will be TRUE
because in this case, five will always be five. However, when you use 2 or more criteria, you’ll need
to specify the link between the criteria. E.g. does every criteria has to be true? Then use AND.
Here’s some code to get used to it:
A = 1
B = 2
C = 3
D = 4
IF (A = 1) AND (B = 2) THEN
‘this will only be run if A is equal to 1 AND B is equal to 2.
END IF
IF (A = 2) OR (C = 3) THEN
‘this code will only be run if one of both criteria is true, or
‘when both are true. So this code will be run when:
‘A = 2 and C <> 3
‘A <> 2 and C = 3
‘A = 2 and C = 3
‘so or is very flexible.
END IF
IF (C = 3) XOR (D = 3) THEN
‘this code will only be run if one of both criteria is true.
‘if both are true, this is not run (also when both are false).
19
20
IF (D = 4) EQV (A = 1) THEN
‘this code will only be run if both are true, or both are false.
‘So it will be run when:
‘D = 4 and A = 1
‘D <> 4 and A <> 1
‘I seldom use EQV though
END IF
IF (B = 3) IMP (A = 1) THEN
‘this code will be run when both are true, when both are false
‘or when only the first one is true (if the second is true and
‘the first is not, this code is not run).
‘So when:
‘B = 3 and A = 1
‘B <> 3 and A <> 1
‘B = 3 and A <> 1
‘Never use it often though
END IF
If you’d studied the TF-Schedules well, you would have noticed one thing. About the
‘NOT’ operator. The difference is that, AND, OR, XOR, EQV and IMP are binary-operators, and
that NOT is an unary-operator. What does this mean? Well it’s actually very simple. The binary
operators link 2 criteria together, while unary operators link only 1. So NOT only links 1. But how?
Simple, NOT turns around the value of all inner values. Example:
IF NOT(A = 5) THEN
‘this code is only run if A is not equal to 5. If A = 5 then the
‘expression between the brackets is true, because A = 5. Then not
‘turns around that value, so it is converted to false. Also, when
‘A = 4, the inner expression is false, because A = 5 is false
‘because A = 4, so not converts false to true, and the if-clausule
‘is run.
END IF
Till now, I kept it easy. But you can also imagine that sometimes, people need very complex
structures. When using more than 1 binary operator, you should also apply brackets to your code so
the QBASIC IDE knows which binary operator to evaluate first. Here’s a very complex example:
IF (((score = 200) AND (level = 5)) OR ((deaths = 10) EQV (units >
200))) AND (NOT(U = 9 IMP kills < 1) XOR (skill <> 5)) THEN
‘find out yourself when this code will be run!! :-D ;*)
END IF
20
21
As you can see when you run this program, the numbers 1 up to and including 100 are
printed on the screen. How’s that possible? Well, first you say that internalvar = 1, it has to go too
100 with steps of +1 in size. Thus, the for equals internalvar to 1, runs all the code beneath, until it
encounters a ‘Next’ command. Here it says ‘Next internalvar’ so the program jumps back to the
‘For internalvar …etc….’ and increases internalvar by step, which here is 1. So the second time the
code will be run, internalvar will equal 2. Then 3, then 4, 5, 6, 7, etc… up to and including 100.
Then the program sees the specified limit has been reached and now skips the ‘Next’ command
when it arrives there.
The for-routine is very flexible and can almost be used for everything what is in need of a
counter. Take a look at this example:
It doesn‘t matter if you can‘t yet understand this. The only thing that matters is that you
understand what For does and how and when to use it.
The step-factor is optional, but when left away, the (virtual) step-factor will always be 1.
DO
PRINT “Always the same text!!! :*)”
LOOP
Press [Ctrl] + [Break] simultaneously to immediately stop the program and return to the
IDE.
As you can see when running this program, the commands between the Do-Loop are run for
ever and therefore the program will never stop. How it actually works is quite simple: the program
21
22
encounters a ‘Do’ and remembers where it was, runs some commands, then encounters a ‘Loop’
and the program will jump back to the last ‘Do’ it encountered (actually it is the do where the loop
belongs to).
Here are 2 examples about the expression-loop:
DIM I AS INTEGER
I = 0
DO
I = I + 1
PRINT I
LOOP UNTIL (I = 10000)
I = 0
DO WHILE I < 10000
I = I + 1
PRINT I
LOOP
You can see the expression can be added to a Do-Loop routine by adding an Until or a
While. Both need an expression, or more linked by using Boolean Operators. As you could have
already known (by pronouncing the code), a Do-Loop routine with While, will run the code while
some kind of expression is true, and it won‘t be run anymore if that expression is false. The Do-
Loop routine with Until will run while some kind of expression is not true, and will exit the loop
when the expression is true. (Pronounce: Loop the loop while my score is smaller than 100, or Loop
the loop until my score is greater than 100, for example).
I = 1
PRINT “From the first loop:”
DO
PRINT I
LOOP UNTIL I = 1
22
23
Line 8: Start of a Do-Loop, and since the loop must be exited when I equals 1, and since still
I equals 1, I won‘t run the loop and will jump immediately to the Loop the Do belongs to.
Line 10: I jumped from Line 8 to Line 10.
It should be clear now, if not, play some with the Do-Loop :*). It will be clear to you in no
time. And… if you still don‘t understand after playing with it, here’s some very short explanation:
When the program encounters a Do-Loop Until, the program first runs the code, and at the
‘Loop Until’ it checks if the expression is true, if so, exit the loop, else, loop again. But when the
program encounters a Do Until-Loop, it first checks if the expression is true, if so, exit the loop,
else, loop the loop, and when it arrived at the Loop, it jumps back to the Do Until and checks again,
if the expression is true now, the program will jump forward to the Loop it came from and goes on
from there.
Linescounted = 0
WHILE Linescounted < 100
PRINT “This is line”; Linescounted + 1
Linescounted = Linescounted + 1
WEND
This example doesn‘t need much explanation. Just pronounce: ‘While the lines I counted is
beneath the 100, do this’.
I think it’s now time for an overall-view.
Concerning Do-Loop, you can only specify 1 expression per routine, so you can‘t make a
Do While-Loop While or a Do Until-Loop While (for example).
23
24
Example:
I = 0
DO
I = I + 1
IF I = 10000 THEN EXIT DO
LOOP
PRINT “Loop exited at I =”; I
I = 0
FOR I = 0 TO 10000 STEP 1
IF I = 5000 THEN EXIT FOR
NEXT I
PRINT “For exited at I =”; I
END
EXIT DO Immediately exits the first specified loop. The loop of that kind is exited if this
EXIT FOR command was in one of them.
CHAPTER PROGRAM
DO
CLS
PRINT “Prime number calculator!”
PRINT “By Mr. Redeemer”
PRINT
PRINT “This program calculates all prime numbers up to”
PRINT “a number n (N > 10)”
N% = 0
WHILE N% <= 10
INPUT “Please enter n:”, N%
WEND
PRINT
PRINT “1”
PRINT “2”
FOR primec = 3 TO N% STEP 2 ‘step 2 because only odd nrs.
24
25
Dividable = 0
FOR primed = 2 TO primec – 1 STEP 1
IF primec / primed = INT(primec / primed) THEN
Dividable = 1
EXIT FOR
END IF
NEXT primed
IF Dividable = 0 THEN PRINT primec
NEXT primec
PRINT
PRINT “That were all.”
PRINT “Want to see more?”
K$ = “a”
DO
INPUT “(yes or no):”, K$
LOOP UNTIL LCASE$(K$) = “yes” OR LCASE$(K$) = “no”
IF LCASE$(K$) = “no” THEN EXIT DO
LOOP
EXERCISES
Exercise #1: Make a program that can calculate the factorial of an inserted number. If you
don‘t know what a factorial is, e.g. the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120. The factorial of 10 is
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 = 3628800.
Exircise #2: Make a program that can calculate perfect numbers. A perfect number is a
number of which the sum of all dividers is equal to that number. E.g. 28 is a perfect number,
because the dividers are: 1, 2, 4, 7, 14. And the sum of the dividers is again 28!
Exercise #3: Make a program which can calculate combinations. (For readers with a Ti-83,
the possible combinations can be calculated with nCr (math-prb)). The user must be able to insert
the range and the length. E.g. if the user enters a 3 for range, only the numbers 1, 2 and 3 will be
used for cominations. If the user enters a 4 for length, the program calculates all possibilities with
length 4. So 1111 and 1321 are 2 of the possible combinations.
Which means this: 1 + 2 + 3 + 4 + 5 + … + n. So calculate the sum of all positive integers up to and
including n.
CATALOGUE
Every command, function, keyword, (simply everything) which I wrote about is in this list.
Here you can find every keyword which is dealt in this part.
AND CHR$
AS CLS
ASC COLOR
CASE DIM
25
26
DO NOT
DOUBLE OR
ELSE PRINT
ELSEIF REM
END RETURN
EQV RIGHT$
EXIT RTRIM$
FOR SELECT
GOSUB SINGLE
GOTO SLEEP
IF SPACE$
IMP STEP
INPUT STR$
INTEGER STRING
IS THEN
LCASE$ TO
LEFT$ UCASE
LEN UNTIL
LOCATE VAL
LONG WEND
LOOP WHILE (expression-waiter)
LTRIM$ WHILE (loop-command)
MID$ XOR
NEXT
In case of any further enquiry you can whatsapp Sir Redeemer on 0243501537
REFERENCE
QuickBASIC-Tutorial Part I: Beginners & Newbies Edition
By Neo Deus Ex Machina
Member of the Blind Coding programming team
http://www.blindcoding.cjb.net
2002-17-10
26