Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Basic Programming Tutorial

  1. #1
    Senior Member
    Join Date
    Oct 2001
    Posts
    689

    Post QBasic Programming Tutorial

    So you heard a long time ago about some programming language called QBasic. This language isn't used much anymore except in its new incarnation as visual basic. If you learn QBasic, vb is quite simple. So what am I going to show you today? How to write a simple program in QBasic. First of all Basic stands for "Quick Beginner's All-purpose Symbolic Instruction Code" and was developed in the 1960's as a simple way to learn programming. This first simple program will use multiplication to find the area of a room.

    First of all the program in its entirety:


    CLS
    INPUT "Please enter the length"; Length
    INPUT "Please enter the width"; Wdth
    LET Area = Length * Wdth
    PRINT "The area is"; Area
    END

    QBasic is a very simple language, and for those programmers who are reading this that have never dealt with QBasic, this is an unstructured programming language meaning that you can use the GOSUB command to jump around to any place in the program at any time. Many experienced programmers do not like this feature, but when I took a class for this, it saved my life while writing programs. Also in this language there is no need to declare variables. Now I will explain the program line by line.

    CLS
    This command tells QBasic to clear the screen. Often times this is needed because old data from the same program doesnt automatically disappear.

    INPUT "Please enter the length "; Length
    This tells Basic to accept input from the user, and displays a message with the text in it. This user input is then assigned to the numeric variable "Length".

    INPUT "Please enter the width "; Wdth
    This is the same as the "Length" variable except one thing. If you havent noticed Ive been misspelling the variable "Wdth". This is because the word "Width is a word that is reserved.

    LET Area = Length * Wdth
    This is the mathematical function in the program. Once again since the variables don't have to be declared, it is easy to use "Area".

    PRINT "The area is"; Area
    Shows on the screen the sentence followed by the results of your calculation.

    END
    After you are finished you must tell QBasic that the program is finished. This is accomplished using the "END" command.

    A program of a similar nature written in java, or C++ would have taken a longer to write and more code. There is a lot more to QBasic, but this should get you started on your way. I feel that QBasic still has use as a teaching aid and that it is important to learn. If you need a QBasic compiler, Microsoft has a small one known as QBasic. There isnt anywhere free to find it except inside a textbook at the public library. If there are any mistakes in my post feel free to correct them.
    Wine maketh merry: but money answereth all things.
    --Ecclesiastes 10:19

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    472
    About the GOSUB: The reason programmers don't use this is that it results in something I call 'spaghetti-code', it's impossible to see the structure of the code. What happens is that when you get many lines of code, you just can't figure out how things work. And then, when you have to go back and change something, you're left with no chance at all.

    Good tut, though!
    ---
    proactive

  3. #3
    oblio
    Guest
    QBasic is available at qbasic.com

  4. #4

    Wink

    regards to ThePreacher,
    Reminds me of learning basic on the old amstrad 464.
    I still have the book for learning basic & the games you can type out yourself like BOMBER which is only about 70 lines long if memory serves me.
    I'll be going home over xmas & i'll see if I can root it out,
    you won't mind Preacher if I type some stuff out from it for your Basic tut.
    tH3#cL3aN3r

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    689

    Post More QBasic Commands

    In addition to those QBasic commands that are shown above I decided to add that a string variable(words or letters) can be identified by the "$" sign located at the end of it. An input string statement would look like this:

    INPUT "Enter your name"; Name$

    Next I will go over the goto command for QBasic, its called "GOSUB". This command enables you to go to a subroutine which is similar to a function in C++, and a method in Java. I will use the previous program to demonstrate how to use the subroutine. First the complete program.


    CLS
    GOSUB MainRoutine
    END

    MainRoutine:
    INPUT "Please enter the length"; Length
    INPUT "Please enter the width"; Wdth
    LET Area = Length * Wdth
    PRINT "The area is"; Area
    RETURN


    The main difference between this and the original program is:

    GOSUB MainRoutine
    This tells QBasic to jump to the part of the program with the name that is indicated.

    MainRoutine:
    This tells QBasic that the subroutine begins here.

    RETURN
    This tells QBasic to return to the main section. Unlike other programming languages, the data from within the subroutine is automatically available for use by other routines and the main program.
    Wine maketh merry: but money answereth all things.
    --Ecclesiastes 10:19

  6. #6
    larryjs
    Guest
    I think thats Qbasic you're talking about there Preach, old basic used line numbering.

  7. #7
    Senior Member
    Join Date
    Oct 2001
    Posts
    689

    Post

    I went ahead and changed everything to QBasic so there would be no confusion.
    Wine maketh merry: but money answereth all things.
    --Ecclesiastes 10:19

  8. #8
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,785
    firstbas from powerbasic is also available at qbasic.com. its a shareware program that never expires, which is their way to make you want their power basic program.

    with it you can write stand alone programs(compiles and links).
    the variables x$ = string var, x% = int. var
    you can make a string var equal any amount of charecters, thats a pretty cool feature.

    some useful commands:

    shell "some.exe" //runs a program and returns when its finished

    delay(3) // pauses the program for 3 seconds

    start "some.txt" //will open an existing text file with the app your system normally uses for that type of file.

    //you can create a batch file and run it as part of your program:

    open "batch.bat" for output as #1 'creat a file if it dosn't exist
    print #1, "pause" 'writes the dos command pause
    close 'closes the file so you can use it
    shell "batch.bat" 'runs the batch file and waits for it to finish

    create very small command line exe's

    can anyone tell me how to loop through a comma delimited file?
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  9. #9
    Senior Member
    Join Date
    Nov 2001
    Posts
    257
    A program of a similar nature written in java, or C++ would have taken a longer to write and more code.
    Code:
    #include <iostream.h>
    
    void main()
    {
     int area,width,length;
     cout<<"Please enter the width";
     cin>>width;
     cout<<"Please enter the length";
     cin>>length;
     area = length*width;
     cout<<"The area is "<<area<<endl;
    }
    Six lines compared to nine, and this c++ code could be compressed further, but for the sake of simplicity I left it as so.

    I'm not trying to dispute your claim of more code, for this example, but if you were going to write a more complex program, qbasic would quickly become inferior to java and c++. I'm sure you know this, but I thought I'd ******** for somebody starting out that basic isn't going to be used for anything but a learning tool.
    -Shkuey
    Living life one line of error free code at a time.

  10. #10
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,785
    This is a tutorial on qbasic, not camparitive religion. and how many lines of code are used from the include file your not counting.

    All programing languages are fun and although limited in what it can do, basic is a very high level language and therefor easy to learn.
    Its a great way to see what programming it about and strengthens the creative process, which is (imho) important when your working with more advanced languages, like c++, and java.

    Here's an example of bad coding, the print command is used instead of pixel positioning. A lot of spagetti code but still fairly understandable, but most important it works. i wrote the same thing using c, which i like better because you can pass arguments at the command line, but this is about basic.
    I'm not really sure if this was the final version, but here goes:
    ==================================
    color 12
    CLS
    BEGIN:
    print" "
    print" "
    print" "
    print" "
    print" "
    print" "
    print" **********************
    print" ***** Bug Buster *****"
    print" **********************"
    PRINT ""
    print " What do you want to do?"
    print ""
    print " 1) Block a site"
    print ""
    print " 2) See if a site has been blocked"
    print ""
    print " 3) Edit hosts file"
    print ""
    print " 4) Changed my mind. Exit"

    input, d%

    SELECT CASE d%

    CASE 1
    GOTO BLOCK:

    CASE 2
    GOTO FINDIT:

    CASE 3
    GOTO REDO:

    CASE 4
    GOTO FINA:

    CASE ELSE
    GOTO BLOCK:

    END SELECT

    BLOCK:

    cls

    input " Enter a site to block : ", blk$

    BLOCK2:

    cls

    open "c:\windows\hosts.sam" FOR append as #1

    print #1, "#"

    print #1, "127.0.0.1 "blk$

    close

    print " "
    print" "
    print" "
    print" "
    print" "
    print" "
    print" "
    print blk$" has been blocked !"
    delay(2)
    GOTO BEGIN:

    REDO:

    open "redo.bat" for output as #1

    print #1, "edit c:\windows\hosts.sam"

    close

    shell "redo.bat"

    cls

    GOTO BEGIN:

    FINDIT:
    cls
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""

    input " Enter the site to search for : ", blk$

    open "findit.bat" for output as #1
    PRINT #1, "@ECHO OFF"
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."

    print #1, "find /I "chr$(34);blk$;chr$(34)" c:\windows\hosts.sam"

    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."
    print #1, "echo ."

    print #1, "pause"

    close

    shell "findit.bat"

    cls

    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print " What do you want to do ?"
    print ""
    print " 1) (or hit enter) Block this site"
    print ""
    print " 2) Search for another"
    print ""
    print " 3) I want out"
    CLS
    input , do2%


    SELECT CASE do2%

    CASE 1
    GOTO BLOCK2:

    CASE 2
    GOTO FINDIT:

    CASE 3
    GOTO FINA:

    CASE ELSE
    GOTO BLOCK2:

    END SELECT

    FINA:
    CLS
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print " ********************************"
    PRINT " **** THANKS FOR USING BUGBUSTER ****"
    PRINT " ********************************"
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    print ""
    PRINT " from tedob1 "
    DELAY(3)
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

Posting Permissions

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