beginner's programming tutorial in · pdf file · 2016-01-29beginner's programming...

Click here to load reader

Post on 01-Apr-2018

252 views

Category:

Documents

13 download

Embed Size (px)

TRANSCRIPT

  • Beginner's Programming Tutorialin QBasic

    This document is meant to get you started into programming, and assumes youhave some experience with computers and with Windows 95 (or 98, etc.).

    Since this tutorial is written for people who don't like to read a lot of text, itincludes a number of examples. Therefore, you can do a lot of work in not muchtime.

    The more important chapters have a star ( ).

    Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc.

    http://www.geocities.com/progsharehouse/qbtutorMirror: http://development.freeservers.com/qbtutor

    http://www.geocities.com/progsharehouse/qbtutorhttp://development.freeservers.com/qbtutor

  • Table of Contents

    Part I: Q-Basics

    Chapter 1: Before you start

    Chapter 2: Your first program

    Chapter 3: Variables

    Chapter 4: Retrieving keyboard input from the user

    Chapter 5: The IF and THEN commands

    Chapter 6: Labels and the GOTO and GOSUB commands

    Chapter 7: Loops

    Chapter 8: What next?

    Part II: Intermediate topics

    Chapter 9: QBasic interface

    Chapter 10: Adding documentation to your programs

    Chapter 11: Reading and writing to files

    Chapter 12: Displaying graphics

    Chapter 13: Mathematics functions

    Chapter 14: Getting the current date and time

    Part III: Advanced topics

    Chapter 15: Arrays

    Chapter 16: Variable types

    Chapter 17: Subroutines and functions

    Chapter 18: Numbering systems

    Chapter 19: Memory

  • Before you start

    Before you can create a program in QBasic, you need the QBasic interpreter. Itis available from your Windows 95 (or 98) CD, or you can download it below.

    To access QBasic from the Windows 95 CD:

    1. Insert the CD into your CD-ROM drive.

    2. Click "browse this CD" (if the menu screen doesn't come up, then browse the CD fromMy Computer.

    3. Go to the \OTHER\OLDMSDOS directory.

    4. Open a program called QBASIC.EXE (this is version 1.1 of the QBasic interpreter).

    To access QBasic from the Windows 98 CD:

    1. Insert the CD into your CD-ROM drive.

    2. Click "browse this CD" (if the menu screen doesn't come up, then browse the CD fromMy Computer.

    3. Go to the \TOOLS\OLDMSDOS directory.

    4. Open a program called QBASIC.EXE (this is version 1.1 of the QBasic interpreter).

    Download it here (right-click and press "Save As"):

    QBASIC.ZIP (323 KB) - QBasic 1.1 interpreter and sample programs

    UNZIP32.EXE (90 KB) - Extracts the ZIP file

    To unzip the QBASIC.ZIP file with UNZIP32.EXE:

    a. Go to the Start Menu

    b. Click Run...

    c. Type the following (this loads MS-DOS):

    command

    d. Enter the following in DOS (assuming you saved QBASIC.ZIP to C:\QBASIC):

    cd c:\qbasicunzip32 -n qbasic.zip

    http://www.geocities.com/progsharehouse/qbtutor/qbasic.zipftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/unzip32.exe

  • Your first program

    After launching the QBasic interpreter (see before you start), you might see a window requestinga list of "parameters." If this window comes up, press the Enter key to continue.

    You should now see the QBasic interpreter, which has a blue background and displays a dialogbox at the center. (If the interpreter fills the entire screen, then you may want to press "Alt +Enter," to make it smaller.)

    Press the Esc key to hide the dialog box.

    QBasic interpreter - main screen

    Type the following (including the quotation marks) in the QBasic interpreter:

    PRINT "Hello World!"

    Now press F5 to run the program. You should now see a black screen, with Hello World at thetop, and Press any key to continue at the bottom.

    Press a key on the keyboard to return to the main screen.

  • (The figure below displays the "output screen.")

    QBasic interpreter - output screen

    If you run the program again, the interpreter adds another Hello World. QBasic adds HelloWorld each time the program is run.

    Deleting the programTo erase the current program:

    1. Go to the "File" menu.

    2. Click "New."

    3. The interpreter asks if you want to save the program.

    4. Select "No" (or if you'd rather keep the program, select "Yes").

    StringsThere are certain types of data (or information) called "strings." Strings contain a sequence ofcharacters (letters, numbers, and symbols) enclosed in quotation marks. For example, "HelloWorld!" is a string.

  • The following are also strings:

    "0123456789"

    "This is a string"

    "abc123"

    "1 + 1 = 2"

    "!@#$%^&*()"

    CommandsThere are also special functions called "commands" (also called "instructions"). A "command"tells the QBasic interpreter to do something.

    The PRINT command tells the QBasic interpreter to print something to the screen. In this case,the interpreter printed "Hello World!".

    TIP: Instead of typing PRINT, you can enter aquestion mark. For example:

    ?"Hello World!"

    With the PRINT command, you can also print numbers to the screen. Delete the current program(unless you already have) and write the following:

    PRINT 512 (or ?512)

    Press F5 to run the program. The program outputs:

    512

    ExpressionsAn expression is something the interpreter calculates (or evaluates). Such as:

    1 + 1 (returns 2)

    100 - 47 (returns 53)

    3 * 34 (returns 102)

    80 / 4 (returns 20)

    (100 * 3) + 56 (returns 356)

  • NOTE: The asterisk (*) means to multiply twonumbers; the slash (/) means to divide

    If you pass an expression to the PRINT command, the value returned (a number) is printed.

    Clear the current program, and then run the following:

    PRINT 512 + 478

    Program output:

    990

    If you enclose the expression with quotation marks, the expression becomes a string and isn'tevaluated. For example:

    PRINT "512 + 478"

    Output:

    512 + 478

    TIP: To clear the output screen, use the CLScommand.

    CLS

    More about the PRINT commandYou can use multiple print statements in your program.

    PRINT "Hello"PRINT "World"

    Output:

    HelloWorld

    To place World onto the previous line, place a semi-colon after PRINT "Hello".

    PRINT "Hello";PRINT "World"

    Output:

    HelloWorld

  • Also, if you put a comma instead of a semi-colon on the first line, the program will insert spacesbetween the two words.

    PRINT "Hello",PRINT "World"

    Output:

    Hello World

  • Variables

    This chapter discusses an important topic in programming, "variables." Please read this sectionthoroughly.

    A variable is a piece of data kept in the computer's memory (RAM). The location of a variable inRAM is called the "address."

    How a variable is stored in RAM

    The following program prints the variable X to the screen:

    print X

    Since the variable hasn't been assigned a number, the value of the variable is 0. So, the output ofthe program is:

    0

    This next program sets X to 15, and then prints the variable:

    X = 15

    print X

    This time, the output is:

    15

  • In the above example, the number 15 was stored in the computer's RAM at a certain memoryaddress. Then the PRINT command accessed (or looked at) that address when it printed "15" tothe screen.

    (NOTE: The memory address of X is not necessarily 1000000)

    ADVANCED TIP: Although you don't normallyneed to, you can find the actual memory addressof a variable (X, for example) by using theVARSEG and VARPTR commands.

    PRINT (VARSEG(X) * 65536) + VARPTR(X)

    (For more information, see Memory.)

    As in the programs above, a variable is accessed by calling its name. Variable names can have acombination of letters and numbers. The following are valid variables:

    Y

    num

    VALUE

    xYz

    abc123

  • Also, you can use multiple variables in your program.

    X = 82Y = 101Z = 79

    PRINT XPRINT YPRINT Z

    Output:

    8210179

    (NOTE: The memory addresses of these variables are not necessarily as specified)

    ExpressionsIf you pass an expression to a variable, the expression is evaluated and the variable is set to thatvalue.

    x = 500 + (10 * 7)

    PRINT x

    Output:

    570

  • You can also use variables as expressions.

    rate = 50time = 2

    distance = rate * time

    PRINT distance

    Output:

    100

    Plus, you can have both variables and numbers in an expression.

    X = 100Y = X * 7

    PRINT Y

    Output:

    700

    TIP: The following increases X by 1:

    X = X + 1

    Strings

    If you add a dollar sign ($) to the end of a variable, the variable is a string.

    X$ = "Hello World!"

    PRINT X$

    Output:

    Hello World!

    If you try to set a string to a non-string variable, an error occurs.

    X = "Hello World!"

    The QBasic interpreter says "Type mismatch" when you try to run the above program.

  • A string can be added to the end of an existing variable string.

    X$ = "Hello"X$ = X$ + "World"

    PRINT X$

    Output:

    HelloWorld

    You can also add variable strings together.

    a$ = "String1"b$ = "String2"c$ = "String3"

    d$ = a$ + b$ + c$

    PRINT d$

    Output:

    String1String2String3

  • Retrieving keyboard input from the user

    One way to receive input from the keyboard is with the INPUT command. The INPUT commandallows the user to enter either a string or a number, which is then stored in a variable.

    INPUT data$

    PRINT data$

    When this program is executed, the INPUT command displays a question mark, followed by ablinking cursor. And when you enter tex

View more