QBasic Tutorial Chapter #1
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: QBasic Tutorial Chapter #1

  1. #1
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    735

    QBasic Tutorial Chapter #1

    I know that ThePreacher wrote a text on QBasic, but I thought I'd share my insight into the matter. Just tell me what you think, thanks.

    So here is Qbasic tutorial, chapter one...

    Chapter One.

    This little tutorial was written for beginners entering the world of QBasic. A lot of people setting out to learn QBasic will have never have learnt any other programming language before, so I will keep this in mind.

    As always, I am not going to load you don't with pointless historical information about QBasic but here is the low-down.

    QBasic stands for Quick BASIC. BASIC stands for Beginner's All-Purpose Symbolic Instruction Code. Another result for making words to fit the acronym letters not the other way around.

    QBasic programs take the ASCII (usually) form *.bas. You can use a compiler to make them into *.exe programs. You can download a QBasic compiler anywhere.

    Here is a sample "Hello World" program:

    ********************** helloworld.bas **********************

    CLS
    PRINT "Hello World"

    **********************************************************

    What this does is clear the screen ("CLS") and print out the message "Hello World". If you have read my tutorial on MS-DOS or you already know DOS commands, you will notice that the CLS command is the same. Unfortunately, this is the only command that is the same.

    Now save that as "helloworld.bas" and run it in QBasic or your QBasic environment if you have one.

    Here is an example of an input program. It will prompt you for a string and then it will print it back out.

    ********************* inputexample.bas ***********************

    ' An example input program
    CLS
    PRINT ""

    INPUT "What is your name: ", urname$
    INPUT "What age are you: ", urage
    INPUT "What is your favourite colour "; urcolour$

    PRINT "Your favourite color is: "; urcolor$
    PRINT "You are "; urage$ ;" years old"
    PRINT urname$; " is your name."

    SLEEP

    PRINT "Thanks!"

    **************************************************************

    Here I introduced the universal programming language addition: the comment. QBasic comments always begin with a '. If you are new to programming languages, comments (remarks) are just little optional messages you can leave for people (or yourself) who are looking at the source code of your program, maybe to tell them what a certain command does or just as a message about the credits or any copyright (yuck!) information. Basically, the command interpreter ignores anything after (to the right of) the '.

    Then there is the INPUT command. You will notice the text surrounded with the quotes will appear beside the place where you are meant to enter the text. The text that is entered will be saved into the variable urname$. The '$' means that it is a string variable. If there is no '$' after the name of the variable, this would indicate that it is an integer variable. If you don't understand what these terms are, I'm sorry but maybe you should just stick with that lovely helloworld.bas, I am steaming ahead here.

    Then there is the second INPUT command which gets saved as the urage variable. This variable is an integer. Then there is the third INPUT command which gets saved as another string value called urcolour$. You may have noticed with the first two INPUT commands, we used the comma (,) to separate the prompt text from the variable and in the third, we used the semi-colon (;). With the semi-colon, it adds a question mark (?) with the prompt text, but the comma gets rid of that. I always use the comma, I think it looks nicer without it.

    Now we want to print out the variable values for the nice people. With this we just include the PRINT text message with the variable in place. However, as always, we have to close the quotes, add a semi-colon to separate any text with a variable and then open the quotes again if there is more to add.

    The SLEEP command pauses the program until someone hits a key. You can decide how long the program has to pause by, by adding an argument to the SLEEP command. The argument goes in the form of miliseconds. For instance if you wanted the program to pause for one second, you would enter "SLEEP 1000", if you wanted it to go for 23 seconds, you would have it as: "SLEEP 23000"

    By the way, the PRINT "" just adds an empty line.

    Here is another program. This deals with some math functions.

    **************************** mymathstuff.bas ********************************

    ' Here we deal the mathematical-related commands

    CLS

    PRINT ""
    PRINT "Welcome to the AMAZING Math Program!"

    SLEEP 2000

    CLS

    INPUT "Please enter a NUMBER: ", num1
    INPUT "Please enter another NUMBER: ", num2

    PRINT ""

    PRINT num1;" x ";num2;" = "; num1*num2
    PRINT num1;" / ";num2;" = "; num1/num2
    PRINT num1;" + ";num2;" = "; num1+num2
    PRINT num1;" - ";num2;" = "; num1-num2

    PRINT "Square Root of ";num1;" = "; SQ(num1)
    PRINT num1;" squared = "; num1*num1

    PRINT "Tangent of ";num1;" = "; tan(num1)
    PRINT "Cosine of ";num1;" = "; cos(num1)
    PRINT "Sine of ";num1;" = "; sin(num1)

    SLEEP

    PRINT "Thank you very much."

    ********************************************************************************

    Here is what the stuff we haven't met so far does.

    SQ() - This gets the square root of the number given to it as an argument, in this case the variable num1. If you don't know what a square root is, this whole program will probably be of no concern, but anyway, to find the square root of a number, is to find what number, mulitiplied by itself, will give the original number. For instance, the square root of 16 is 4. The square root of 4 is 2 and the square root of 2 is some god-awful decimal.

    tan() - Find the Tangent of a number

    cos() - Find the cosine of a number

    sin() - Find the sine of a number

    *****************************************************************

    Next we will deal more with string variables. If you are new to the world of programming, string variables, are variables that aren't numbers (aka. Letters and the like)

    For instance
    "Fred" is a variable and so is "Fr3d"
    12 is no and neither is d12

    Note: String variables are ALWAYS enclosed in (parenthesis)! Numbers (or "integers") aren't.

    In this program, we are going to ask the user for a phrase and then, just, do stuff with it.

    ************************ stringexample.bas ********************************

    CLS

    PRINT ""
    PRINT "Welcome to Jethro's World of Fun, and the like"

    INPUT "Please enter in a string: ", mystring$
    INPUT "Please enter in a number: ", mynumber

    PRINT "You said: ", mystring$
    PRINT "You said: ", mynumber
    PRINT mynumber, " is a number"
    PRINT mystring$, " is a string"

    IF INSTR(mystring$, "f") THEN PRINT "There is the letter 'f' in your name!"
    IF NOT INSTR(mystring$, "k") THEN PRINT "There is no 'k' in your name!"

    IF mynumber > 0 THEN PRINT mynumber; " is bigger than 0"
    IF mynumber < 0 THEN PRINT mynumber; " is smaller than 0"
    IF mynumber = 0 THEN PRINT mynumber; " is equal to 0"

    IF INSTR(mystring$, " ") AND mynumber > 0 THEN PRINT "There is a space in your phrase AND your number is bigger than 0!"
    IF INSTR(mystring$, " ") OR mynumber > 0 THEN PRINT "There is a space in your phrase OR your number is bigger than 0!"

    PRINT ""
    PRINT "Please press a key to continue..."
    SLEEP

    ***********************************************************************

    Here is some more stuff we haven't covered yet:

    IF...THEN - The IF command... I think this command is in pretty much every language expect maybe ASM and those kind of languages. Fortunately the IF command is virtually self explanatory:

    Syntax for IF:
    IF <condition> THEN <action>
    The <condition> is exchanged for your conditional case. Like name$ = "Jethro" or INSTR(urc, 4)
    The condition usually takes the form:
    item1 OPERATOR item2
    Like if you wanted to check if 3 is equal to 4 (makes no sense I know, but bare with me, this is leading somewhere :)
    IF 3 = 4 THEN <action>
    Or if you wanted to check if "Jethro" is equal to "Jones":
    IF "Jethro" = "Jones" THEN <action>
    Or course this will result negative and the action will not be performed

    There are many operators but here are the most common ones:
    = Equal To
    > Is bigger than
    < Is smaller than
    To reverse this you put "NOT" infront of the condition. For example:
    IF NOT 3 > 5 THEN <action>
    This means that IF 3 IS NOT bigger than 5 then the action will be performed. Of course it isn't bigger, so the action can be performed.
    The <action> in question is simply a regular command like PRINT "HELLO" or even CLS.

    We'll take this third IF command in stringexample.bas for instance:
    IF mynumber > 0 THEN PRINT mynumber; " is bigger than 0"
    This means that if the variable, mynumberr, is bigger than 0 then print out "(mynumber) is bigger than 0"

    However there is another type of condition which contains no operators (usually). This is called the "boolean condition". Boolean basically means that it can either be true (1) or false (0). There are two ways of performing these:
    The first is the way that I don't like:
    IF <boolean condition> = 1 THEN PRINT "True"
    IF <boolean condition> = 0 THEN PRINT "False"
    You can do that, if you want, but I prefer this way.
    IF <boolean condition> THEN PRINT "True"
    IF NOT <boolean condition> THEN PRINT "False"

    They both do the same thing but in my opinion, the second is better and easier to read.
    Here is an example of a boolean expression, which is also used in "stringexample.bas":

    INSTR() - This command is really easy. If argument number two is contained in argument number one, return true. That's it! Oh crap, I haven't explained what an argument is yet. Oh well, here it goes.

    We've all had arguments, be it with loved ones, co-forum-users, ourselves... But in the dark land of programming arguments have a more deadly sinister meaning...

    Basically, an argument is a value passed along to a sub/function or command. Everything you use after the command itself is an argument. It can be just placed after the command, separated by commas, or in (parenthesis):
    PRINT "Hello World"
    Where's the argument? "Hello World"! It is passed to the PRINT command which prints it out onto the screen accordingly.

    With a command like INSTR(), the first argument is mystring$ and the second is "f". Understand?

    AND/OR - This is used with the IF command. Basically AND means that the action will be met if both conditions are met and OR will be met if either condition is met.

    Expect Chapter Two sometime soon...

  2. #2
    Token drunken Irish guy
    Join Date
    Sep 2001
    Posts
    2,813
    Good work Damo! Keep em' coming and get bleedin studying or I'll pour a can of Dutch Gold over ya!

    a diabhal!

  3. #3
    Senior Member
    Join Date
    Jan 2002
    Posts
    218
    hey man, that was a very informative post. i have always been more into networking andsupport than programming, but i have been starting to dip into a bit with perl. just out of curiousity, what compiler do you need to work with qbasic? i know it used to come in windows way back in the day, but what about today? i think i would like to experiment with it a bit. your post has inspired me.
    \"Computer games don\'t affect kids; I mean if Pac-Man affected us as kids, we\'d all be running around in darkened rooms munching magic pills and listening to repetitive electronic music.\" Kristian Wilson, Nintendo, Inc. 1989

  4. #4
    Member
    Join Date
    Oct 2001
    Posts
    64
    you can complie it right inside the program

    just select make EXE
    and if you like the networking stuff i used to have some commands to get qbasic to talk with a another system, but I think its only by modem its been a while since I looked.
    -=Legacy Boy=-

    -= You mean there is stuff better then DOS? =-

  5. #5
    Member
    Join Date
    Oct 2001
    Posts
    64
    never mind I thoug you could compile it, but my version at work wont let me

    -=Legacy Boy=-

    -= You mean there is stuff better then DOS? =-

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    735
    Yeah some of the earlier versions of QBasic won't let you compile but I think it's version 4.5 that can let you. Now, Microsoft isn't making this anymore and for some reason you can't download it legally, but it's available on different websites

  7. #7
    Senior Member
    Join Date
    Jan 2002
    Posts
    883
    Nice tut.......

    PS: You can find QBasic on the CD in Windows 95OSR2,98,98SE and ME under tools oldmsdos. It's different on some versions, but it's on there somewhere.
    The COOKIE TUX lives!!!!
    Windows NT crashed,I am the Blue Screen of Death.
    No one hears your screams.


  8. #8
    Banned
    Join Date
    Dec 2001
    Posts
    159

    Thumbs up

    thanks for posting this. i was thinking of getting into programming and was thinking of starting with something easy. your qbasic tutorial covers all the basic stuff. good job. ;-)

    i found a site for qbasic compilers and interpeters (some free, some not)

    http://www.basicusers.net/files/index.shtml

    i havent tested anyof them out yet though.

  9. #9
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    A nice little tutorial. Sniff, sniff. It brings back so many memories. GW-Basic was my first programming language. Then I "graduated" to QBasic. I didn't think that people used this at all anymore other than for personal interest!
    OpenBSD - The proactively secure operating system.

  10. #10
    Senior Member
    Join Date
    Feb 2002
    Posts
    120
    Jethro,
    Very nice tutorial, very informative and basic. Good for newbies who need the first step in something as simple as basic, this will be a good thread to keep bookmarked to redirect all of those questions in the newbies forum on Q Basic.
    \"To follow the path:
    look to the master,
    follow the master,
    walk with the master,
    see through the master,
    become the master.\"
    -Unknown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides