|
-
June 24th, 2002, 11:11 PM
#1
QBasic Chapter Two
QBasic Chapter One
I forgot I had already written Chapter Two in the QBasic saga, so here it is (sorry for the delay )
QBasic Programming Chapter Two by Jethro
---------------------------------
Chapter Two.
Welcome to the second installment of my QBasic series!
Index:
o FOR...NEXT
o WHILE...WEND
o DO...LOOP
o Sequential Files
FOR...NEXT
------------
Here is a qbasic program which uses the FOR statement:
******* for.bas ********
' This program uses FOR...NEXT
' fl00t!
CLS
FOR i = 1 TO 100
PRINT "Number: "; i
PRINT "Woo-hoo"
NEXT i
************************
Compile and run that program. What happens? The numbers should start at
one and go all the way to 100.
The FOR loop is in most languages: JavaScript, C++, PHP, even MS-DOS!
Unfortunately, QBasic being the... annoying, language that it is, the
syntax is *completely* different to all the other languages *sigh*
"FOR i = 1 to 100". This means that the variable (i) starts at 1 and
executes the print commands until i reaches 100.
"NEXT i" increments the i variable. This means that it adds 1 to i, so
the loop won't continue for infinity.
***** while.bas *****
' This program uses WHILE...WEND
' fl00t!
CLS
number = 20
WHILE number > 18
INPUT "Enter an number smaller than 18: ", number
WEND
PRINT "Well Done, Kiddo!"
********************
The WHILE loop is also a pretty common feature of most programming
languages. *Of course* the syntax is different.
"WHILE number > 18". This is pretty self-explanatory (as are most
things in QBasic). Basically, while the variable called "number" is
bigger than 18, keep executing the INPUT command. Of course, if there
was no opportunity to change the number variable in this, then the
loop would continue for eternity!
"WEND". I think this stands for "While End" which is basically the
statement which defines the end of the loop.
Here are two programs (one for FOR and one for WHILE) which will show
you what not to do! These two programs will just keep looping and
looping and looping and looping... you get the idea.
******* forever.bas *******
' FOR(ever)
' Bad #1!
CLS
FOR i = 1 to i + 1
PRINT "The number is ";i
NEXT i
***************************
and...
******* whilewereyoung.bas *****
' WHILE(we're young)
' Bad #2!
CLS
j = 10
WHILE j = 10
PRINT "J is 10"
WEND
*********************************
Do...Loop
Basically, this is another way of using the WHILE statement...
***** dowhile.bas *******
DO WHILE NOT j = 5
INPUT "Enter 5: ", j
LOOP
************************
As you can see, it is just basically an alternative method of looping.
This one loops while j is not equal to 5.
But there is another way of using do. It's called the "until"
****** dountil.bas *******
DO UNTIL j = 5
INPUT "Enter 5: ", j
LOOP
**************************
This program has the same function as the above except it keeps looping
UNTIL the j variable equals 5. LOOP indicates the end of the loop.
Sequential Files
----------------
Sequential files are a large part of QBasic programming and really easy
to use. A large term for something really easy.
Basically, sequential files are the way of using external files for
storing or reading data.
Here is an example of a program whiich uses sequential files.
******** sequential.bas *****
' This program stores data
CLS
INPUT "What is your name: ", name$
OPEN "hey.txt" FOR OUTPUT AS #1
PRINT #1, name$
CLOSE #1
******************************
Doesn't look to complicated does it? Here is a basic explanation of
everything.
'OPEN "hey.txt" FOR OUTPUT AS #1'... Basically, this OPENs the file
"hey.txt" (if it doesn't exist, a new one will be created) so it can
be written to (OUTPUT). #1 is what's called the "file number", or
"file pointer". This is what we call the file when we need to refer to
it, so we can read/write to it.
"PRINT #1, name$". This PRINTS out the variable name$ however because
we used #1 right after the PRINT command, the name$ isn't outputted to
the screen, its OUTPUT goes to #1 (ie. "hey.txt").
"CLOSE #1". This CLOSEs the file we have called #1.
There are 3 main access modes that we can use.
+++++++++-
OUTPUT - This is how we can write data to a file. Information written
to a file with the OUTPUT access mode, overwrites all (if any)
information already in the file.
---------
APPEND - With the APPEND access mode, we can write information to the
end of a file, preserving the existant information.
---------
INPUT - With this, we can get information *from* a file.
+++++++++-
Here is an example of a program using the INPUT access mode keyword. If
"hey.txt" doesn't exist, you'll get an error message when you are
running/compiling the program.
**** seq-input.bas ****
' This program uses the INPUT keyword
CLS
OPEN "hey.txt" FOR INPUT AS #1
INPUT #1, line1$
INPUT #1, line2$
CLOSE #1
PRINT "The First Line is: ", line1$
PRINT "The Second Line is: ", line2$
************************
As you can see, to get the information from #1, we use the INPUT
command.
That's enough for Chapter Two. Expect for about sequential files, among
other things, in Chapter Three.
Happy Programming,
Jethro
******** EOF ***********
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|